PHP Basics (i)

Source: Internet
Author: User

The Basicscomparison operators

Comparison operators is an often overlooked aspect of PHP, which can leads to many unexpected outcomes. One such problem stems from strict comparisons (the comparison of booleans as integers).

<?php$a = 5;   5 as an integervar_dump ($a = = 5);       Compare value; return Truevar_dump ($a = = ' 5 ');     Compare value (ignore type); return Truevar_dump ($a = = = 5);      Compare Type/value (integer vs. integer); return Truevar_dump ($a = = = ' 5 ');    Compare Type/value (integer vs. string); Return false/** * Strict comparisons */if (strpos (' testing ', ' test ')) {    //' test ' is found at position 0, which is int Erpreted as the Boolean ' false '    //code ...} Vs.if (Strpos (' testing ', ' Test ')!== false) {    //true, as strict comparison was made (0!== false)    //code ...}
Conditional statementsif Statements

While using ' if/else ' statements within a function or class, there are a common misconception that ' else ' must being used in C Onjunction to declare potential outcomes. However if the outcome is to define the return value, ' else ' was not necessary as ' return ' would end the function, causing ' Else ' to become moot.

<?phpfunction Test ($a) {    if ($a) {        return true;    } else {        return false;}    } Vs.function Test ($a) {    if ($a) {        return true;    }    return false;    else is not necessary}

  

Switch statements

Switch statements is a great to avoid typing endless if's and ElseIf ' s, but there is a few things to be aware of:

    • Switch Statements only compare values, and not the type (equivalent to ' = = ')
    • They iterate case by case Until a match is found. If no match is found and then the default was used (if defined)
    • without a ' break ', they would continue to implement E Ach case until reaching a break/return
    • within a function, using ' return ' alleviates the need for ' break ' as it E NDS the function
<?php$answer = Test (2);    The code from both ' Case 2 ' and ' case 3 ' would be implementedfunction test ($a) {    switch ($a) {case        1:            //COD E ...            break;             Break was used to end of the switch statement case        2:            //code ...         With no break, comparison'll continue to ' case 3 ' case        3:            //code ...            return $result;    Within a function, ' return ' would end the function        default:                           //code ...            return $error;    }} If a return statement is called in a function, the execution of the function is terminated immediately and its arguments are returned as the value of the function. Return terminates the Eval () statement or the execution of the script file. Note: Since return is a language structure and not a function, it is not necessary to enclose the argument in parentheses. Usually without parentheses, you should actually not use it, which can reduce the burden on PHP.

 

 

PHP Basics (i)

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.