Php Data type conversion problem caused by a small question _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
The php Data type conversion problem caused by a small question. Question: [php]? Php $ a3; $ b5; $ c $ a $ B? 2: 4; echo $ d $ a $ c; * analysiswhytheresultis1 * echo $ d; echogettype ($ d); echovar_dump ($ d); * example2 * if (ABC0:

[Php]

$ A = 3;

$ B = 5;

$ C = $ a> $ B? 2: 4;

Echo $ d = $ a <$ c;

/* Analysis why the result is 1 */

Echo $ d;

Echo gettype ($ d );

Echo var_dump ($ d );

/* Example 2 */

If ('ABC' = 0)

Echo 'yes ';

Else

Echo 'no ';

/* Example 3 the result $ h = 10, $ g = 10 ;*/

$ H = + $ g = 10;

Echo $ h, "$ g ";

/* Operator priority */

?>

Are you confused about the results !?

As a weak language, php allows mixed operations (similar to VbScript) on different types of data. The PHP interpretation engine automatically performs type conversion. Attention is required.

Let's first recall the PHP echo () function in the manual.

Definition and usage

The echo () function outputs one or more strings.

Syntax

Echo (strings)

Parameter description

Strings is required. One or more strings to be sent to the output.

Note: Note: echo () is actually not a function, so you do not need to use parentheses for it. However, if you want to pass one or more parameters to echo (), a parsing error occurs when brackets are used. The echo () function can be used as echo ('...') or echo '...'. The echo Operand type should be string type.

See http://www.w3school.com.cn/php/func_string_echo.asp for detailed usage

To better understand the php Data type conversion and understand the complexity of the computing results, remember the following points:

1: pay attention to the differences between echo and print functions, such:

1 ),

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

2) The echo () function can use the simplified syntax.

For example:

[Php]

$ Color = "red ";

?>

Roses are

3) The echo () function is a little faster than the print () function.

Next, you need to familiarize yourself with the operations of vertex operators. here we use the example of "Zhang Qing (mesh:

Example 1:

In the most general case, the output string, such as echo 'ABC'. "123", is abc123, which is not explained.

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.

Why [php]? Php $ a = 3; $ B = 5; $ c = $ a $ B? 2: 4; echo $ d = $ a $ c;/* analysis why the result is 1 */echo $ d; echo gettype ($ d); echo var_dump ($ d ); /* example 2 */if (ABC = 0 )...

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.