isset和is_null的不同
isset和is_null啥区别,
看手册上讲的话, isset和is_null的功能几乎完全”相反的一样”..
是不是isset就是一个is_null的相反的别名?
诶, 要说区别, 那还真的是很多~
一切的不同都是因为: is_null是函数, isset是语句.
isset是语句, 和echo, print一样, 是PHP本身的一种语言结构.
而is_null是函数, 和我们一般的函数一样, 可以做为可变函数调用.
你也许会说, 好了,好了,我知道函数和语句的区别, 但到底是TMD什么区别?
诶, 所谓语句,语言结构, 就是说, 是语言本身支持的语句, 标识符.
比如, for, foreach, continue 等等, 它们在语法分析的时刻就被”抹掉”(逻辑上替代了)了.
让我们看看isset这个语句在语法分析的过程中, 是如何被”抹掉”的.
1. 首先, 在词法分析的时候, isset会被识别为T_ISSET标识符.
2. 而在语法分析阶段, isset($var)这个指令, 会被分析成一条Opcode:ZEND_ISSET_ISEMPTY_VARS.
你可以理解isset就想C语言里面的宏, 在编译/执行之前已经被展开了.
因为这个, 所以在表现上, 会有如下的不同:
因为is_null是函数, 所以它可以通过如下方式调用:
$var = NULL;
$func = "is_null";
$func($var);
?>
而, isset因为是语句, 所以不能这样调用.
因为is_null是函数, 所以它可以接受函数返回值做为参数, 而isset不行(当然, 如果PHP想支持, 其实也是可以的, 只不过就要增加编译阶段的复杂度了):
is_null(intval("0x45"));
//OK
isset(intval("0x45"));
//PHP Fatal error: Can't use function return value in write context
is_null(NULL);
//OK
isset(NULL);
//PHP Parse error: syntax error
?>
说了这么多isset的缺点了, 说点它的优点吧:
因为isset是语句, 所以它快!
在一千万次的简单检测语句循环中, 对比结果如下:
$a="laruence":
isset($a); //用时: 1.15s
is_null($a); //用时: 3.89s
?>
因为isset叫做isset, 所以它在检测未定义变量的时候, 不会产生NOTICE:
isset($laruence);
//OK
is_null($laruence);
//PHP Notice: Undefined variable: laruence
?>
那么, 对于什么时候用isset什么时候用is_null, 我有什么建议呢?
诶, 我的建议是, 用函数做函数应该做的事情~, 听起来象废话?
isset => is set? => 变量有没有被赋值(声明)
is_null => is null? => 变量为NULL么?
另外, 如果要用is_null, 我建议使用 “=== NULL” 来代替, 它不仅语义和is_null一致, 结果一致, 速度还和isset差不多:
在一千万次的简单检测语句循环中, 对比结果如下:
$a="laruence":
isset($a); //用时: 1.15s
is_null($a); //用时: 3.88s
$a===NULL; //用时: 1.22s
?>

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

AI Hentai Generator
Generate AI Hentai for free.

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

The difference between null and NULL in C language is: null is a macro definition in C language, usually used to represent a null pointer, which can be used to initialize pointer variables, or to determine whether the pointer is null in a conditional statement; NULL is a macro definition in C language A predefined constant in , usually used to represent a null value, used to represent a null pointer, null pointer array or null structure pointer.

This article will explain in detail how PHP determines whether a specified key exists in an array. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP determines whether a specified key exists in an array: In PHP, there are many ways to determine whether a specified key exists in an array: 1. Use the isset() function: isset($array["key"]) This function returns a Boolean value, true if the specified key exists, false otherwise. 2. Use array_key_exists() function: array_key_exists("key",$arr

In JavaScript, both undefined and null represent the concept of "nothing": 1. undefined represents an uninitialized variable or a non-existent property. When a variable is declared but no value is assigned to it, the value of the variable is undefined , when accessing properties that do not exist in the object, the returned value is also undefined; 2. null represents an empty object reference. In some cases, the object reference can be set to null to release the memory it occupies.

Both null and undefined indicate a lack of value or an undefined state. Depending on the usage scenario, there are some guiding principles for choosing to use null or undefined: 1. When you need to clearly indicate that a variable is empty or invalid, you can use null; 2. When a variable has been declared but not yet assigned a value, it will be set to undefined by default; 3. When you need to check whether a variable is empty or undefined, use the strict equality operator "===" to determine whether the variable is null or undefined. .

The difference between null and undefined is: 1. Semantic meaning; 2. Usage scenarios; 3. Comparison with other values; 4. Relationship with global variables; 5. Relationship with function parameters; 6. Nullability check; 7. Performance considerations; 8. Performance in JSON serialization; 9. Relationship with types. Detailed introduction: 1. Semantic meaning, null usually means knowing that this variable will not have any valid object value, while undefined usually means that the variable has not been assigned a value, or the object does not have this attribute; 2. Usage scenarios, etc.

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

Usage: 1. Initialize the reference type variable to null, indicating that the variable does not currently point to any object; 2. Set the reference type variable to null, which can release the memory space of the object referenced by the variable and help the garbage collector to recover This object; 3. Use null to check whether a reference is empty. You can avoid the occurrence of NullPointerException by judging whether the reference is null. 4. Use null in conditional judgment to judge whether a reference is empty.
