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

coldplay.xixi
Release: 2023-03-06 07:58:02
Original
4135 people have browsed it

phpThe difference between single quotes and double quotes: 1. The escape character [\] can be used in both single quotes and double quotes, but only single quotes and escapes enclosed in single quotes can be escaped. The escape character itself; 2. Variables appearing in single-quoted strings will not be replaced by variable values, while double-quoted variable names will be replaced by variable values.

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

The difference between single quotes and double quotes:

1. The escaped characters are different

The escape character (\) can be used in both single quotes and double quotes, but only single quotes and escape characters enclosed in single quotes can be escaped. If you enclose a string in double quotes (""), PHP knows more about special string escape sequences.

'; $str2 = "\",\\,a\r\n\tb\v\$\'"; echo $str2,'
'; ?>
Copy after login

2. Different parsing of variables

Variables appearing in single quote strings will not be replaced by variable values. That is, PHP will not parse variables in single quotes, but will output the variable name as is. The most important thing about double-quoted strings is that the variable names in them will be replaced by variable values, that is, variables contained in double quotes can be parsed.

'; // I am $age years old echo $str2,'
'; // I am 20 years old; ?>
Copy after login

3. 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 also It is easier to use, such as 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 \
Copy after login

using sql

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

select * from abc_table where user_name='abc';
Copy after login

SQL statement can be written as:

SQLstr = “select * from abc_table where user _name= ‘abc'” ;
Copy after login

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

$user_name = $_REQUEST['user_name']; //字符串变量
Copy after login

or

$user=array (”name”=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//数组变量
Copy after login

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"] . ” ‘ “;
Copy after login

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"] . ” ‘ “;
Copy after login

SQLstr can be decomposed into the following three parts:

1:”select * from table where user_name = ‘ ” //固定SQL语句 2:$user //变量 3:” ‘ ”
Copy after login

Attachment: You have also seen that the tags in echo '
'; html are in single Valid within both quotes and double quotes.

Summarize the principles of using quotation marks in PHP

1. Use quotation marks for string values

2. Try to use single quotation marks in PHP, and all HTML codes Use double quotes

3. When including variables, use double quotes to simplify the operation

4. In complex situations, use curly braces

PHP quotes and One use is that sometimes you need to use PHP to generate a text file. The newline character n needs to be double quoted. Single quotes will directly output n as a character.

Usage summary: When there is no need to add variables or single quotes (') and backslashes (\) to the string, try to use single quotes to quote the string, because double quote checking and escaping are omitted. and the time above the parsed variable. Use single quotes whenever possible.

If you want to learn more about programming, please pay attention to thephp trainingcolumn!

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!