Grammar: Implementation of IF judgment
When interviewing PHP, we often encounter a question about whether various types of null values are 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>
Let’s take a brief look at how if is handled in the zend engine from the following example:
<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>
Here is an example Relatively simple, the result will be nothing. (The codes involved in this article are all version php-7.0.4)
The previous article introduced the zend_execute function, the entrance to the zend execution phase. Let’s start directly from here. If you are not familiar with it, you can read the previous article.
The opcodes generated by compilation are as follows:
Among them, opcode=38 is the execution operation of $a = ”, and opcode=43 is the operation of if. Let’s see how this step is executed.
According to the opcode and the two operand types, the corresponding handler can be found: 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>
From this function, we can see the execution process of if: if the condition is true, opline++ will execute the next opcode (that is, the statement within the if) in sequence. Otherwise, jump is performed, skipping the inner if statement and directly executing the outer if statement.
i_zend_is_true This function is used to determine whether various types of values are true. The previous part is to determine whether they are bool type. If so, it will be processed directly.
<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 and empty functions will be added later...
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above introduces the grammar: the implementation of IF judgment, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OPStack is an open source blockchain framework released by Optimism Collective, the development group behind the Optimism Network. It is an important tool for both the Ethereum and Optimism communities. The main goal of OPStack is to strengthen the Optimism Network, providing key software tools to the Optimism Mainnet, as well as the upcoming Optimism Superchain and its governance model. By providing a developer-oriented environment, the core idea of OPStack is to promote growth and innovation in the Ethereum space. It paves the way for cutting-edge developments and makes blockchain creation simpler. OPStac

Previously, Optimism officially announced that the Optimism chain has been renamed OPMainnet. Some people may ask what chain OPMainnet is? According to current information, OPMainnet is an open source super chain dedicated to funding public goods in a sustainable manner. It is one of many L2 chains that can communicate seamlessly with Base, ZoraNetwork, PGN, Redstone and other chains, but it does not It is not a single blockchain, it represents twenty entire super chain networks. The editor below will talk about this OPMainnet in detail to help you understand the OPMainnet network. What chain is OPMainnet? OPMainnet is Ethernet

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

PHP does not recognize ZendOptimizer, how to solve it? In PHP development, sometimes you may encounter a situation where PHP cannot recognize ZendOptimizer, which will cause some PHP codes to not run properly. In this case, we need to take some measures to solve the problem. Some possible workarounds are described below, along with specific code examples. 1. Confirm whether ZendOptimizer is installed correctly: First, we need to confirm that ZendOptimizer

The Windows 2003 installation package includes Zend, PHP5.2.17, PHPWind8.7 and PHPMyadmin3.5.2. You can download the installation package directly to save time searching for resources. However, since MySQL has exceeded the upload limit, you need to go to the MySQL official website to download. Then unzip and copy to the D drive, as shown below: MySQLinDdisk Install and configure WindowsIIS+FTP Click Start>Control Panel>Add or Remove Programs.AddingordeletingaPG Click Add/Remove Windows Components (A). Addingorde

It is very common to use switch statements to select multiple branches in PHP. Usually, a break statement is used to exit the switch statement after each branch. However, there are situations where we do not want to use the break statement. This article will introduce the situation of not using break in the PHP switch statement.

Note 1. The function of break is to jump out of the current loop block (for, while, dowhile) or program block (switch). 2. The function of the loop block is to jump out of the loop body currently in the loop. The function of the program block is to interrupt and compare the next case condition. Use break in a switch statement to terminate the switch statement. When break is used in a loop, it breaks out of the loop. There is no point in using break elsewhere. Example intsum=0;inti;for(i=1;i
