I'm doing a basic quiz . I don't understand the last part: $_SESSION["x{$user['serial']}"]
What exactly does x{5}
array value mean? x
isn't defined anywhere, and in general object literals should be like {x: 5}
– some help with this would be great.
session_start();
function setData($data){
if(is_array($data)){
$x = 1;
foreach ($data as $key => $value) {
$_SESSION["x$value"] = $x;
$x++;
}
}
}
$user = array('id'=>3,'serial'=>5);
setData($user);
echo $_SESSION["x{$user['serial']}"];
You can't access (all kinds of) arrays in a double quoted string.
For instance:
echo "x$user['serial']";
This results in:
PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in php shell code on line 1
In other words, access array elements in an indexed array ("$user[0]"
) works, but accessing associative elements with a string literal doesn't.
doesn't work because it is too complicated to parse. By using accolades, you say the part should be interpreted separately.
So:
echo "x{$user['serial']}";
is equivalent to:
echo "x".$user['serial'];
The resulting code is thus:
echo $_SESSION["x".$user['serial']];
But more simple.
Given your sample $user
, the result is that you access x5
from $_SESSION
.
The curly brackets is used for expanding complex expressions inside double-quote strings, like arrays.
So, in your example, x{$user['serial']}
will expand to "x5". You can also write x$value
as x{$value}
, both with the same result.
BTW that object literal is for Javascript, not PHP :)
It is not x{5}
because it doesn't mean anything. The expression:
"x{$user['serial']}"
is a string enclosed in double quotes. Inside strings enclosed in double quotes, PHP does what in other languages is called "variables interpolation". In plain English, it searches for variable names and replaces them with their values.
It can easily recognize variable names in strings and replace them but the things become more difficult when you want to use more complex expressions like arrays or objects. It could try to guess what the programmer wants but this is not a solution.
That's why PHP finds only simple variables inside the double quotes and relies on the programmer to signal when they want to insert complex things like arrays and objects. And the programmer uses curly braces ({
and }
) to tell PHP about the complex expressions inserted in strings.
Check the documentation about string parsing on PHP manual.
Without using the curly braces, in the string "x$user['serial']"
PHP recognizes the variable $user
and ignores the ['serial']
part. It attempts to replace $user
with its string representation and because it is an array, the final string becomes "xArray['serial']"
and that is not what you want.
Using curly braces allows the programmer to write something like "x{$user[$field]}"
given the variable $field
was initialized forehand with the string 'serial'
.
If $user['serial'] == 5
then the final outcome is $_SESSION["x5"]
.
Curly braces can always be used to enclose the variable names in double quoted strings. They are useful, for example, in this situation:
$sep = '--';
$str = "a{$sep}b";
// echo($str) will produce: a--b
Without curly braces around the variable $sep
, the string looks like "a$sepb"
, PHP finds inside it the variable name $sepb
and because no such variable exists, the value of $str
becomes "a"
and this is not what we intended.