PHP String

PHP String

PHP String

A PHP string is a sequence of characters i.e. used to store and manipulate text. There are 4 ways to specify string in PHP.

  • single quoted
  • double quoted
  • heredoc syntax
  • newdoc syntax (since PHP 5.3)

Single Quoted PHP String

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

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

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

<?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
<?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 <br/> $str2 <br/> $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 PHP String

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.

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

<?php  
$str1="Using double "quote" directly inside double quoted string";  
echo $str1;  
?>
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.

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

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