Difference Analysis of Single Quotation Mark and Double Quotation Mark in PHP

  • 2021-07-10 19:19:48
  • OfStack

① Escaped characters are different

The escape character (\) can be used in both single and double quotation marks, but only the single quotation mark and the escape character itself that are caused in the single quotation mark can be escaped. If you enclose strings in double quotation marks (""), PHP knows more about escape sequences for particular strings.


 <?php
$str1 = ' \ ', \\ , \ r \ n \ t \ v \ $ \ "';
echo $str1,'<br />';
 
$str2 = " \ ", \\ ,a \ r \ n \ tb \ v \ $ \ '";
echo $str2,'<br />';
?>

② The analysis of variables is different
Variables that appear in single quote strings are not replaced by variable values. That is, PHP does not parse variables in single quotation marks, but outputs variable names as they are. The most important point of a double quotation mark string is that the variable name is replaced by the variable value, which means that the variable contained in double quotation marks can be parsed.


 <?php
$age = 20;
$str1 = 'I am $age years old';
$str2 = "I am $age years old";
echo $str1,'<br />'; // I am $age years old 
echo $str2,'<br />'; // I am 20 years old;
?>

③ The resolution speed is different

Single quotation marks do not need to consider the parsing of variables, and are faster than double quotation marks. Single quotation marks are recommended. Sometimes double quotation marks are also easier to use, such as piecing together sql statements

Backslash


 // Use single quotation marks 
echo ' this  \ n is  \ r the blog  \ t of  \\  zhoumanhe  \\ '; 
// The value printed above in single quotation marks is  this  \ n is  \ r the blog  \ t of  \  zhoumanhe  \ 
 
echo '
';
echo "
";
 
// Use double quotation marks 
echo "this  \ n is  \ r the blog  \ t of  \\  zhoumanhe  \\ "; 
// The value printed above in double quotation marks is  this is the blog of  \  zhoumanhe  \  

Using sql

Assume that constants are used in query conditions, such as:


select * from abc_table where user_name='abc';

The SQL statement can be written as:


SQLstr =  " select * from abc_table where user _name=  ' abc' "  ;

Assume that variables are used in query conditions, such as:


$user_name = $_REQUEST['user_name']; // String variable 

Or


$user=array ( " name " => $_REQUEST['user_name ' ,"age"=>$_REQUEST['age'];// Array variable 

The SQL statement can be written as:


SQLstr =  " select * from abc_table where user_name =  '   "  . $user_name .  "   '   " ;
SQLstr =  " select * from abc_table where user_name =  '   "  . $user["name"] .  "   '   " ;

Under comparison 1:


SQLstr= " select * from abc_table where user_name =  '  abc  '   "  ;
SQLstr= " select * from abc_table where user_name ='  "  . $user _name .  "   '   " ;
SQLstr= " select * from abc_table where user_name ='  "  . $user["name"] .  "   '   " ;

SQLstr can be broken down into the following three parts:


1 : " select * from table where user_name =  '   "  // Fixed SQL Statement 
2 : $user // Variable 
3 : "   '   " 

Attachment: You have also seen echo ' < br/ > '; Tags in html are valid in both single and double quotation marks.

Summarize the principles of using quotation marks in PHP under 1

1. The value of a string is quoted

2. Use single quotation marks as much as possible in PHP, and use double quotation marks in all HTML codes

3. Using double quotation marks makes it easier to include variables

STEP 4 Wrap in braces for complicated situations

Another use of PHP quotation marks is that sometimes you need to use php to generate text files, the newline character n needs double quotation marks to work well, and single quotation marks will directly output n as characters.

Use summary: When you don't need to add variables or single quotation marks (') and backslashes (\) to the string, try to quote the string in single quotation marks, because it saves the time of double quotation mark checking to process escape and parse variables. You can use single quotation marks as much as possible.


Related articles: