your image

PHP Strings - javatpoint

javapoint
Related Topic
:- PHP programming languages

PHP String

PHP string is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character set and so that it does not offer native Unicode support. There are 4 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

We can create a string in PHP by enclosing the text in a single-quote. It is the easiest way to specify string in PHP.

For specifying a literal single quote, escape it with a backslash (\) and to specify a literal backslash (\) use double backslash (\\). All the other instances with backslash such as \r or \n, will be output same as they specified instead of having any special meaning.

For Example

 

10 Sec

6. MVC Model & PHP Singleton pattern | Build a CMS using OOP PHP tutorial MVC [2020]

Following some examples are given to understand the single quoted PHP String in a better way:

Example 1

  1. <?php  
  2.        $str='Hello text within single quote';  
  3.        echo $str;  
  4. ?>  

Output:

Hello text within single quote

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

Example 2

  1. <?php  
  2. $str1='Hello text   
  3. multiple line  
  4. text within single quoted string';  
  5. $str2='Using double "quote" directly inside single quoted string';  
  6. $str3='Using escape sequences \n in single quoted string';  
  7. echo "$str1 <br/> $str2 <br/> $str3";  
  8. ?>  

Output:

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

Example 3

  1. <?php  
  2. $num1=10;   
  3. $str1='trying variable $num1';  
  4. $str2='trying backslash n and backslash t inside single quoted string \n \t';  
  5. $str3='Using single quote \'my quote\' and \\backslash';  
  6. echo "$str1 <br/> $str2 <br/> $str3";  
  7. ?>  

Output:

 

trying variable $num1trying backslash n and backslash t inside single quoted string \n \tUsing single quote 'my quote' and \backslash

Note: In single quoted PHP strings, most escape sequences and variables will not be interpreted. But, we can use single quote through \' and backslash through \\ inside single quoted PHP strings.

Double Quoted

In PHP, we can specify string through enclosing text within double quote also. But escape sequences and variables will be interpreted using double quote PHP strings.

Example 1

  1. <?php  
  2. $str="Hello text within double quote";  
  3. echo $str;  
  4. ?>  

Output:

Hello text within double quote

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

Example 2

 

  1. <?php  
  2. $str1="Using double "quote" directly inside double quoted string";  
  3. echo $str1;  
  4. ?>  

Output:

Parse error: syntax error, unexpected 'quote' (T_STRING) in C:\wamp\www\string1.php on line 2

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

Example 3

 

  1. <?php  
  2. $str1="Hello text   
  3. multiple line  
  4. text within double quoted string";  
  5. $str2="Using double \"quote\" with backslash inside double quoted string";  
  6. $str3="Using escape sequences \n in double quoted string";  
  7. echo "$str1 <br/> $str2 <br/> $str3";  
  8. ?>  

Output:

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

In double quoted strings, variable will be interpreted.

Example 4

  1. <?php  
  2. $num1=10;   
  3. echo "Number is: $num1";  
  4. ?>  

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

  1. <?php  
  2.     $str = <<<Demo  
  3. It is a valid example  
  4. Demo;    //Valid code as whitespace or tab is not valid before closing identifier  
  5. echo $str;  
  6. ?>  

Output:

It is a valid example

Invalid 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.

  1. <?php  
  2.     $str = <<<Demo  
  3. It is Invalid example  
  4.        Demo;    //Invalid code as whitespace or tab is not valid before closing identifier  
  5. echo $str;  
  6. ?>  

This code will generate an error.

Output:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\xampp\PMA\heredoc.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

  1. <?php  
  2.     $city = 'Delhi';  
  3.     $str = <<<DEMO  
  4. Hello! My name is Misthi, and I live in $city.  
  5. DEMO;  
  6.     echo $str;  
  7.  ?>  

Output:

Hello! My name is Misthi, and I live in Delhi.

Example

We can add multiple lines of text here between heredoc syntax.

  1. <?php  
  2.     $str = <<<DEMO  
  3. It is the example   
  4. of multiple  
  5. lines of text.  
  6. DEMO;  
  7.     echo $str;  
  8.   
  9. echo '</br>';  
  10.   
  11. echo <<<DEMO    // Here we are not storing string content in variable str.   
  12. It is the example   
  13. of multiple  
  14. lines of text.  
  15. DEMO;  
  16.  ?>  

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

Example

  1. <?php  
  2. class heredocExample{  
  3.         var $demo;  
  4.         var $example;  
  5.         function __construct()  
  6.         {  
  7.                 $this->demo = 'DEMO';  
  8.                 $this->example = array('Example1', 'Example2', 'Example3');  
  9.         }  
  10.     }  
  11.     $heredocExample = new heredocExample();  
  12.     $name =  'Gunjan';  
  13.       
  14.     echo <<<ECO  
  15.     My name is "$name". I am printing some $heredocExample->demo example.  
  16.     Now, I am printing {$heredocExample->example[1]}.  
  17.     It will print a capital 'A': \x41  
  18. ECO;  
  19.  ?>  

Output:

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

Newdoc

Newdoc is similar to the heredoc, but in newdoc parsing is not done. It is also identified with three less than symbols <<< followed by an identifier. But here identifier is enclosed in single-quote, e.g. <<<'EXP'. Newdoc follows the same rule as heredocs.

The difference between newdoc and heredoc is that - Newdoc is a single-quoted string whereas heredoc is a double-quoted string.

Note: Newdoc works as single quotes.

Example-1:

  1. <?php  
  2.     $str = <<<'DEMO'  
  3.     Welcome to javaTpoint.  
  4.            Learn with newdoc example.  
  5. DEMO;  
  6. echo $str;  
  7. echo '</br>';  
  8.   
  9. echo <<< 'Demo'    // Here we are not storing string content in variable str.  
  10.     Welcome to javaTpoint.  
  11.            Learn with newdoc example.  
  12. Demo;  
  13. ?>  

Output:

Welcome to javaTpoint. Learn with newdoc example.Welcome to javaTpoint. Learn with newdoc example.

Go to view page source and see the source of the program.

Example

The below example shows that newdoc does not print the variable's value.

  1. <?php  
  2. class heredocExample{  
  3.         var $demo;  
  4.         var $example;  
  5.         function __construct()  
  6.         {  
  7.                 $this->demo = 'DEMO';  
  8.                 $this->example = array('Example1', 'Example2', 'Example3');  
  9.         }  
  10.     }  
  11.     $heredocExample = new heredocExample();  
  12.     $name =  'Gunjan';  
  13.       
  14.     echo <<<ECO  
  15.     My name is "$name". I am printing some $heredocExample->demo example.  
  16.     Now, I am printing {$heredocExample->example[1]}.  
  17.     It will print a capital 'A': \x41  
  18. ECO;  
  19.  ?>  

Output:

The output of the above program will be like:

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

Note: newdoc supported by PHP 5.3.0+ versions.

Invalid Example

We cannot use any whitespace or tab before and after the identifier and semicolon, means identifier must not be indented. The identifier must begin from the new line. It is also invalid in newdoc same as heredoc.

  1. <?php  
  2.     $str = <<<'Demo'  
  3. It is Invalid example  
  4. Demo;  //Invalid code as whitespace or tab is not valid before closing identifier  
  5. echo $str;  
  6. ?>  

This code will generate an error.

Output:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\xampp\PMA\newdoc.php on line 7

Comments