Introduction to the correct use of PHP quotes

Source: Internet
Author: User
Tags date constant functions include variables variable

For beginners who don't have much coding experience, the correct use of PHP quotes is a headache, often resulting in errors in the program due to incorrect use of PHP quotes. Now let's give you a concrete introduction to the correct use of PHP quotes.

first think about all the words in PHP (in fact, should be called symbols) there are several categories.

1.php,mysql Two-party keywords and functions. such as Echo,print,mysql_connect and so on. These are definitely not quotes.

2. Constants. Beginners may not use much, the benefits of constants are global, penetrating functions. Faster, but beginners can ignore the constant stuff for a while.

3. Variables. The "$" sign is the variable. You can set a value for a variable, such as a string of characters, a number, a logical (TRUE/False) value, and so on. Can also represent a set of values (arrays, objects, etc.)

4. Value. You usually set values for constants and variables. Assignment Statement $a=\ ' Abc\ ', the right \ ' abc\ ' is the value.

5. The parameters of the function (in parentheses). Can be constants, variables, values of three.

The relationship between a variable (constant) and a value is as follows.

"Color" and "red",

"Length" with 100,

"Date" and "October 25, 2007"

two. What is the case with PHP quotes

In fact, only the 4th "value" needs to use quotation marks, the function is only the value in quotation marks. and only strings (date values can be used as strings) need to be enclosed in quotes. Numbers (available for use), and true (unusable) exceptions.

Example

three. The difference between single and double quotes

In general, the two are generic. However, the internal variables of double quotes are parsed, and single quotes are not resolved.

Example

So if there's only a pure string inside, use a single quote (speed), and there's something else inside (like a variable), and it's better to use a few.

Four. What to do if there are PHP quotes inside the string-about escaping.

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

That's when you have to use the escape. Escaping is the conversion of an otherwise useful symbol into meaningless characters.

This is normal, because the number turns any character behind it into meaningless symbols. In this case, the PHP parser does not look at the quotes behind the number as quotes.

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

Five. Connection of strings .

This is a troublesome question. In general, the value of the variable is included directly in the double quotes. Another string is superimposed with "." Character.

In complex situations, you can include curly braces, and PHP knows it's a complete thing, and the quotes inside don't affect the outside quotation marks.

Mixing with HTML is also very simple, it's best to get into the habit of using single quotes in all HTML quotes in PHP. This makes it easy to copy large sections of HTML code, as long as the tail plus the single quotation mark is a correct string. Hundreds of lines of HTML code don't worry about PHP quotes.

Summarize the use of the PHP quotes principle

1. The value of the string is enclosed in quotation marks

In 2.PHP, try to use single quotes, all HTML code with double quotes

3. When you include a variable, you can use double quotes to simplify the operation

4. In complex cases, wrap them in curly braces.

Another use of PHP quotes is that sometimes you need to use PHP to generate text files, newline characters need to use double quotes to work, single quotes will be directly as the character output.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.