首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

isset

(PHP 4, PHP 5, PHP 7)

isset - 确定是否设置了变量 NULL

描述

代码语言:javascript
复制
bool isset ( mixed $var [, mixed $... ] )

确定是否设置了变量NULL

如果使用unset()取消设置变量,则将不再设置该变量。如果测试已设置的变量,则isset()将返回。另请注意,空字符(“\ 0”)不等于PHP 常量。FALSENULLNULL

如果提供了多个参数,则仅当设置了所有参数时,isset()才会返回TRUE。评估从左到右进行,一旦遇到未设置的变量就停止。

参数

var

要检查的变量。

...

另一个变量

返回值

TRUE如果var存在则返回并且具有除以外的值NULLFALSE除此以外。

Changelog

描述

5.4.0

检查字符串的非数字偏移现在返回FALSE。

Examples

示例#1 isset()示例

代码语言:javascript
复制
<?php

$var?=?'';

//?This?will?evaluate?to?TRUE?so?the?text?will?be?printed.
if?(isset($var))?{
????echo?"This?var?is?set?so?I?will?print.";
}

//?In?the?next?examples?we'll?use?var_dump?to?output
//?the?return?value?of?isset().

$a?=?"test";
$b?=?"anothertest";

var_dump(isset($a));??????//?TRUE
var_dump(isset($a,?$b));?//?TRUE

unset?($a);

var_dump(isset($a));?????//?FALSE
var_dump(isset($a,?$b));?//?FALSE

$foo?=?NULL;
var_dump(isset($foo));???//?FALSE

?>

这也适用于数组中的元素:

代码语言:javascript
复制
<?php

$a?=?array?('test'?=>?1,?'hello'?=>?NULL,?'pie'?=>?array('a'?=>?'apple'));

var_dump(isset($a['test']));????????????//?TRUE
var_dump(isset($a['foo']));?????????????//?FALSE
var_dump(isset($a['hello']));???????????//?FALSE

//?The?key?'hello'?equals?NULL?so?is?considered?unset
//?If?you?want?to?check?for?NULL?key?values?then?try:?
var_dump(array_key_exists('hello',?$a));?//?TRUE

//?Checking?deeper?array?values
var_dump(isset($a['pie']['a']));????????//?TRUE
var_dump(isset($a['pie']['b']));????????//?FALSE
var_dump(isset($a['cake']['a']['b']));??//?FALSE

?>

示例#2 isset()on String Offsets

PHP 5.4更改了传递字符串偏移时isset()的行为方式。

代码语言:javascript
复制
<?php
$expected_array_got_string?=?'somestring';
var_dump(isset($expected_array_got_string['some_key']));
var_dump(isset($expected_array_got_string[0]));
var_dump(isset($expected_array_got_string['0']));
var_dump(isset($expected_array_got_string[0.5]));
var_dump(isset($expected_array_got_string['0.5']));
var_dump(isset($expected_array_got_string['0?Mostel']));
?>

在PHP 5.3中输出上面的例子:

代码语言:javascript
复制
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

在PHP 5.4中输出上述示例:

代码语言:javascript
复制
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)

Notes

Warning

isset()仅适用于变量,因为传递任何其他内容都会导致解析错误。要检查是否设置了常量,请使用defined()函数。

注意:因为这是一种语言结构而不是函数,所以不能使用变量函数调用它。

注意:在不可访问的对象属性上使用isset()时,如果声明,将调用__isset()重载方法。

See Also

  • empty() - 确定变量是否为空
  • __isset()
  • unset() - 取消设置给定变量
  • defined() - 检查给定的命名常量是否存在
  • array_key_exists() - 检查数组中是否存在给定的键或索引
  • is_null() - 查找变量是否为NULL
  • the error control @ operator

← is_string

print_r →

代码语言:txt
复制
 ? 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com