PHP String

PHP string is a sequence of characters used to store and manipulate text. PHP supports only a 256-character set and does not offer native Unicode support. There are four ways to specify a string literal in PHP:

  1. Single quoted
  2. Double quoted
  3. Heredoc syntax
  4. Newdoc syntax (since PHP 5.3)

Single Quoted

A string in PHP can be created by enclosing the text in single quotes. To specify a literal single quote, use a backslash (), and for a literal backslash, use double backslashes (). Other escape sequences like n or t will not be interpreted.
We can store multiple line text, special characters, and escape sequences in a single-quoted PHP string.

Examlpe 1

Addition Form

 
<?php  
$str = 'Hello text within single quote';  
echo $str;  
?> 
                
                    

Output

Hello text within single quote

Examlpe 2

 
<?php  
$str1 = 'Hello text  
multiple line  
text within single quoted string';  
$str2 = 'Using double "quote" directly inside single quoted string';  
$str3 = 'Using escape sequences n in single quoted string';  
echo "$str1 <br/> $str2 <br/> $str3";
?> 
                
                    

Output

Hello text multiple line text within single quoted string Using double "quote" directly inside single quoted string Using escape sequences n in single quoted string

Examlpe 3

 
<?php  
$num1=10;   
$str1='trying variable $num1';  
$str2='trying backslash n and backslash t inside single quoted string n t';  
$str3='Using single quote 'my quote' and backslash';  
echo "$str1 
$str2
$str3"; ?>

Output

trying variable $num1 trying backslash n and backslash t inside single quoted string n t Using single quote 'my quote' and backslash

Double Quoted

Double-quoted strings allow escape sequences and variable parsing.

Examlpe 1

 
<?php  
$str="Hello text within double quote";  
echo $str;  
?> 
                   

Output

Hello text within double quote

Now, you can't use double quote directly inside double quoted string.

Examlpe 2

 
<?php
$str1="Using double "quote" directly inside double quoted string";  
echo $str1;    
?> 
        

Output

Parse error: syntax error, unexpected 'quote' (T_STRING) in C:wampwwwstring1.php on line 2

We can store multiple line text, special characters and escape sequences in a double quoted PHP string.

Examlpe 3

 
<?php  
?php  
$str1="Hello text   
multiple line  
text within double quoted string";  
$str2="Using double "quote" with backslash inside double quoted string";  
$str3="Using escape sequences n in double quoted string";  
echo "$str1 <br/> $str2 <br/> $str3";     
?> 
           

Output

Hello text multiple line text within double quoted string Using double "quote" with backslash inside double quoted string Using escape sequences in double quoted string

In double quoted strings,variable will be interpreted.

Example 4

 
<?php  
$num1=10;   
echo "Number is: $num1";     
?> 
 

Output

Number is: 10

Heredoc

Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an identifier is provided after this heredoc <<< operator, and immediately a new line is started to write any text. To close the quotation, the string follows itself and then again that same identifier is provided. That closing identifier must begin from the new line without any whitespace or tab.

Naming Rules

The identifier should follow the naming rule that it must contain only alphanumeric characters and underscores, and must start with an underscore or a non-digit character.

For Example

Valid Example

 
 <?php    
$str = < < <Demo  
It is a valid example  
Demo;    //valid code as whitespace or tab is not valid before closing identifier  
echo $str;  
?>
            

Output

It is a valid example

We cannot use any whitespace or tab before and after the identifier and semicolon, which means identifier must not be indented. The identifier must begin from the new line.

Invalid Example

 
<?php  
$str = < < <Demo  
It is Invalid example  
    Demo;    //Invalid code as whitespace or tab is not valid before closing identifier  
echo $str;  
?>
        
        
        

This code will generate an error.

Output

Parse error: syntax error, unexpected end of file in C:xampphtdocsxamppPMAheredoc.php on line 7

Heredoc is similar to the double-quoted string, without the double quote, means that quote in a heredoc are not required. It can also print the variable's value.

Example

 
<?php  
$str =< < <Demo  
It is the example   
of multiple  
lines of text.  
DEMO;  
  echo $str;  

echo '</br>';  
echo < <Demo     // Here we are not storing string content in variable str.   
It is the example   
of multiple  
lines of text.  
DEMO;   
?>
        

Output

It is the example of multiple lines of text. It is the example of multiple lines of text.

Below are the example with class and their variable

 
<?php  
class heredocExample{  
var $demo;  
var $example;  
function __construct()  
{  
        $this->demo = 'DEMO';  
        $this->example = array('Example1', 'Example2', 'Example3');  
}  
}  
$heredocExample = new heredocExample();  
$name =  'Gunjan';  

echo < < <ECO  
My name is "$name". I am printing some $heredocExample->demo example.  
Now, I am printing {$heredocExample->example[1]}.  
It will print a capital 'A': x41  
ECO;  
?>  

?>
        

Output

My name is "Gunjan". I am printing some DEMO example. Now, I am printing Example2. It will print a capital 'A': A