国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
深入理解PHP中的empty和isset函數(shù),emptyisset
Home Backend Development PHP Tutorial In-depth understanding of empty and isset functions in PHP, emptyisset_PHP tutorial

In-depth understanding of empty and isset functions in PHP, emptyisset_PHP tutorial

Jul 12, 2016 am 08:50 AM
empty isset php

深入理解PHP中的empty和isset函數(shù),emptyisset

近日被問(wèn)到PHP中empty和isset函數(shù)時(shí)怎么判斷變量的,剛開(kāi)始我是一臉懵逼的,因?yàn)槲易约阂仓皇且恢虢猓瑸榱伺湔嬲脑?,趕緊翻開(kāi)源碼研究研究。經(jīng)過(guò)分析可發(fā)現(xiàn)兩個(gè)函數(shù)調(diào)用的都是同一個(gè)函數(shù),因此本文將對(duì)兩個(gè)函數(shù)一起分析。

我在github有對(duì)PHP源碼更詳細(xì)的注解。感興趣的可以圍觀一下,給個(gè)star。PHP5.4源碼注解??梢酝ㄟ^(guò)commit記錄查看已添加的注解。

函數(shù)使用格式

empty

bool empty ( mixed $var )

判斷變量是否為空。

isset

bool isset ( mixed $var [ , mixed $... ] )

判斷變量是否被設(shè)置且不為NULL。

參數(shù)說(shuō)明

對(duì)于empty,在PHP5.5版本以前,empty只支持變量參數(shù),其他類型的參數(shù)會(huì)導(dǎo)致解析錯(cuò)誤,比如函數(shù)調(diào)用的結(jié)果不能作為參數(shù)。

對(duì)于isset,如果變量被如unset的函數(shù)設(shè)為NULL,則函數(shù)會(huì)返回false。如果多個(gè)參數(shù)被傳遞到isset函數(shù),那么只有所有參數(shù)都被設(shè)置isset函數(shù)才會(huì)返回true。從左到右計(jì)算,一旦遇到?jīng)]被設(shè)置的變量就停止。

運(yùn)行示例

$result = empty(0); // true
$result = empty(null); // true
$result = empty(false); // true
$result = empty(array()); // true
$result = empty('0'); // true
$result = empty(1); // false
$result = empty(callback function); // 報(bào)錯(cuò)

$a = null;
$result = isset($a); // false;

$a = 1;
$result = isset($a); // true;

$a = 1;$b = 2;$c = 3;
$result = isset($a, $b, $c); // true

$a = 1;$b = null;$c = 3;
$result = isset($a, $b, $c); // false

找到函數(shù)的定義位置

實(shí)際上,empty不是一個(gè)函數(shù),而是一個(gè)語(yǔ)言結(jié)構(gòu)。語(yǔ)言結(jié)構(gòu)是在PHP程序運(yùn)行前編譯好的,因此不能像之前那樣簡(jiǎn)單地搜索"PHP_FUNCTION empty"或"ZEND_FUNCTION empty"查看其源碼。要想看empty等語(yǔ)言結(jié)構(gòu)的源碼,先要理解PHP代碼執(zhí)行的機(jī)制。

PHP執(zhí)行代碼會(huì)經(jīng)過(guò)4個(gè)步驟,其流程圖如下所示:


在第一個(gè)階段,即Scanning階段,程序會(huì)掃描zend_language_scanner.l文件將代碼文件轉(zhuǎn)換成語(yǔ)言片段。對(duì)于isset和empty函數(shù)來(lái)說(shuō),在zend_language_scanner.l文件中搜索empty和isset可以得到函數(shù)在此文件中的宏定義如下:

<ST_IN_SCRIPTING>"isset" {
return T_ISSET;
}


<ST_IN_SCRIPTING>"empty" {
return T_EMPTY;
}

接下來(lái)就到了Parsing階段,這個(gè)階段,程序?qū)_ISSET和T_EMPTY等Tokens轉(zhuǎn)換成有意義的表達(dá)式,此時(shí)會(huì)做語(yǔ)法分析,Tokens的yacc保存在zend_language_parser.y文件中,可以找到T_ISSET和T_EMPTY的定義

internal_functions_in_yacc:
T_ISSET '(' isset_variables ')' { $$ = $3; }
| T_EMPTY '(' variable ')' { zend_do_isset_or_isempty(ZEND_ISEMPTY, &$$, &$3 TSRMLS_CC); }
| T_INCLUDE expr { zend_do_include_or_eval(ZEND_INCLUDE, &$$, &$2 TSRMLS_CC); }
| T_INCLUDE_ONCE expr { zend_do_include_or_eval(ZEND_INCLUDE_ONCE, &$$, &$2 TSRMLS_CC); }
| T_EVAL '(' expr ')' { zend_do_include_or_eval(ZEND_EVAL, &$$, &$3 TSRMLS_CC); }
| T_REQUIRE expr { zend_do_include_or_eval(ZEND_REQUIRE, &$$, &$2 TSRMLS_CC); }
| T_REQUIRE_ONCE expr { zend_do_include_or_eval(ZEND_REQUIRE_ONCE, &$$, &$2 TSRMLS_CC); }
;

isset和empty函數(shù)最終都執(zhí)行了zend_do_isset_or_isempty函數(shù),繼續(xù)查找

grep -rn "zend_do_isset_or_isempty"

可以發(fā)現(xiàn),此函數(shù)在zend_compile.c文件中定義。

函數(shù)執(zhí)行步驟

1、解析參數(shù)

2、檢查是否為可寫變量

3、如果是變量的op_type是IS_CV(編譯時(shí)期的變量),則設(shè)置其opcode為ZEND_ISSET_ISEMPTY_VAR;否則從active_op_array中獲取下一個(gè)op值,根據(jù)其op值設(shè)置last_op的opcode。

4、設(shè)置了opcode之后,之后會(huì)交給zend_excute執(zhí)行。

源碼解讀

IS_CV是編譯器使用的一種cache機(jī)制,這種變量保存著它被引用的變量的地址,當(dāng)一個(gè)變量第一次被引用的時(shí)候,就會(huì)被CV起來(lái),以后這個(gè)變量的引用就不需要再去查找active符號(hào)表了。

對(duì)于empty函數(shù),到了opcode的步驟后,參閱opcode處理函數(shù),可以知道,isset和empty在excute的時(shí)候執(zhí)行的是ZEND_ISSET_ISEMPTY_VAR等一系列函數(shù),以ZEND_ISSET_ISEMPTY_VAR_SPEC_CV_VAR_HANDLER為例,找到這個(gè)函數(shù)的定義在zend_vm_execute.h。查看函數(shù)可以知道,empty函數(shù)的最終執(zhí)行函數(shù)是i_zend_is_true(),而i_zend_is_true函數(shù)定義在zend_execute.h。i_zend_is_true函數(shù)的核心代碼如下:

switch (Z_TYPE_P(op)) {
    case IS_NULL:
      result = 0;
      break;
    case IS_LONG:
    case IS_BOOL:
    case IS_RESOURCE:
      // empty參數(shù)為整數(shù)時(shí)非0的話就為false
      result = (Z_LVAL_P(op)&#63;1:0);
      break;
    case IS_DOUBLE:
      result = (Z_DVAL_P(op) &#63; 1 : 0);
      break;
    case IS_STRING:
      if (Z_STRLEN_P(op) == 0
        || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
        // empty("0") == true
        result = 0;
      } else {
        result = 1;
      }
      break;
    case IS_ARRAY:
      // empty(array) 是根據(jù)數(shù)組的數(shù)量來(lái)判斷
      result = (zend_hash_num_elements(Z_ARRVAL_P(op))&#63;1:0);
      break;
    case IS_OBJECT:
      if(IS_ZEND_STD_OBJECT(*op)) {
        TSRMLS_FETCH();

        if (Z_OBJ_HT_P(op)->cast_object) {
          zval tmp;
          if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL TSRMLS_CC) == SUCCESS) {
            result = Z_LVAL(tmp);
            break;
          }
        } else if (Z_OBJ_HT_P(op)->get) {
          zval *tmp = Z_OBJ_HT_P(op)->get(op TSRMLS_CC);
          if(Z_TYPE_P(tmp) != IS_OBJECT) {
            /* for safety - avoid loop */
            convert_to_boolean(tmp);
            result = Z_LVAL_P(tmp);
            zval_ptr_dtor(&tmp);
            break;
          }
        }
      }
      result = 1;
      break;
    default:
      result = 0;
      break;
  }

這段代碼比較直觀,函數(shù)沒(méi)有對(duì)檢測(cè)值做任何的轉(zhuǎn)換,通過(guò)這段代碼來(lái)進(jìn)一步分析示例中的empty函數(shù)做分析:
empty(null),到IS_NULL分支,result=0,i_zend_is_true() == 0,!i_zend_is_true() == 1,因此返回true。

empty(false),到IS_BOOL分支,result = ZLVAL_P(false) = 0,i_zend_is_true() == 0,!i_zend_is_true() == 1,因此返回true。

empty(array()),到IS_ARRAY分支,result = zend_hash_num_elements(Z_ARRVAL_P(op)) ? 1 : 0),zend_hash_num_elements返回?cái)?shù)組元素的數(shù)量,array為空,因此result為0,i_zend_is_true() == 0,!i_zend_is_true() == 1,因此返回true。

empty('0'),到IS_STRING分支,因?yàn)閆_STRLENP(op) == 1 且 Z_STRVAL_P(op)[0] == '0',因此result為0,i_zend_is_true() == 0,!i_zend_is_true() == 1,因此返回true。

empty(1),到IS_LONG分支,result = Z_LVAL_P(op) = 1,i_zend_is_true == 1,!i_zend_is_true() == 0,因此返回false。

對(duì)于isset函數(shù),最終實(shí)現(xiàn)判斷的代碼是:

if (isset && Z_TYPE_PP(value) != IS_NULL) {
  ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 1);
} else {
  ZVAL_BOOL(&EX_T(opline->result.var).tmp_var, 0);
}

只要value被設(shè)置了且不為NULL,isset函數(shù)就返回true。

小結(jié)

這次閱讀這兩個(gè)函數(shù)的源碼,學(xué)習(xí)到了:

1、PHP代碼在編譯期間的執(zhí)行步驟

2、如何查找PHP語(yǔ)言結(jié)構(gòu)的源碼位置

3、如何查找opcode處理函數(shù)的具體函數(shù)

學(xué)無(wú)止境,每個(gè)人都有自己的短板,只有通過(guò)不斷學(xué)習(xí)才能將自己的短板補(bǔ)上。

原創(chuàng)文章,文筆有限,才疏學(xué)淺,文中若有不正之處,萬(wàn)望告知。

如果本文對(duì)你有幫助,請(qǐng)點(diǎn)下推薦吧,謝謝^_^

以上這篇深入理解PHP中的empty和isset函數(shù)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持幫客之家。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133076.htmlTechArticle深入理解PHP中的empty和isset函數(shù),emptyisset 近日被問(wèn)到PHP中empty和isset函數(shù)時(shí)怎么判斷變量的,剛開(kāi)始我是一臉懵逼的,因?yàn)槲易约阂仓皇且恢?..
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

What is Late Static Binding in PHP? What is Late Static Binding in PHP? Jul 13, 2025 am 02:36 AM

LateStaticBindinginPHPallowsstatic::torefertotheclassinitiallycalledatruntimeininheritancescenarios.BeforePHP5.3,self::alwaysreferencedtheclasswherethemethodwasdefined,causingChildClass::sayHello()tooutput"ParentClass".Withlatestaticbinding

See all articles