If you want to use a variable in an array index within a
double quoted string you have to realize that when you put
the curly braces around the array, everything inside the
curly braces gets evaluated as if it were outside a string.
Here are some examples:
<?php
$i = 0;
$myArray[Person0] = Bob;
$myArray[Person1] = George;
// prints Bob (the ++ is used to emphasize that the
expression inside the {} is really being evaluated.)
echo "{$myArray['Person'.$i++]}<br>";
// these print George
echo "{$myArray['Person'.$i]}<br>";
echo
"{$myArray["Person{$i}"]}<br>";
// These don't work
echo "{$myArray['Person$i']}<br>";
echo "{$myArray['Person'$i]}<br>";
// These both throw fatal errors
// echo "$myArray[Person$i]<br>";
//echo "$myArray[Person{$i}]<br>";
?>
----
Server IP: 208.69.120.35
Probable Submitter: 155.98.27.144
----
Manual Page -- http://www.php.net/manual/en/language.types.string.php
Edit -- https://master
.php.net/note/edit/78067
Del: integrated -- h
ttps://master.php.net/note/delete/78067/integrated
Del: useless -- http
s://master.php.net/note/delete/78067/useless
Del: bad code -- htt
ps://master.php.net/note/delete/78067/bad+code
Del: spam -- https:/
/master.php.net/note/delete/78067/spam
Del: non-english --
https://master.php.net/note/delete/78067/non-english
Del: in docs -- http
s://master.php.net/note/delete/78067/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78067
Reject -- https://mast
er.php.net/note/reject/78067
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|