简体   繁体   中英

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. You had:

"$_SESSION['domainname']"

But what PHP wanted to see was:

"$_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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM