2011-02-05 135 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']}"); 
    } 
}