PHP Process Control statements and data type conversions in conditional judgments

Source: Internet
Author: User
Tags switches type null
PHP Process Control statements and data type conversions in conditional judgments

PHP's Process Control statements include loop body statements and branch judgment statements.

The loop operation consists of three sets of statements, namely while, Do-while, and for loops.

The branch judgment body consists of two groups: if Else statement, switch statement.

Here are a few topics to understand PHP Process Control statements and data type conversions in conditional judgments.

***********************************************

The data type conversion involved in IF-ELSE statement and conditional judgment

***********************************************

 
  "; if (" 0 ") echo" true "; Elseecho" False-2 "."
"; if ($i) echo" true "; Elseecho" false-3 "."
"; if (" false ") echo" true "; Elseecho" false-4 "."
"; if (false) echo" true "; Elseecho" false-5 "."
"; if (0.0) echo" true "; Elseecho" false-6 "."
"; if (PHP) echo" true "; Elseecho" false-7 "."
"; if (") echo "true"; Elseecho "False-8". "
"; if (null) echo" true "; Elseecho" false-9 "."
"; if (array) echo" true "; Elseecho" false-10 "."
";" if (Array ()) echo "true"; Elseecho "false-11". "
";/* Output: False-1false-2false-3true false-5false-6true false-8false-9true FALSE-11 Analysis: If statement returns false when the condition is not satisfied (that is, the conditional expression evaluates to false). PHP If statement does not have then, there is elseif (same else if) when converted to Boolean, the following values are considered False:1) boolean value false;2) integer value 0 (0); 3) floating-point value 0.0 (0); 4) blank string and string "0" ; 5) No array of member variables, 6) object with no cell (only for PHP4), 7) special type null (including variable not yet set);*/?>

***********************************************
Topic two, Switch-case-default statement and break statement relationship

***********************************************

 
  "Case 6:echo"?? "; Case ' 8 ': echo "| | |"; Case 10:echo "10". "
break;//If this break is removed, the @ @default of the default branch will be output: echo "@@"; /* Run Results for: six??| | | 10 Analysis: Note that in the above Switch-case-default statement, there is no break statement in some places. The ①default is always last executed in all case switches. ② Once the switch condition is determined to enter a switch (including default), if there is no break jump statement in the switches, then the statement after the switch is executed as a normal program statement until a break is encountered (for example, if the condition satisfies the case 2 switch, While the case 2 switch has no break statement, the statement after Case 2 is executed as a normal program statement and case judgment is no longer valid. )。 If there is a break statement inside the switch, then you jump out of switch. ③ The case statement for the current statement is empty, the controller automatically transfers to the next cases statement. This shows that, when using switch, if there is no break in case, this program is quite dangerous, and the latent loophole can be difficult to find even experienced testers. Refer to the explanation of W3school: Working principle: 1, the expression (usually a variable) to do a calculation 2, the value of the expression and the value of the case in the structure of the comparison 3, if there is a match, then execute the code associated with the box 4, code execution, The break statement prevents the code from jumping into the next case to continue execution 5, and if there is not true, use the default statement */?>

***********************************************

Topic three, the problem of automatic conversion of Switch-case-default statement and data type

***********************************************

 
  

***********************************************

Topic Iv. Error-prone errors in programming-differences between comparison operators and assignment operators

***********************************************

 
  

With the above four examples, we have a better understanding of the rules of PHP language in use, especially the PHP Process Control statements and the data type conversion in conditional judgment, under the premise of PHP basic grammar. The mastery of these knowledge is beneficial to develop the good code writing habit, evade some realistic traps and improve the development efficiency.

  • 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.