note 78067 deleted from language.types.string by cmb
| From: | cmb@php.net | Date: | Thu, 18 Nov 2021 12:55:51 +0000 |
| Subject: | note 78067 deleted from language.types.string by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217262@lists.php.net to get a copy of this message | ||
Note Submitter: rkfranklin+php at gmail dot com
----
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>";
?>