PHP Data Types Strings

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP Data Types: Strings


Strings are a sequence of characters. In PHP, a character is the same as a byte, therefore there are exactly 256 different characters possible. The long string is supported in PHP, in fact, there is no practical bound to the size of strings. But PHP has no native support for Unicode.

A string literal can be specified in three different ways.

Single quoted
Double quoted

PHP Strings : Single quoted

The simple way to print a string is to enclose it in a single quote ( use the character ' ). If you want to print a single quote (') within a string then you must escape it with a backslash like many other languages. If a backslash () needs to print before a single quote or at the end of the string then the backslash must occur twice. At the end of the every string, a html break character has added for line breaking.

Example :

view plaincopy to clipboardprint?

  1. echo 'One line simple string.

  2. echo 'Two line simple  

  3. string example

  4. echo 'Tomorrow I 'll learn PHP global variables.

  5. />';  

  6. echo 'This is a bad command : del c:*.* 

  7. ?>  

Explanation :

The above echo statements display single quoted strings. In the second echo statement, we have written the string in two lines though it will be displayed in a single line. In the third echo statement a single quotation character (') is printed for the backslash character. In the fourth echo statement, a backslash character() has printed after c:, here we have taken two backslash characters together.

View this example in browser

PHP Strings : Double quoted

When we want to print some special characters or the values of variables within a string then we enclose the string with double-quotes(") character.

itemscope itemtype="">Following character sequences beginning with a backslash () are replaced with special character


Replaced by


linefeed (LF)


carriage return (CR)


horizontal tab  (HT)


vertical tab (VT)


form feed (FF)



dollar sign



Example :

  1. echo "Example ::  Linefeed string 

  2. echo "Example ::  Carriage return 

  3. echo "Example ::  Horizontal tab 

  4. echo "Example ::v Vertical tab 

  5. echo "Example ::f Form feed 

  6. echo "Example :: Backslash 

  7. echo "Example ::" Double quote" 

  8. />";  

  9. echo "$ is replaced by the dollar sign itself 

  10. />";  

  11. echo "Single quote within a double quote : I 'll go   

  12. tomorrow.
    " ;  

  13. echo "This is bad command : del c:*.*";  

  14. ?>  

View this example in browser

Variable in double quoted string :

Whenever a $ symbol appears within a string, PHP tries to read the immediate part followed the $ character as a variable name. If the variable type is a string, that string is inserted into the string in that position, if the variable type is a non-string then it is converted into string type.

Example :

view plaincopy to clipboardprint?

  1. $samt=2000;  

  2. $desc="My salary amount for this month is : ";  

  3. echo "$desc $samt 

  4. // We can write the above example in the following way  

  5. echo "My salary amount for this month is : $$samt";  

  6. ?>  

View this example in browser

PHP Strings : heredocs

In addition to the single-quote and double-quote syntaxes, there is an another way to embed large pieces of text in your scripts which may include lots of double and single quotes.



....text here .......

....text here .......

....text here .......


Example :

  1. $mystring=<<

  2. "Tomorrow I 'll not go at your house"  

  3. "may be another day."  

  4. 'Thank you'  

  5. MYID;  

  6. echo $mystring;  

  7. ?>  

View this example in browser