PHP data type conversion problem from a small topic _php tutorial

Source: Internet
Author: User
The title 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"; /* This is about operator precedence */?> doubts about the result!? PHP, as a weakly typed language, is a way to allow mixed operations of different types of data (like VbScript), and the PHP interpretation engine automatically does the type conversion work. Attention must be drawn. Let's recall the PHP echo () function definition in the manual and use the Echo () function to output one or more strings. Syntax Echo (strings) parameter Description strings Necessary. One or more strings to send to the output. Note: note: Echo () is not actually a function, so you do not need to use parentheses on it. However, if you want to pass one or more parameters to echo (), a parse error occurs when you use parentheses. The Echo () function can be used as echo (' ... ') or as echo ' ... '. The operand type of echo should be string-type. For detailed usage see http://www.w3school.com.cn/php/func_string_echo.asp for a better understanding of PHP data type conversions and figuring out the complexities of computational results, keep the following in mind: 1: Pay attention to echo and print The difference between functions, such as: 1), echo "123". Print ("abc"); The syntax is correct, but the output is not 123abc, but the abc1231//reason is to first execute the print () function output ABC, then connect "123" and return value of the function 1 print "ABC". Echo (' ooo '); Echo (' ooo ') causes the Parse error:syntax error 2), and the Echo () function can use the simplified syntax. Example: [PHP]

Roses is

3), the Echo () function is a little bit faster than the print () function. Next, you need to familiarize yourself with the operation of the dot operator, which is the example of a Netizen "Zhang Qing": Example 1: The most general case, the output string, such as Echo ' abc '. "123", the result is abc123, did not explain the good explanation. Example 2:echo true; The output result is 1. True is a PHP built-in Boolean constant that is converted to the string "1" before the output. echo false; The output is empty. False is also a PHP built-in Boolean constant that 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 type (string converted to numeric value, there is a rule, there is a chance to speak again), 0, and then 0, the result is true, and then echo True, which is 1. 2 types of conversions were performed silently. Notice again, echo ' ABC ' ==0; The result is still 1. Note that the 0 is not converted to the string "0" and then the string ' ABC ' is compared to get false. Oh, is it weird? Similarly, there is another topic: if (' ABC ' ==0) echo ' yes '; else echo ' no '; Do you know if the output is yes or no? Example 4: A more bizarre one. Echo 100. "ABC"; Attention! There are spaces on both sides of the dot symbol output 100ABC, because 100 is converted to "100". echo 100. " ABC "; Attention! There are no spaces on either side of the dot symbol, it is ligatures but the syntax error is reported! Why? Because PHP treats the dot symbol as a decimal point here, "100." ABC "" is certainly not a correct number. Echo 100. "ABC"; There is only a space on the right of the dot symbol, syntax error. The reason is as above. echo 100. " ABC "; Only the dot symbol to the left has a space, the syntax is correct, output 100ABC. Similar: $a = 100; echo $a. 200; The dot symbol has a space on both sides to output 100200. $a = 100; echo $a. 200; There is no space syntax error on either side of the dot symbol. "$a. 200" is not a qualified variable. Example 5:echo + "ABC"; The output result is 100. Be careful not to think that this will produce a syntax error because the "+" number here is the arithmetic operator, not the error of the join operator. "ABC" is converted to the number 0, plus 100 equals 100, then turnTo the string "100" output. 2 type conversions have been made. Echo 100 + "20". 30; The dot symbol has a space on both sides of the result output 12030. Echo 100 + "20". 30; Dot symbol left blank, right no space syntax error: 30 is treated as a double type of 0.30. With 100 + "20" result "120" directly together, PHP resolution unclear, error. You can also cite some examples. Carefully understand, or can find the type conversion law, PHP is not strange.

http://www.bkjia.com/PHPjc/477788.html www.bkjia.com true http://www.bkjia.com/PHPjc/477788.html techarticle The topics are as follows: [PHP]? php $a = 3; $b = 5; $c = $a $b? 2:4; echo $d = $a $c;/*analysis Why the result is 1*/echo $d; Echo Gett Ype ($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.