首頁 >後端開發 >php教程 >語法:IF判斷的實現

語法:IF判斷的實現

WBOY
WBOY原創
2016-07-29 08:55:341581瀏覽

面試PHP時經常碰到一種判斷各種類型的空值是否為true的題:

<code>$a = <span>''</span>;
$a = null
$a = false;

<span>if</span>($a){<span>...</span>}
<span>if</span>(isset($a)){<span>...</span>}
<span>if</span>(empty($a)){<span>...</span>}
<span>...</span></code>

由下面的例子我們來簡單看下zend引擎中對if是怎麼處理的:

<code><span><span><?php </span><span>$a</span> = <span>''</span>; <span>//array();</span><span>if</span>(<span>$a</span>){
    <span>echo</span><span>"Y"</span>;
}</span></span></code>

這裡例子比較簡單,結果將什麼也不輸出。 (文中涉及程式碼皆為php-7.0.4版本)

之前的文章介紹過zend執行階段的入口zend_execute函數,我們直接從這裡開始,不熟悉的可以翻一下前面的文章。
編譯產生的opcodes如下:
語法:IF判斷的實現
其中opcode=38是$a = 」的執行操作,opcode=43是if的操作,下面具體看這一步是如何執行的。
根據opcode及兩個操作數類型可以找到對應的handler為:ZEND_JMPZ_SPEC_CV_HANDLER

<code><span>//zend_vm_execute.h #28307</span><span>static</span> ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_JMPZ_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{   
    USE_OPLINE

    zval *<span>val</span>;

    <span>val</span> = _get_zval_ptr_cv_undef(execute_data, opline->op1.var);

    <span>if</span> (Z_TYPE_INFO_P(<span>val</span>) == IS_TRUE) { 
        ZEND_VM_SET_NEXT_OPCODE(opline + <span>1</span>);
        ZEND_VM_CONTINUE();
    } <span>else</span><span>if</span> (EXPECTED(Z_TYPE_INFO_P(<span>val</span>) if (IS_CV == IS_CV && UNEXPECTED(Z_TYPE_INFO_P(<span>val</span>) == IS_UNDEF)) {
            SAVE_OPLINE();
            GET_OP1_UNDEF_CV(<span>val</span>, BP_VAR_R);
            ZEND_VM_JMP(OP_JMP_ADDR(opline, opline->op2));
        } <span>else</span> {
            ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline, opline->op2));
            ZEND_VM_CONTINUE();
        }
    }

    SAVE_OPLINE();
    <span>if</span> (i_zend_is_true(<span>val</span>)) {
        opline++;
    } <span>else</span> {
        opline = OP_JMP_ADDR(opline, opline->op2);
    }

    <span>if</span> (UNEXPECTED(EG(<span>exception</span>) != NULL)) {
        HANDLE_EXCEPTION();
    }
    ZEND_VM_JMP(opline);
}</code>

從這個函數可以看出if的執行過程:如果條件為true的話則opline++,順序執行下一條opcode(即if內語句),否則進行跳轉,跳過if內語句直接執行if外語句。

i_zend_is_true這個函數就是用來判斷各種類型的值是否為真,前面那部分是判斷是否為bool型,是的話則直接處理。

<code><span>//</span>zend_operators.h <span>#283</span>
static zend_always_inline int i_zend_is_true(zval *op)
{   
    int result = <span>0</span>;

<span>again</span>:
    <span>switch</span> (Z_TYPE_P(op)) {
        <span>case</span><span>IS_TRUE</span>:
            result = <span>1</span>;
            <span>break</span>;
        <span>//</span>数值类型long、double直接判断即可,与c用法相同
        <span>case</span><span>IS_LONG</span>:
            <span>if</span> (Z_LVAL_P(op)) {
                result = <span>1</span>;
            }
            <span>break</span>;
        <span>case</span><span>IS_DOUBLE</span>:
            <span>if</span> (Z_DVAL_P(op)) {
                result = <span>1</span>;
            }
            <span>break</span>;
        <span>//</span>字符串类型根据长度判断:长度><span>1</span>,或=<span>1</span>且不为<span>'0'</span>为<span>true</span>,所以上面那个例子<span>''</span><span> =></span><span>false</span><span>case</span><span>IS_STRING</span>:
            <span>if</span> (Z_STRLEN_P(op) > <span>1</span> || (Z_STRLEN_P(op) && Z_STRVAL_P(op)[<span>0</span>] != <span>'0'</span>)) {
                result = <span>1</span>;
            }
            <span>break</span>;
        <span>//</span>数组类型根据数组元素的个数判断:大于<span>0</span>即为真
        <span>case</span><span>IS_ARRAY</span>:
            <span>if</span><span><span>(zend_hash_num_elements(Z_ARRVAL_P(op)))</span> { // <span>(Z_ARRVAL_P(op))</span>-></span>nNumOfElements
                result = <span>1</span>;
            }
            <span>break</span>;
        <span>case</span><span>IS_OBJECT</span>:
            result = zend_object_is_true(op);
            <span>break</span>;
        <span>//</span>资源类型实际就是整形(后续会专门介绍资源类型),所以直接判断即可
        <span>case</span><span>IS_RESOURCE</span>:
            <span>if</span> (EXPECTED(Z_RES_HANDLE_P(op))) {
                result = <span>1</span>;
            }
            <span>break</span>;
        <span>//</span>引用类型则根据指向的值判断
        <span>case</span><span>IS_REFERENCE</span>:
            op = Z_REFVAL_P(op);
            goto again;
            <span>break</span>;
        <span>default</span>:
            <span>break</span>;
    }
    <span>return</span> result;
}</code>

isset、empty函數後續補充…

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了文法:IF判斷的實現,包括了方面的內容,希望對PHP教程有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn