PHP isset()与empty()的使用区别详解
通过对PHP语言的学习,应该知道它是基于函数的一款HTML脚本语言。庞大的函数库支持着PHP语言功能的实现。下面我们为大家介绍有关PHP函数isset()与empty()的相关
PHP的isset()函数 一般用来检测变量是否设置
格式:bool isset ( mixed var [, mixed var [, ...]] )
功能:检测变量是否设置
返回值:
若变量不存在则返回 FALSE
若变量存在且其值为NULL,也返回 FALSE
若变量存在且值不为NULL,香港服务器,则返回 TURE
同时检查多个变量时,香港空间,每个单项都符合上一条要求时才返回 TRUE,否则结果为 FALSE
版本:PHP 3, PHP 4, PHP 5
更多说明:
使用 unset() 释放变量之后,它将不再是 isset()。
PHP函数isset()只能用于变量,传递任何其它参数都将造成解析错误。
检测常量是否已设置可使用 defined() 函数。
PHP的empty()函数 判断值为否为空
格式:bool empty ( mixed var )
功能:检查一个变量是否为空
返回值:
若变量不存在则返回 TRUE
若变量存在且其值为""、0、"0"、NULL、、FALSE、array()、var $var; 以及没有任何属性的对象,则返回 TURE
若变量存在且值不为""、0、"0"、NULL、、FALSE、array()、var $var; 以及没有任何属性的对象,美国空间,则返回 FALSE
版本:PHP 3, PHP 4, PHP 5
更多说明:
empty()的返回值=!(boolean) var,但不会因为变量未定义而产生警告信息。参见转换为布尔值获取更多信息。
empty() 只能用于变量,传递任何其它参数都将造成Paser error而终止运行。
检测常量是否已设置可使用 defined() 函数。
例子: empty() 与 isset() 的一个简单比较
复制代码 代码如下:
$var = 0;
// 结果为 true,因为 $var 为空
if (empty($var)) {
echo '$var is either 0 or not set at all';
}
// 结果为 false,因为 $var 已设置
if (!isset($var)) {
echo '$var is not set at all';
}
?>
注: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。
注: empty() 只检测变量,检测任何非变量的东西都将导致解析错误。换句话说,后边的语句将不会起作用: empty(addslashes($name))。
下面是经过脚本之家测试过的一段isset与empty函数详细例子的代码,看完这个基本上就差不多了:
复制代码 代码如下:
error_reporting(E_ALL);
echo '未定义$var
';
echo "isset测试:
";
if ( isset ( $var ))
{
echo '变量$var存在!
' ;
}
echo "empty测试:
";
if ( empty ( $var )){
echo '变量$var的值为空
';
}
else
{
echo '变量$var的值不为空
';
}
echo "变量直接测试:
";
if ( $var ){
echo '变量$var存在!
';
}
else {
echo '变量$var不存在!
';
}
echo '----------------------------------
';
echo '$var = \'\'
';
echo "isset测试:
";
$var = '';
if ( isset ( $var ))
{
echo '变量$var存在!
' ;
}
echo "empty测试:
";
if ( empty ( $var )){
echo '变量$var的值为空
';
}
else
{
echo '变量$var的值不为空
';
}
echo "变量直接测试:
";
if ( $var ){
echo '变量$var存在!
';
}
else {
echo '变量$var不存在!
';
}
echo '----------------------------------
';
echo '$var = 0
';
echo 'isset测试:
';
$var = 0 ;
if ( isset ( $var ))
{
echo '变量$var存在!
' ;
}
echo "empty测试:
";
if ( empty ( $var )){
echo '变量$var的值为空
';
}
else
{
echo '变量$var的值不为空
';
}
echo "变量直接测试:
";
if ( $var ){
echo '变量$var存在!
';
}
else {
echo '变量$var不存在!
';
}
echo '----------------------------------
';
echo '$var = null
';
echo 'isset测试:
';
$var = null ;
if ( isset ( $var ))
{
echo '变量$var存在!
' ;
}
echo "empty测试:
";
if ( empty ( $var )){
echo '变量$var的值为空
';
}
else
{
echo '变量$var的值不为空
';
}
echo "变量直接测试:
";
if ( $var ){
echo '变量$var存在!
';
}
else {
echo '变量$var不存在!
';
}
echo '----------------------------------
';
echo '$var ="php"
';
echo 'isset测试:
';
$var = "php";
if ( isset ( $var ))
{
echo '变量$var存在!
' ;
}
echo "empty测试:
";
if ( empty ( $var )){
echo '变量$var的值为空
';
}
else
{
echo '变量$var的值不为空
';
}
echo "变量直接测试:
";
if ( $var ){
echo '变量$var存在!
';
}
else {
echo '变量$var不存在!
';
}
?>

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

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

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

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

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

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

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

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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