TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   single quotes or double quotes in php (http://www.talkphp.com/absolute-beginners/3932-single-quotes-double-quotes-php.html)

planepixel 02-03-2009 03:29 AM

single quotes or double quotes in php
 
Hi

i am confused about which quotes to use.
PHP Code:

 $firstName "first name";
 
$secondName 'second name';

 echo 
'first name is ' $firstName '. it is good name ';
 echo 
"second name is $secondName."

which approach is good. I read that single quotes give better performance.
which one you recommend?

thanks

Krik 02-03-2009 04:34 AM

Go with single quote better performance and when you have to echo out HTML you can keep your double quotes for your HTML.

PHP Code:

echo '<span id="myspan">' $mytext '</span>'

Also a note on the example code you game that last echo

PHP Code:

echo "second name is $secondName."

That "." may get in trouble. Better to

PHP Code:

echo 'second name is' $secondName '.'


Wildhoney 02-03-2009 01:08 PM

Though don't forget that any special character beginning with a back-slash: \b (backspace), \n (line break), \t (tab), etcetera, doesn't work as expected when encapsulated with single quotes. For that to work you must break out of your single quotes:

php Code:
echo 'Put this on' . "\n";
echo '...Two lines!';

CoryMathews 02-03-2009 01:35 PM

the amount of speed gained won't matter to much. Usually thats not the bottleneck. So it really comes down to which one you want to use and which one is easier in the specific case.

Wildhoney 02-04-2009 01:44 AM

Actually, Cory has a point because most of the time I tend to use sprintf for readability purposes.

planepixel 02-04-2009 05:48 AM

Thanks. no confusion now.


All times are GMT. The time now is 07:31 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0