Monday, November 14, 2011

Difference between single quote and double quote string in PHP


A string is a series of characters. PHP imposes no boundary on the size of the string.


A string in PHP can be specified in 4 different ways:


1. Single Quoted
2. Double Quoted
3. Heredoc
4. Nowdoc


Single Quoted
Here the strings will be specified as it is. Check the example below


Example 1:

<?php
echo 'Hai, \n I am Solutio'; //Output: Hai, \n I am Solutio
?>
As you see in the above example \n, which is line feed, is displayed as such when printed with single quotes.

Example 2:


<?php
$name="Solutio";
echo 'Hai, I am $name'; //Output: Hai, I am $name
?>
In example 2, the variable $name is not expanded on usage of single quotes

Example 3:

<?php
echo 'Wait at St. Joseph's Church'; 
?>
Output: Parse error: parse error, expecting `','' or `';'' in C:\wamp\www\test.php on line 2

Oops you got an error.
So you expected the output to be Wait at St.Joseph's Church. To specify a literal single quote, escape it with a backslash, as shown below


<?php
echo 'Wait at St. Joseph\'s Church'; //Output: Wait at St.Joseph's Church
?>


So what if I want to display a literal backslash, double it (\\).




Double Quoted


Double quoted strings can display escape characters like \n etc and also they can expand variables as shown below.


Example 1:

<?php
echo "Hai, \n I am Solutio"; 
?>


Output: Hai, I am Solutio


Wait you may be thinking why "I am Solutio" is not in the next line. Remember that HTML does not render newlines. You need to use <br />. Well if you had printed it to a 'txt' file it will be in the new line.


Example 2:

<?php
  $name="Solutio";
  echo "Hai, I am $name"; // Output: Hai, I am Solutio
?>

So double quotes can expand the variables.


Example 3:
Ravi said, "You are a fool"
If you need an output as above, do as below



<?php
echo "Ravi said, \"You are a fool\"";
?>



Example 4:
You can use curly braces to isolate the name of the variable. i.e. You have a variable $type. 


<?php
$type="data";
echo "All the {$type}s are corrupted";
?>






Heredoc



Heredoc string works like double quoted strings. The syntax is as follows: <<< After this (i.e. <<<) operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.


 I know you didn't get it. See the example below


Example:
<?php
  $name="Solutio";
  echo <<<CHK
    My name is "$name". 
    Please check my blog.
  CHK;
?>


Output: My name is "Solutio". Please check my blog.


Now read the syntax again. 


Now you got it. Please note that CHK is just an identifier. You can specify your own identifier.




Nowdoc


Nowdoc string works like single quoted strings. It works similar to heredoc. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'CHK'.


It was added in PHP 5.3.0 version.


Example:
<?php
  $name="Solutio";
  echo <<<'CHK'
    My name is "$name". 
    Please check my blog.
  CHK;
?>


Output: My name is "$name". Please check my blog.


Why $name in the output? Initially I did specify that Nowdoc string works like single quoted strings.


Hope you got the idea!

1 comment:

  1. Very informative post!
    Always had a confusion between these qoutes..!

    Thanks Magister!
    =)
    -Vaishakh Pushpoth.

    ReplyDelete