Home php教程 php手册 PHP函数isset()只能用于变量

PHP函数isset()只能用于变量

Jun 13, 2016 am 11:11 AM
isset php var function Function variable right Format Detection pass

通过对

格式:bool isset ( mixed var [, mixed var [, ...]] )

功能:检测变量是否设置

返回值:

若变量不存在则返回 FALSE
若变量存在且其值为NULL,也返回 FALSE
若变量存在且值不为NULL,则返回 TURE

同时检查多个变量时,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE

版本:PHP 3, PHP 4, PHP 5

更多说明:

使用 unset() 释放变量之后,它将不再是 isset()。
PHP函数isset()只能用于变量,传递任何其它参数都将造成解析错误。
检测常量是否已设置可使用 defined() 函数。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">var</span><span> = </span><span class="attribute-value">''</span><span>;  </span>
</li>
<li class="alt"><span>if (isset($var)) // 空值、0、false<br>的赋值结果均被isset判为 TRUE,所以后边<br>的文本将被打印出来。  </span></li>
<li>
<span>print "blank value -</span><span class="tag">></span><span> </span><span class="attribute">isset</span><span> = </span><span class="attribute-value">true</span><span>. ";   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">var</span><span> = </span><span class="attribute-value">NULL</span><span>;if (!isset($var)) // <br>NULL将被isset判为 FALSE  </span>
</li>
<li>
<span>print "NULL value -</span><span class="tag">></span><span> </span><span class="attribute">isset</span><span> = </span><span class="attribute-value">false</span><span>. "; <br>//以下使用 var_dump 输出 isset() 的返回值。  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">"test"</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">b</span><span> = </span><span class="attribute-value">"anothertest"</span><span>;  </span>
</li>
<li class="alt"><span>var_dump( isset($a) ); // TRUE  </span></li>
<li><span>var_dump( isset ($a, $b) ); // TRUE   </span></li>
<li class="alt"><span>unset ($a);  </span></li>
<li><span>var_dump( isset ($a) ); // FALSE  </span></li>
<li class="alt"><span>var_dump( isset ($a, $b) ); // FALSE  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

PHP函数isset()同样适用于数组元素和对象元素的检查。若数组或对象实例未被定义,则检测其中的数组元素/对象元素将被返回false。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php  </span></span></span></li>
<li>
<span>$</span><span class="attribute">a</span><span> = </span><span class="attribute-value">array</span><span> ('test' =</span><span class="tag">></span><span> 1, 'hello' =</span><span class="tag">></span><span> NULL);   </span>
</li>
<li class="alt"><span>var_dump( isset ($a['test']) ); // TRUE  </span></li>
<li><span>var_dump( isset ($a['foo']) ); // FALSE  </span></li>
<li class="alt"><span>var_dump( isset ($a['hello']) ); // FALSE   </span></li>
<li><span>// 键 'hello' 的值等于 NULL,所以被认为是未置值的。  </span></li>
<li class="alt"><span>// 如果想检测 NULL 键值,可以试试下边的方法。  </span></li>
<li><span>var_dump( array_key_exists('hello', $a) ); // TRUE  </span></li>
<li class="alt">
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login

注: 由于这是一个语言结构而非函数,因此PHP函数isset()无法被变量函数调用。


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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles