Re: When and when not to use ' and "

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 6/2/07, Brian Seymour <brian@xxxxxxxxxxxx> wrote:
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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



   Brian,

   Single quotes give a literal value of the data passed, while
double quotes give the translated value.  For example, if you have the
following:
<?
$the = "end";

echo '$the';

echo " ";

echo "$the";
?>

   .... you'll have the following output:
$the end

   The same counts for escaped characters, such as the newline
character (\n).  If you put it in single quotes, it's processed as a
literal value; the same newline inserted in double quotes is displayed
properly, as again, it's processed as a translated value.

   So:
<?
$word = "this";

echo '$word\nworks';

echo "\n\n";

echo "$word\nworks";
?>

   Shows:
$word\nworks

this
works


   Hope that helps a bit.  I'm in the middle of moving furniture into
my new house and can't get the bed upstairs, so I decided to take a
break and grab some "sanity" from the list.  Talk about your
oxymorons....


--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux