[英]Variable into Header Location
How can i include a variable and make it part the string. 我如何包含变量并使它成为字符串的一部分。
header("Location: http://www." . "$_SESSION['domainname']");
The above code doesn't work. 上面的代码不起作用。
The reason why your code didn't work is due to how PHP handles indexed arrays inside strings. 您的代码无法正常工作的原因是由于PHP如何处理字符串内部的索引数组。 You had:
你有过:
"$_SESSION['domainname']"
But what PHP wanted to see was: 但是PHP希望看到的是:
"$_SESSION[domainname]"
No single quotes this time. 这次没有单引号。 You only omit those single quotes if you are referencing a variable directly inside a string.
仅当直接在字符串内部引用变量时,才忽略那些单引号。
Note that string interpolation, such as this, can work with simple arrays ( "$a[x]"
) but not with arrays of arrays ( "$a[x][y]"
) unless you use curly braces ( {$x}
, {$a['x']['y']}
; note the single quotes in the curly braces--they aren't exactly like PHP's normal string interpolation, but rather more like referencing a variable elsewhere in PHP). 请注意,这样的字符串插值可以使用简单的数组(
"$a[x]"
),但是不能用于数组的数组( "$a[x][y]"
),除非您使用花括号( {$x}
, {$a['x']['y']}
;注意花括号中的单引号-它们与PHP的普通字符串插值并不完全一样,而更像是引用PHP中其他地方的变量)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.