Home >>PHP Tutorial >PHP String

PHP String

PHP String

A sequence of characters that is generally used to store and manipulate the text is known as PHP String. A set of 256-character is supported by PHP in order to prevent it from offering native Unicode support. In order to specify a string literal in PHP, there are generally 2 ways to do it that are depicted below:

  • Single quoted
  • Double quoted

1. Single Quoted

A string can be created in PHP by the programmer just by enclosing the text in a single-quote. This way is generally known as the easiest way that is used to specify the string in PHP. In order to specify a literal single quote you have to escape it just with a backslash (\) and in order to specify a literal backslash (\) you have to use the double backslash (\\). Any other instances that have a backslash like \r or \n will be their output just the same as they have specified instead of possessing any special meaning to it.

Example:

Here is an example that will let you understand the single quoted PHP text in a depth that will help you in mastering the topic:

<?php  
   $myStr='welcome';  
   echo $myStr;  
?>  
<?php  
   $myStr='welcome text using signle quote';  
   echo $myStr;  
?>  

Note : Many of the escape sequences and the variables that are present in the single quoted PHP strings will generally not be interpreted. But that doesn’t prevent us from using the single quote through \' and backslash through \\ inside the single quoted PHP strings.

2.Double Quoted

A string can be specified just by enclosing the text within the double quote in the PHP language. And the fact is that unlike in the single quote the escape sequences and variables will generally be interpreted just by the use of double quote PHP strings.

Example:

Here is an example of the double quoted in strings that will make you understand the concept very clearly. Observe this example carefully:

<?php  
   $myStr="welcome";  
   echo $myStr;  
?>  
<?php  
   $myStr="welcome text using Double quote";  
   echo $myStr;  
?>  

No Sidebar ads