Home  >  Article  >  Backend Development  >  What is the difference between single quotes and double quotes in php?

What is the difference between single quotes and double quotes in php?

青灯夜游
青灯夜游Original
2020-07-16 15:05:593634browse

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.

What is the difference between single quotes and double quotes in php?

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 = &#39;I am $age years old&#39;;
$str2 = "I am $age years old";
echo $str1,&#39;<br />&#39;; // I am $age years old 
echo $str2,&#39;<br />&#39;; // 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 &#39; this \n is \r the blog \t of \\ zhoumanhe \\&#39;; 
//上面使用单引号输出的值是 this \n is \r the blog \t of \ zhoumanhe \  
echo &#39;&#39;;
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=&#39;abc&#39;;

The SQL statement can be written as:

SQLstr = “select * from abc_table where user _name= ‘abc&#39;” ;

Assume that variables are used in the query conditions, for example:

$user_name = $_REQUEST[&#39;user_name&#39;]; //字符串变量

or

$user=array (”name”=> $_REQUEST[&#39;user_name‘,"age"=>$_REQUEST[&#39;age&#39;];//数组变量

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 =&#39; ” . $user _name . ” ‘ “;
SQLstr=”select * from abc_table where user_name =&#39; ” . $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn