Jump to content

Single or double quoted strings in PHP/HTML (" and ')


skew

Recommended Posts

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.  :o

 

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.

Link to comment
Share on other sites

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!!

 

;D

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :shrug:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.