2011-02-05 139 views
1

我有一段代码保持失败。这个PHP代码有什么问题?

if (isset($_GET ['id']) && $rawdata) { 
     if ($_SERVER["REQUEST_URI"] != $rawdata ['htmltitle']) { 
     header("Location: http://".$_SERVER['SERVER_NAME'].".$rawdata['htmltitle']."); 
     } 
    } 

失败,是一个与它的头,和失败,该行:

PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING 

什么地方不对劲任何想法?

回答

4

如果你索引到一个数组中的字符串中,你需要使用{}

header("Location: http://${_SERVER['SERVER_NAME']}${rawdata['htmltitle']}"); 
+0

是不是你的错边$ {? – 2011-02-05 22:52:49

+0

@Jared它可以工作 – 2011-02-05 22:54:23

4

你必须写:

header("Location: http://$_SERVER[SERVER_NAME]$rawdata[htmltitle]"); 

或者:

header("Location: http://{$_SERVER['SERVER_NAME']}{$rawdata['htmltitle']}"); 

或者:

header('Location: http://' . $_SERVER['SERVER_NAME'] . $rawdata['htmltitle']); 

您不能在字符串的数组偏移量中使用字符串(T_CONSTANT_ENCAPSED_STRING),您需要使用T_STRING

1

http://php.net/manual/en/language.types.string.php PHP是在内部字符串变量插值发牢骚:你必须使用{braces}包装用数组索引字符串和表达式

这应该工作

if (isset($_GET ['id']) && $rawdata) { 
    if ($_SERVER["REQUEST_URI"] != $rawdata ['htmltitle']) { 
    header("Location: http://{$_SERVER['SERVER_NAME']}{$rawdata['htmltitle']}"); 
    } 
}