Introduction to the correct use of PHP quotation marks_PHP tutorial

WBOY
Release: 2016-07-15 13:30:03
Original
1045 people have browsed it

For novices who don’t have much coding experience, 1. First think about the categories of all the 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, so the benefits of constants It is a global, penetrating function. It is also faster, but novices can ignore constants for the time being.

3. Variables. Those with a "$" sign in front are variables. You can set a "value" for the variable, such as a A string character, 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. Assignment statement $a='abc ', the 'abc' on the right is the value.

5. The parameters of the function (in brackets). They can be constants, variables, and values.

Variables (constants) and values The relationship is as follows:

"color" and "red",

"length" and 100,

"date" and October 25, 2007"

2. When to use PHP quotation marks

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 strings (date values) can be regarded as characters String) content requires the use of quotation marks. Numbers (can be used or not), true or false (cannot be used) exceptions.

Example

3. The difference between single quotation marks and double quotation marks

Generally, the two are common. However, double quotes for 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 (such as variables) inside, it is better to use double quotes.

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

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

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

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

Similarly, you can also escape the semicolon, $ symbol and other special symbols.

5. String concatenation.

This is a troublesome problem. Generally speaking, variable values ​​are directly included in double quotes That's it. In addition, the "." character is used to superimpose strings.

can be included in curly brackets in complex situations. PHP will know that this is a complete thing, and the quotation marks inside will not affect the quotation marks outside. Relationship. 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 easy to copy large sections of HTML code. Just add single quotes at the beginning and end. Quotation marks are a correct string. You don’t have to worry about confusing PHP quotation marks for hundreds of lines of HTML code.

Summarize the principles of using PHP quotation marks

1. The value of a string is Quotes

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

3. When including variables, use double quotes to simplify operations

4. In complex situations, use curly brackets

Another use of PHP quotes is that sometimes you need to use PHP to generate text files. The newline character n needs to be double quotes to work, while single quotes will directly treat n as a character. output.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446320.htmlTechArticleFor novices who don’t have much coding experience, 1. First think about all the words in PHP (actually it should be called There are several types of symbols. 1. Keywords and functions of PHP and mysql. For example, echo, print, mysq...
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