Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
When and when not to use ' and "
Old
  (#1)
Brian Seymour
Guest
 
Posts: n/a
Default When and when not to use ' and " - 06-02-2007, 10:08 PM

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_***OC);

Now all I wanted to do was get the value of 1 into the variable $bar. Please
***ume $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/
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: [PHP] When and when not to use ' and "
Old
  (#2)
Daniel Brown
Guest
 
Posts: n/a
Default Re: [PHP] When and when not to use ' and " - 06-02-2007, 10:08 PM

On 6/2/07, Brian Seymour <EMAIL REMOVED> 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_***OC);
>
> Now all I wanted to do was get the value of 1 into the variable $bar. Please
> ***ume $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 p***ed, 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
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] When and when not to use ' and "
Old
  (#3)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] When and when not to use ' and " - 06-04-2007, 01:37 AM

On Sat, June 2, 2007 3:15 pm, Brian Seymour wrote:
> $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?


' is actually easier to explain.

' has only TWO special characters you can embed in it: ' and \

$foo = 'That\'s life!';
$bar = 'The backslash \\ should be escaped, because it\'s special';

You don't HAVE to escape \ with \\ if PHP won't get "confused" by
whatever else you have in your string, but it's a Good Idea, imho, to
just always escape it, so you understand what the string parser is
doing internally.

" is a bit more complex.

In addition to " and \ being special, exactly parallel to ' and \
inside '', you also have:
variables like $foo and 1-D arrays like $foo[id]
special control characters like \n \t \r
any char you want with octal
{ and } for some versions (ugh) of PHP

There is also heredoc syntax, which is probably best for large chunks
of text, in general.

Full documentation is here:
http://us.php.net/manual/en/language.types.string.php

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




Contact Us - Forum Care Forums - Archive - Top