在php中判断字符串为空的方法有很多种方法,像等于空 == null,empty,isset都可以用来判断一个变量或字符串是否为空哦,下面我来给各位朋友分别介绍.
字符串;判断字符串是否为空;输出判断;你可以在修饰一下
实例代码如下:
if (emptyempty($C_char)) return false; //是否已设定 if ($C_char=='') return false; //是否为空
利用==""
实例代码如下:
<?php $str = ''; if ($str === '') { //''==null ''==false ''!==false echo 'str is a NULL string.'; } ?>
empty判断是否为空
实例代码如下:
<?php $var = 0; // 结果为 true,因为 $var 为空 if (emptyempty($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()函数是用来测试变量是否已经配置.若变量已存在、非空字符串或者非零,则返回 false 值;反之返回true值.所以,当字符串的值为0时,也返回true,就是执行empty内部的语句.这就是陷阱.如: 假设 $value = 0; 则empty($value)=false.
判断字符串是否为空,可以这么判断: if ($value=="") ...
* 格式:bool empty ( mixed var )
* 功能:检查一个变量是否为空
* 返回值:
* 若变量不存在则返回 TRUE
* 若变量存在且其值为""、0、"0"、NULL、、FALSE、 array()、var $var; 以及没有任何属性的对象,则返回TURE
* 若变量存在且值不为""、0、"0"、NULL、、FALSE、 array()、var $var; 以及没有任何属性的对象,则返回FALSE
isset()和empty()判断方法:
实例代码如下:
<?php function demo() { $var = _post['a']; //接受过来的参数 echo " isset测试:<Br> "; if (isset($var)) n { echo ' 变量$var存在!<Br> '; } else { echo ' 变量$var不存在!<Br> '; } echo " empty测试:<Br> "; if (emptyempty($var)) { echo ' 变量$var的值为空<Br> '; } else { echo ' 变量$var的值不为空<Br> '; } echo " 变量直接测试:<Br> "; if ($var) { echo ' 变量$var存在!<Br> '; } else { echo ' 变量$var不存在!<Br> '; } } ?>
本文地址:
转载随意,但请附上文章地址:-)

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

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
