isset() - php 变量处理函数
isset()
(PHP 4, PHP 5, PHP 7)
检测变量是否已设置并且非NULL
说明
isset(mixed $var[,mixed$...
]): bool检测变量是否设置,并且不是NULL
。
如果已经使用unset()释放了一个变量之后,它将不再是isset()。若使用isset()测试一个被设置成NULL
的变量,将返回FALSE
。同时要注意的是 null 字符("0")并不等同于 PHP 的NULL
常量。
如果一次传入多个参数,那么isset()只有在全部参数都以被设置时返回TRUE
计算过程从左至右,中途遇到没有设置的变量时就会立即停止。
参数
$var要检查的变量。
...其他变量。
返回值
如果$var存在并且值不是NULL
则返回TRUE
,否则返回FALSE
。
更新日志
版本 | 说明 |
---|---|
5.4.0 | 检查字符的非数字偏移量将会返回 |
范例
Example #1isset()例子
这对于数组中的元素也同样有效:
在字符串位移中使用isset()
PHP 5.4 改变了传入字符串位移时isset()的行为。
以上例程在PHP 5.3中的输出:
bool(true) bool(true) bool(true) bool(true) bool(true) bool(true)
以上例程在PHP 5.4中的输出:
bool(false) bool(true) bool(true) bool(true) bool(false) bool(false)
注释
Warningisset()只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置,可使用defined()函数。
Note:因为是一个语言构造器而不是一个函数,不能被可变函数调用。Note:
如果使用isset()来检查对象无法访问的属性,如果__isset()方法已经定义则会调用这个重载方法。
参见
empty()
检查一个变量是否为空- __isset()
unset()
释放给定的变量defined()
检查某个名称的常量是否存在- the type comparison tables
array_key_exists()
检查数组里是否有指定的键名或索引is_null()
检测变量是否为 NULL- 错误控制@运算符。
I, too, was dismayed to find that isset($foo) returns false if ($foo == null). Here's an (awkward) way around it. unset($foo); if (compact('foo') != array()) { do_your_thing(); } Of course, that is very non-intuitive, long, hard-to-understand, and kludgy. Better to design your code so you don't depend on the difference between an unset variable and a variable with the value null. But "better" only because PHP has made this weird development choice. In my thinking this was a mistake in the development of PHP. The name ("isset") should describe the function and not have the desciption be "is set AND is not null". If it was done properly a programmer could very easily do (isset($var) || is_null($var)) if they wanted to check for this! A variable set to null is a different state than a variable not set - there should be some easy way to differentiate. Just my (pointless) $0.02.
"empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set." So essentially is the same as doesn't it? :) !empty() mimics the chk() function posted before.
You can safely use isset to check properties and subproperties of objects directly. So instead of writing isset($abc) && isset($abc->def) && isset($abc->def->ghi) or in a shorter form isset($abc, $abc->def, $abc->def->ghi) you can just write isset ($abc->def->ghi) without raising any errors, warnings or notices. Examples
How to test for a variable actually existing, including being set to null. This will prevent errors when passing to functions. Note: you can't turn this into a function (e.g. is_defined($myvar)) because get_defined_vars() only gets the variables in the current scope and entering a function changes the scope.
in PHP5, if you have and will always echo 'false'. because the isset() accepts VARIABLES as it parameters, but in this case, $foo->bar is NOT a VARIABLE. it is a VALUE returned from the __get() method of the class Foo. thus the isset($foo->bar) expreesion will always equal 'false'.
The new (as of PHP7) 'null coalesce operator' allows shorthand isset. You can use it like so: Quoted from http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
Careful with this function "ifsetfor" by soapergem, passing by reference means that if, like the example $_GET['id'], the argument is an array index, it will be created in the original array (with a null value), thus causing posible trouble with the following code. At least in PHP 5. For example: will print Array ( ) Array ( [unsetindex] => ) Any foreach or similar will be different before and after the call.
Return Values : Returns TRUE if var exists and has value other than NULL, FALSE otherwise. Could any one explain me in clarity.
I tried the example posted previously by Slawek: $foo = 'a little string'; echo isset($foo)?'yes ':'no ', isset($foo['aaaa'])?'yes ':'no '; He got yes yes, but he didn't say what version of PHP he was using. I tried this on PHP 5.0.5 and got: yes no But on PHP 4.3.5 I got: yes yes Apparently, PHP4 converts the the string 'aaaa' to zero and then returns the string character at that position within the string $foo, when $foo is not an array. That means you can't assume you are dealing with an array, even if you used an expression such as isset($foo['aaaa']['bbb']['cc']['d']), because it will return true also if any part is a string. PHP5 does not do this. If $foo is a string, the index must actually be numeric (e.g. $foo[0]) for it to return the indexed character.
1) Note that isset($var) doesn't distinguish the two cases when $var is undefined, or is null. Evidence is in the following code.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!