尝试通过http-equiv重定向时出错


Error when I try to redirect by http-equiv

这是我的代码:

if ( $A=="3" ) {
<meta http-equiv="'refresh'" content='"0; url=http://ex.com/"> 
}

错误:

Parse error: syntax error, unexpected '<' in /home/ex/public_html/sy.php on line 94

请问有什么问题?

问题很明显,您有一个'<'(比)号突然出现,就在左括号后面,这是一个语法错误。我想你的意思是:

if ( $A=="3" ) {
echo '<meta http-equiv="refresh" content="0; url=http://ex.com">'; 
}

或者(如果你在"视图"或类似的视图中):

<?php if ( $A=="3" ) {  ?>
<meta http-equiv="refresh" content="0; url=http://ex.com">; 
<?php } ?>

问题是

<meta http-equiv="'refresh'" content='"0; url=http://ex.com/"> 
是html。所以你必须使用echo语句来打印出来,而不是像那样写/所以它应该是

if ( $A=="3" ) {
echo '<meta http-equiv="refresh" content="0; url=http://ex.com">'; 
}

按照您使用它的方式,您的PHP将继续执行,并且在客户端浏览器执行HTML元标记之前,页面不会重定向。也许这就是你想要它做的,但如果不是,这可能是一个更好的解决方案。

if ($A == "3")
    die(header("Location: http://ex.com/"));

否则,正如其他海报已经指出的那样,您需要echo:

if ($A == "3")
    echo "<meta http-equiv='"refresh'" content='"0; url=http://ex.com/'">";