skew Posted July 26, 2011 Share Posted July 26, 2011 I had just began to learn PHP and now I stumbled over a problem. In PHP and also in HTML, it is possible to use single and double quoted strings. In HTML, for example, I can write the following code: <img src="test.jpg" /> <img src='test.jpg' /> Both lines are equivalent and the result on the screen is the same. The same is also in PHP. This gives me a headache and some confusing. Is there something like a coding-guideline when and where I should use which of the quotations? And what's with quotations which are nested in other strings like the following: echo "<img src='test.jpg' /> ? What are the reasons why PHP supports two kinds of quotations? In other popular programming languages (C#, Delphi, C/C++, ...) this doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/ Share on other sites More sharing options...
Nodral Posted July 26, 2011 Share Posted July 26, 2011 Hi In php ' ' and " " do have a very subtle difference. if you were to use the following $output="Hello World"; echo "I want to say $output"; You would see I want to say Hello World However if you put $output="Hello World"; echo 'I want to say $output'; You would see I want to say $output Also you can mix and match to output SQL or HTML eg $output='<div class="test">Hello World</div>"; or $sql="INSERT INTO table SET VALUES column1='data'"; Hope this helps!! Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247191 Share on other sites More sharing options...
trq Posted July 26, 2011 Share Posted July 26, 2011 PHP supports two kinds of quotes for a reason. It's not the only language to do so. Single quotes accept literal strings and do no interpolation on the contents within them. Double quoted string are interpolated however, meaning variables are parsed. eg; $foo = 'thorpe'; echo "Hello, my name is $foo, what's doing?"; Also note that single quotes can appear in double quoted strings (as shown above), and double quotes can appear in single quoted strings without needing to be escaped. Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247194 Share on other sites More sharing options...
Muddy_Funster Posted July 26, 2011 Share Posted July 26, 2011 PHP processes the contents of single and double quotes differenttly! single quotes define a litteral string, double quotes deffine a dynamic string (that's just what I have been calling it for a while now, can't remember the real name for it). try running the following code to see better what I am talking about. eg <?php $stringVariable = 'HELLO WORLD'; echo 'This line is wrapped in single quotes and as you can see -- $stringVariable -- does not get proccessed'; ehco "<BR><BR>"; echo "this line is wrapped in double quotes and as you can see --$stringVariable -- does get proccessed"; ?> I would personaly suggest that you do not get into the habbit of Nodral's suggestion on mix and match for SQL. Use double quotes to have variables proccessed within the string, otherwise use single quotes, and if you need to contain single quotes within your string escape them properly. -- That is purely my opinion and not to be taken as any suggestion of being the "proper" way of doing it. Meh- I type too slow Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247195 Share on other sites More sharing options...
the182guy Posted July 26, 2011 Share Posted July 26, 2011 As Nodral points out, using the single quote in PHP (') means that the PHP interpreter will not look for variables in the string. For me a good rule of thumb is: double quotes (") for HTML unless there's reason to use single, and single quotes for PHP string literals unless I need variables in it. Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247198 Share on other sites More sharing options...
skew Posted July 26, 2011 Author Share Posted July 26, 2011 It sounds like a question of taste, isn't it? Thanks for all your answers. Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247203 Share on other sites More sharing options...
WebStyles Posted July 26, 2011 Share Posted July 26, 2011 Not really a question of taste. It's a question of speed/necessity for me. If you use single quotes, PHP will not try to interpret variables inside them, therefore it will process faster. I normally use single quotes for most everything, except when I specifically need special interpretations, like when sending an email, if I need to insert a line break (\n) this will only be properly interpreted as a line break if it's withing double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247213 Share on other sites More sharing options...
arbitter Posted July 26, 2011 Share Posted July 26, 2011 Oh boy I didn't even know that and I've never stumbled upon any trouble! I'm in the habbit of always using double quotes though, so that if I echo a class or so I can easily single-quote it. Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247217 Share on other sites More sharing options...
skew Posted July 26, 2011 Author Share Posted July 26, 2011 Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/242826-single-or-double-quoted-strings-in-phphtml-and/#findComment-1247268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.