search
Homephp教程php手册php判断字符串为空函数介绍

在php中判断字符串为空的方法有很多种方法,像等于空 == null,empty,isset都可以用来判断一个变量或字符串是否为空哦,下面我来给各位朋友分别介绍.

字符串;判断字符串是否为空;输出判断;你可以在修饰一下

实例代码如下:

if (emptyempty($C_char)) return false; //是否已设定 
if ($C_char=='') return false; //是否为空

利用=="" 

实例代码如下:

<?php
$str = &#39;&#39;;
if ($str === &#39;&#39;) { //&#39;&#39;==null &#39;&#39;==false &#39;&#39;!==false
    echo &#39;str is a NULL string.&#39;;
}
?>

empty判断是否为空

实例代码如下:

<?php
$var = 0;
// 结果为 true,因为 $var 为空
if (emptyempty($var)) {
    echo &#39;$var is either 0 or not set at all&#39;;
}
// 结果为 false,因为 $var 已设置
if (!isset($var)) {
    echo &#39;$var is not set at all&#39;;
}
?>

从表面上看,很容易误解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[&#39;a&#39;]; //接受过来的参数
    echo " isset测试:<Br> ";
    if (isset($var)) n {
        echo &#39; 变量$var存在!<Br> &#39;;
    } else {
        echo &#39; 变量$var不存在!<Br> &#39;;
    }
    echo " empty测试:<Br> ";
    if (emptyempty($var)) {
        echo &#39; 变量$var的值为空<Br> &#39;;
    } else {
        echo &#39; 变量$var的值不为空<Br> &#39;;
    }
    echo " 变量直接测试:<Br> ";
    if ($var) {
        echo &#39; 变量$var存在!<Br> &#39;;
    } else {
        echo &#39; 变量$var不存在!<Br> &#39;;
    }
}
?>


本文地址:

转载随意,但请附上文章地址:-)

Statement
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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

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

Dreamweaver CS6

Visual web development tools

DVWA

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