The difference between single and double quotes in php_PHP tutorial

WBOY
Release: 2016-07-13 10:27:53
Original
681 people have browsed it

” ” Fields enclosed in double quotes will be interpreted by the compiler and then output as HTML code.

  ‘ ‘ The words in the single quotes are not interpreted and are output directly.

It can be seen from the literal meaning that single quotes are faster than double quotes.

For example:

$abc=’my name is tome’;

echo $abc //The result is: my name is tom

echo ‘$abc’ //The result is

abc

echo “$abc” //The result is: my name is tom

Especially when using MYSQL statements, the usage of double quotes and single quotes can be confusing to novices. Here, I will give an example to illustrate.

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']; //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"] . ” ‘ “;

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:” ‘ ”

Use "." to connect the strings 1, 2, and 3

====

1. First think about how many types of words (actually they should be called symbols) in PHP.

1. Keywords and functions of PHP and mysql. For example, echo, print, mysql_connect, etc. These must be without quotation marks.

2. Constants. Novices may not use them much. The advantage of constants is that they are global and penetrate functions. They are also faster, but novices can ignore constants for the time being.

3. Variables. Those with "$" in front are variables. You can set a "value" for the variable, such as a string of characters, a number, a logical (true/false) value, etc. It can also represent a set of values ​​( Array, object, etc.)

4. Value. Usually you need to set values ​​for constants and variables. In the assignment statement $a='abc', the 'abc' on the right is the value.

5. Function parameters (in brackets). They can be constants, variables, and values.

The relationship between variables (constants) and values ​​is as follows.

"Color" and "red",

"Length" and 100,

"Date" and October 25, 2007 "

2. When to use PHP quotes

In fact, only the fourth item "value" needs to use quotation marks, and only the value in the function needs to use quotation marks. And only the string (date value can be regarded as a string) content needs to use quotation marks. Numbers (available or not) , true or false (cannot be used) exception.

Example

3. The difference between single quotes and double quotes

Under normal circumstances, the two are common. However, double quotes internal variables will be parsed, but single quotes will not be parsed.

Example

So if there is only a pure string inside, use single quotes (faster). When there are other things inside (such as variables), it is better to use double quotes.

IV. What to do if PHP quotes appear inside the string - about escaping.

For example, we want to output: I "am a genius

At this time, escaping must be used. Escape is to convert the originally useful symbols into meaningless characters.

This is normal, because the number converts any characters following it into meaningless symbols. In this case, the PHP parser does not treat the quotation marks after the number as quotation marks at all.

Similarly, you can also escape special symbols such as semicolons and $ symbols.

5. String concatenation.

This is a troublesome problem. Generally speaking, variable values ​​can be directly included in double quotes. In addition, the "." character is used to superimpose strings.

In complex situations, you can use braces to include it. PHP will know that this is a complete thing, and the quotation marks inside will not affect the relationship between the quotation marks outside.

Mixing with HTML is also very simple. It is best to develop the habit of using double quotes in HTML and single quotes in PHP. This makes it easier to copy large sections of HTML code, just add single quotes at the beginning and end. A correct string. Even with hundreds of lines of HTML code, you don’t have to worry about confusing PHP quotes.

Summarize the principles of using quotes in PHP

1. Use quotation marks for string values ​​

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

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

4. In complex cases, use braces

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815012.htmlTechArticleFields in double quotes will be interpreted by the compiler and then output as HTML code. The words inside the single quotes are not interpreted and are output directly. As you can see from the literal meaning, single quotes...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template