• PHP基础知识(一)


    The Basics

    Comparison operators

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

    <?php
    $a = 5;   // 5 as an integer
    
    var_dump($a == 5);       // compare value; return true
    var_dump($a == '5');     // compare value (ignore type); return true
    var_dump($a === 5);      // compare type/value (integer vs. integer); return true
    var_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 interpreted as the boolean 'false'
        // code...
    }
    
    vs.
    
    if (strpos('testing', 'test') !== false) {    // true, as strict comparison was made (0 !== false)
        // code...
    }
    

    Conditional statements

    If statements

    While using ‘if/else’ statements within a function or class, there is a common misconception that ‘else’ must be used in conjunction to declare potential outcomes. However if the outcome is to define the return value, ‘else’ is not necessary as ‘return’ will end the function, causing ‘else’ to become moot.

    <?php
    function 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 are a great way to avoid typing endless if’s and elseif’s, but there are 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, then the default is used (if defined)
    • Without a ‘break’, they will continue to implement each case until reaching a break/return
    • Within a function, using ‘return’ alleviates the need for ‘break’ as it ends the function
    <?php
    $answer = test(2);    // the code from both 'case 2' and 'case 3' will be implemented
    
    function test($a)
    {
        switch ($a) {
            case 1:
                // code...
                break;             // break is used to end the switch statement
            case 2:
                // code...         // with no break, comparison will continue to 'case 3'
            case 3:
                // code...
                return $result;    // within a function, 'return' will end the function
            default:               
                // code...
                return $error;
        }
    }
    
    如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。return 也会终止 eval() 语句或者脚本文件的执行。
    Note: 注意既然 return 是语言结构而不是函数,因此其参数没有必要用括号将其括起来。通常都不用括号,实际上也应该不用,这样可以降低 PHP 的负担。
    

     

     
  • 相关阅读:
    10.5 实现任务的自动调度
    6.2 常见多媒体标准及压缩技术
    南海区行政审批管理系统接口规范v0.3(规划)4.2.【queryExpireList】当天到期业务查询
    南海区行政审批管理系统接口规范v0.3(规划)4.1.【queryAcceptById】业务明细查询
    域名相关的一些基础知识
    域名相关的一些基础知识
    NTP时间服务器介绍
    NTP时间服务器介绍
    NTP时间服务器介绍
    运维实用操作命令
  • 原文地址:https://www.cnblogs.com/perseverancevictory/p/4246008.html
Copyright © 2020-2023  润新知