IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Double quotes force evaluation of the contents
When you use double quotes, the parser has to read everything inside and decide if it's a variable that needs to be expanded. If you use single quotes, it just looks for the next single quote.

To get single quotes into a single-quoted string, you double it:
echo 'That''s the way.';


To get double quotes into a double-quoted string you escape it:
echo "He said, \\"Do this.\\"";


Reverse it and you get:
echo "That's the way.";
echo 'He said, "Do this."';


So there are lots of ways to do it. As a style issue, I prefer to only use double quotes when outputting text to the browser and dropping in values as I go. Within array keys, I always use single-quotes.

Poorly-documented gotcha. What's the difference?
echo "This belongs to $user["name"].";
echo "This belongs to $user['name'].";
echo "This belongs to $user[name].";

echo "This belongs to ".$user["name"].".";
echo "This belongs to ".$user['name'].".";
echo "This belongs to ".$user[name].".";

echo 'This belongs to '.$user["name"].'.';
echo 'This belongs to '.$user['name'].'.';
echo 'This belongs to '.$user[name].'.';


If I remember correctly, the first two don't work, the third one does. The last six all work, but the second-to-last is the way I'd do it. It's unambiguous and easy to read.


Oh, and while I can't remember the exact syntactic difference, I know I prefer a period instead of a comma in echo 'The time is now ',$time;
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New I thought the period was used to...
Oh, and while I can't remember the exact syntactic difference, I know I prefer a period instead of a comma in

echo 'The time is now ',$time;



I thought the period was used to append information to something in the previous line of a script?

For example:


echo 'Hello';
echo .' World';


Gave you "Hello World" -- both appearing on one line.
"We are all born originals -- why is it so many of us die copies?"
- Edward Young
New Err, not really
I thought the period was used to append information to something in the previous line of a script?

For example:


echo 'Hello';
echo .' World';


Gave you "Hello World" -- both appearing on one line.

If you run that it will print 'Hello World' on one line, but it has nothing to do with the period. PHP don't automatically include any line breaks, so everything goes on one line unless you include some yourself. This can be confusing, because if the output is HTML, it will often be rendered on multiple lines even if the output is on one.

Doing
echo 'This is one <br /> line of output';
will produce one line of output but render as two lines of HTML. But doing
echo "This is one line /n";
echo "and this is another one";
will output two lines and render as one in HTML.

The period is string concatenation in PHP. $name = 'Jay' . ' ' . 'Mehaffey' sets $name to 'Jay Mehaffey'.

The echo command takes any number of values, seperated by commas, and outputs them.

Thus echo 'The time is now ',$time; and echo 'The time is now' . $time are actually doing two subtly different things. The first outputs 'The time is now' and then outputs $time right after it. The second puts 'The time is now' and $time together into one string and then outputs that.

Jay
     Learning PHP Part Deux: When to use (') and when to use (") - (cwbrenn) - (12)
         Keeping in mind that I don't know PHP at all... - (Yendor)
         Double quotes force evaluation of the contents - (drewk) - (2)
             I thought the period was used to... - (cwbrenn) - (1)
                 Err, not really - (JayMehaffey)
         Well they are never required - (JayMehaffey)
         On quoting array keys - (drewk) - (5)
             I have to say... - (cwbrenn) - (4)
                 It looks better in vim - (drewk) - (3)
                     Kate or Quanta... - (cwbrenn) - (2)
                         I can't write code without syntax highlighting - (drewk) - (1)
                             both Kate and Quanta have syntax highlighting -NT - (cwbrenn)
         Sometimes it matters, sometimes it doesn't. - (static)

There's no wraith like an Old wraith.
38 ms