Why is it that I can do this:
$x[1] = 2; $y[2] = "blah"; $a = $y[$x[1]]; print $a; // prints "blah"
but I can't print $y[$x[1]] directly:
$x[1] = 2; $y[2] "blah"; print "$y[$x[1]]";
I get a 'Parse Error' (expecting `']'')
print $y[$x[1]];
instead of print "$y[$x[1]]";
works, but as to why...err check the manual for print
hth
I figured it out. You have to use:
print "{$y[$x[1]]}";
Originally posted by supersalo I figured it out. You have to use: print "{$y[$x[1]]}";
You don't have to; as jernhenrik noted, you could just
In fact, it's preferable that you do. By using
you are:
creating a string,
looking through the string to see if there is a variable in it,
reading the variable,
getting the variable's contents,
converting those contents to a string if necessary,
replacing the variable's name in the string with those contents,
printing out the resulting string.
Which is a hell of a lot slower than
Get the contents of a variable
convert those contents to a string if necessary
print that string.
The reason I'm not just using
is because I'm trying to include the value as part of a string (which is actually part of an HTML form)
You can concatenate strings instead of having the variable parsed within the string.
print "Like this: " . $y[$x[1]];