In this topic we will learn about use of Single quotes ( ‘ ‘ ) and double quotes ( ” ” ) in php. In last chapter we learned about variables and their usage in php. We learnt from last two chapters that while echoing strings we used single quotes and while echoing variables we used NO Quotes.
In between if you are interested in php learning check this post on php training in delhi. We provide both online and offline classes on php, drupal, wordpress training, SEO and HTML/CSS.
<?php echo 'This is a string with single quotes'; $Name = 'David'; echo $Name; /* NO quotes used while echoing a variable. Now we will learn in detail use single and double quotes plus their differences. */ ?>
The simplest way to specify a string is to enclose it in single quotes.
If the string is enclosed in double-quotes (“), PHP will interpret more escape sequences for special characters. You can also add a variable into a string using double quotes. Let me show you with an example from php.net, little edited by me.
<?php $juice = "apple"; // Case 1 echo "He drank some $juice juice.".PHP_EOL; /* .PHP_EOL represents end of line Output = He drank some apple juice. */ // Case 2 echo 'He drank some $juice juice.'; /* Output = He drank some $juice juice. So its clear that to use a variable in a string we need to use double quotes ( " " ) */ ?>
|\v||vertical tab (since PHP 5.2.5)|
|\e||escape (since PHP 5.4.0)|
|\f||form feed (since PHP 5.2.5)|
Escaped Characters in Detail.
W3Schools.com, one of the best tutorial site gives perfect examples to understand how Escaped character works.
Case 1 : \n (linefeed in php) and its use with nl2br function
The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.
<?php echo nl2br("One line.\nAnother line."); ?> <?php /* Browser Output : One line. Another line. */ /* HTML Output (View Source): One line.<br /> Another line. */ ?>