More about Strings in PHP. Use of single and double quotes and Escaped characters (Chapter 3)

No Comments

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.

Previous Learning

<?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.
*/

?>

 

Single quoted

The simplest way to specify a string is to enclose it in single quotes.

 

Double quoted

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 ( " " )
*/

?>

 

Escaped characters

Sequence Meaning
\n linefeed
\r carriage return
\t horizontal tab
\v vertical tab  (since PHP 5.2.5)
\e escape (since PHP 5.4.0)
\f form feed (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\” double-quote

 

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.

*/

?>

 

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.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

Fields marked with an * are required
July 2019
M T W T F S S
« Sep    
1234567
891011121314
15161718192021
22232425262728
293031  

More from our blog

See all posts
No Comments