Php development BASICS (1): An in-depth discussion of PHP vertex operators (.) and data type conversion

Source: Internet
Author: User
Tags parse error

If it is correct, in the PHP language, the most commonly used operator number is ".", and the most used statement is "Echo ". It is easy to understand that PHP aims to generate hypertext scripts, which are composed of strings. Therefore, PHP processes the most data as strings, therefore, the dot operator that connects strings and the statement "Echo" that outputs strings are frequently used.

The most common is not necessarily the most familiar. When we hit echo "...", when the statement outputs the computation result, do you dare to say that you are familiar with strings and vertices and can use them freely?

For example, the echo '123456' statement and echo 100; the output HTML code is 100. What are their differences?
For another example, the echo 100.200 statement is the same as Echo 100 [space]. [space] 200. Is the output result the same? Why?
......

1. Understand that PHP is a "weak type" Language

Every computer language (nearly 20 languages I know) has data types, and PHP is almost impossible. The language of "weak type" does not mean that the language does not have a data type, but that it weakens or does not emphasize the data type. Specifically, the variable does not need to declare the data type before use, the type is determined by the assigned type, and the variable itself is also defined. However, PHP functions and various operators (operators) require variable and operand types. Therefore, it is inevitable that there is a problem of automatic conversion of data types. For strongly typed languages, such as C, C ++, Java, and C #, different types of data cannot be involved in hybrid operations. If necessary, you must use some methods (such as functions and classes) to convert data types. However, PHP allows mixed operations (similar to VBScript) on different types of data. The PHP interpretation engine automatically performs type conversion.

2. Understand the operations of ECHO.

ECHO is actually not a function, but a language structure, so it can be used as ECHO ('...') or ECHO '...'. The echo operand type should be string type.

Refer to the instructions in the PHP manual for ECHO:

Syntax: ECHO (strings );
The strings parameter is required.
Description: one or more strings to be sent to the output.

Knowledge 1: Pay attention to the differences between ECHO and print functions, such:
Echo "123". Print ("ABC"); // The syntax is correct, but the output is not 123abc, but abc1231
// The cause is that the print () function is executed first to output ABC, and then "123" is connected to the return value 1 of the function.
Print "ABC". Echo ('ooo'); // echo ('ooo') will cause Parse error: syntax error

Knowledge 2: Simplified echo syntax <? = '...'?>

Knowledge 3: ECHO () is a little faster than the print () function.

3. Measure the test taker's knowledge about the operators of vertex operators.

The operand of the vertex operator is the string strings.
Syntax: strings. Strings

4. in-depth understanding of vertex operators (.)

After learning about the first, second, and third sides of the front, can strings, ECHO, and vertex operators be correctly applied? Not all. There is often a distance between familiarity with syntax and practical applications. In addition, automatic data type conversion leads to a certain degree of complexity.

Example 1:
In the most general case, the output string, such as Echo 'abc'. "123", is ABC123. There is nothing to explain.

Example 2:
Echo true;
The output value is 1. True is a PHP built-in Boolean constant, which is converted to a string "1" before output ".

Echo false;
The output result is null. False is also a PHP built-in Boolean constant, which is converted to an empty string before output.

Example 3:
Echo 0 = "ABC ";
The output value is 1. 0 = "ABC", the string "ABC" is first converted to a numeric value (the string is converted to a numeric value, and there is a rule that you can talk about again), 0, and then compared with 0, if the result is true, run echo true, which is 1. Two type conversions were performed silently.

Note that echo 'abc' = 0; the result is still 1. Note that it is not to convert 0 to string "0" and compare it with string 'abc' to get false. Is it weird?

Similarly, there is another question:

If ('abc' = 0)
Echo 'yes ';
Else
Echo 'no ';

Do you know whether yes or no is output?

Example 4: Try again.
Echo 100. "ABC"; // note! The dot symbol has spaces on both sides.
The output is 100abc because 100 is converted to "100.

Echo 100. "ABC"; // note! There is no space on both sides of the vertex symbol. It is a hyphen.
However, the syntax of this report is incorrect! Why? Because PHP treats the dot as the decimal point here, "100." ABC "is certainly not a correct number.

Echo 100. "ABC ";
The syntax is incorrect because only the right side of the dot symbol has spaces. The reason is as follows.
Echo 100. "ABC ";
Only the space on the left of the dot symbol is displayed. The syntax is correct and 100abc is output.

Similar:

$ A = 100;
Echo $ A. 200; // There are spaces on both sides of the dot symbol
100200 output.

$ A = 100;
Echo $ a.200; // there is no space on either side of the dot symbol
Syntax error. "$ A.200" is not a qualified variable.

Example 5:
Echo 100 + "ABC ";
The output is 100. Do not consider this as a syntax error, because the "+" here is an arithmetic operator, not a mistake in the concatenation operator. "ABC" is converted to 0, followed by 100 and equal to 100, and then converted to the string "100" output. Type conversion is performed twice.

Echo 100 + "20". 30; // There are spaces on both sides of the dot symbol
Output 12030 results.

Echo 100 + "20". 30; // there is a space on the left side of the dot symbol and no space on the right side
Syntax error .. 30 is treated as 0.30 of the double type. With the result of 100 + "20" 120 ", PHP cannot tell clearly and reports an error.

You can also give some examples. After careful consideration, we can still find out the type conversion rules. php is not surprising.

Even the old bird of PHP may not be familiar with the above situations. I have seen a lot of phper and think that the path is very wild. In fact, I think it is true to be careful when programming, the knowledge structure is like a spider's network. The mesh is too large to pass through a whale. Therefore, we still need to learn some basic knowledge with an open mind and a calm mind. AD: If you like, come to my Xi'an PHP education and training center for study and discussion.

Author: Zhang Qing (mesh) Xi'an PHP Education and Training Center (QQ group: 108017660)
From mesh horizon: http://blog.why100000.com
Why 100,000 computer learning networks: http://www.why100000.com

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.