When and when not to use ' and "
| From: | Brian Seymour | Date: | Sat, 02 Jun 2007 20:15:50 +0000 |
| Subject: | When and when not to use ' and " | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-256014@lists.php.net to get a copy of this message | ||
I noticed today when I was using mysql_fetch_array something weird happened.
Database:
| id |
1
Code:
$colVal = "id";
$foo=mysql_fetch_array($someresult, MYSQL_ASSOC);
Now all I wanted to do was get the value of 1 into the variable $bar. Please
assume $someresult was the direct product of mysql_query("select * from
thistable");.
$bar = $foo['$colVal']; // didn't work
$bar = $foo['{$colVal}']; // didn't work
$bar = $foo[$colVal]; // worked
$bar = $foo['id']; // obviously worked
What I don't understand is why the first or second option didn't work.
Can anybody shed some light on this?
Brian Seymour
AeroCoreProductions
http://www.aerocore.net/