String in PHP

String is a series of characters, where a character is like a byte. This means PHP supports only 256-character sets, and therefore does not provide original Unicode support. See string type details.

Types of String
  • single quoted
  • double quoted
  • heredoc syntax
  • newdoc syntax

Single Quoted

The easiest way to specify a string is to attach it to a single quote (character).

To specify a literal single bid, avoid this backslash (\). To specify a literal backslash, double it (\\). All other examples of backslash will be considered as a literal backslash: This means that other escape sequences that you can use, such as \ r or \ n, will have output in specified form instead of having a special meaning.

Example - 

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


<?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";  
?>  

Double Quoted

We can attach a text to a single quote and make a string in PHP. This is the easiest way to specify the string in PHP

Syntax -

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

Example - 

<?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";  
?>  




Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment