Php Data type conversion caused by a small question

Source: Internet
Author: User
Tags parse error
Question: [php] & lt ;? Php $ a = 3; $ B = 5; $ c = $ a & gt; $ B? 2: 4; echo $ d = $ a & lt; $ c;/* analysiswhytheresultis1 */echo $ d; echogettype ($ d); echovar_dump ($ d);/* The question is as follows: [php] $ 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 here */?> 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 recall the definition and usage of the PHP echo () function in the manual. The echo () function outputs one or more strings. The description of the echo (strings) parameter 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. For more information, see http://www.w3school.com.cn/php/func_string_echo.asp To better understand php Data type conversion and understand the complexity of the computation results, remember the following points: 1. note the differences between echo and print functions, for example: 1) echo "123 ". print ("abc"); // The syntax is correct, but the output is not 123abc, but abc1231. // The reason is that the print () function is first executed to output abc, connect "123" to the return value of the function 1 print "abc ". echo ('ooo'); // echo ('ooo') will cause Parse error: syntax error 2) and echo () functions can use simplified syntax. Example: [php]

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: output strings, such as echo 'ABC', in the most general case '. "123", the result is abc123, not explained. Example 2: echo true; the output result 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 result 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 the output is yes or no? Example 4: try again. Echo 100. "ABC"; // note! There are spaces on both sides of the vertex symbol. the output value 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 write-on statement, but the syntax is incorrect! Why? Because PHP treats the dot as the decimal point here, "100." ABC "is certainly not a correct number. Echo 100. "ABC"; only the right side of the dot symbol has spaces. syntax error. 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. Similarly: $ a = 100; echo $ a. 200; // There is a space on both sides of the dot symbol to output 100200. $ A = 100; echo $ a.200; // There is no space syntax error on both sides of the dot symbol. "$ A.200" is not a qualified variable. Example 5: echo 100 + "ABC"; the output result 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; // if there is a space on both sides of the DOT sign, output 12030. Echo 100 + "20". 30; // There is a space on the left of the dot symbol and no space on the right. The syntax is incorrect .. 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.

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.