The difference between single quotes and double quotes in php: fields inside double quotes will be interpreted by the compiler and then output as HTML code; fields inside single quotes will not be interpreted and will be output directly. Single quotes do not need to consider variable parsing and are faster than double quotes.
In PHP, we can use single quotes or double quotes to represent strings. But we, as developers, should understand the difference.
1. Different parsing of content
""Fields inside double quotes will be interpreted by the compiler and then output as HTML code.
''The fields enclosed in single quotes will not be interpreted and will be output directly. [Related recommendations: PHP Tutorial]
<?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; ?>
2. Different parsing speeds
Single quotes do not need to consider the parsing of variables and are faster than double quotes. It is recommended to use single quotes. Sometimes double quotes are easier to use. For example, when piecing together sql statements
backslash
//使用单引号 echo ' this \n is \r the blog \t of \\ zhoumanhe \\'; //上面使用单引号输出的值是 this \n is \r the blog \t of \ zhoumanhe \ echo ''; echo ""; //使用双引号 echo "this \n is \r the blog \t of \\ zhoumanhe \\"; //上面使用双引号输出的值是 this is the blog of \ zhoumanhe \
use sql
Assume that constants are used in the query conditions, for example:
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 the query conditions, for example:
$user_name = $_REQUEST['user_name']; //字符串变量
or
$user=array (”name”=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//数组变量
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"] . ” ‘ “;
Compare:
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 decomposed into the following 3 parts:
1: "select * from table where user_name = ' " // Fixed SQL statement
2: $user // Variable
3: " ' "
Principles for using quotation marks in PHP
1. Use quotation marks for string values
2. Try to use single quotation marks in PHP, and use double quotation marks for all HTML codes
3. When including variables, use double quotes to simplify the operation
4. In complex situations, use curly brackets
Another use of PHP quotes is that sometimes they need to be generated with PHP In text files, the newline character \n needs to be double quoted, while single quotes will directly output \n as a character.
The above is the detailed content of What is the difference between single quotes and double quotes in php?. For more information, please follow other related articles on the PHP Chinese website!