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.
echo 'This is a string with single quotes';
$Name = 'David';
/* 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.
$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 ( " " )
vertical tab (since PHP 5.2.5)
escape (since PHP 5.4.0)
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.
echo nl2br("One line.\nAnother line.");
Browser Output :
HTML Output (View Source):
One line.<br />
A website developer, loves #wordpress #drupal #php. Love posting about wordpress, php, bootstrap and SEO. Also provide online/offline training on wordpress, drupal, php and SEO. I am available on Twitter @ anibabbar , Skype @anibabbar , gmail @anibabbar !! facebook @thinkncode.com
About us and this blog
We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.