note 78067 added to language.types.string
| From: | rkfranklin+php at gmail dot com | Date: | Wed, 26 Sep 2007 19:35:45 +0000 |
| Subject: | note 78067 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130803@lists.php.net to get a copy of this message | ||
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 -- https://master.php.net/note/delete/78067/integrated
Del: useless -- https://master.php.net/note/delete/78067/useless
Del: bad code -- https://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 -- https://master.php.net/note/delete/78067/in+docs
Del: other reasons-- https://master.php.net/note/delete/78067
Reject -- https://master.php.net/note/reject/78067
Search -- https://master.php.net/manage/user-notes.php