Introduction to functions for determining null strings in php

Source: Internet
Author: User

There are many methods to judge whether a string is null in php, such as null = null, empty, and isset, which can be used to determine whether a variable or string is null, next I will introduce it to you.

String; judge whether the string is null; Output judgment; you can modify it

The Code is as follows: Copy code

If (empty ($ C_char) return false; // whether it is set

If ($ C_char = '') return false; // whether it is null

Use = ""

Example

The Code is as follows: Copy code

<? Php
$ Str = '';

If ($ str = '') {//'' = null ''= false ''! = False
Echo 'str is a NULL string .';
}
?>


Empty determines whether it is null

The Code is as follows: Copy code

<? Php
$ Var = 0;
// The result is true because $ var is null.
If (empty ($ var )){
Echo '$ var is either 0 or not set at all ';
}
// The result is false because $ var has been set.
If (! Isset ($ var )){
Echo '$ var is not set at all ';
}
?>

On the surface, it is easy to misunderstand that the empty () function is a function used to judge whether the string is null. In fact, it is not, so I suffered a lot of losses.
The empty () function is used to test whether the variable has been configured. If the variable already exists, non-null string, or non-zero, false is returned. Otherwise, false is returned.

True. Therefore, if the value of the string is 0, true is returned, that is, the internal statement of empty is executed. This is a trap.
For example, suppose $ value = 0; then empty ($ value) = false.


To judge whether the string is null, you can judge it as follows: if ($ value = "")...
* Format: bool empty (mixed var)
* Function: Check whether a variable is empty.
* Return value:
* Returns TRUE if the variable does not exist.
* If a variable exists and its values are "", 0, "0", NULL, FALSE, array (), var $ var, and objects without any attributes, the system returns

TURE
* If a variable exists and its values are not "", 0, "0", NULL, FALSE, array (), var $ var, or an object without any attribute

FALSE

Isset () and empty () judgment methods:

The Code is as follows: Copy code

Function demo (){
$ Var = _ post ['a']; // The accepted parameter.
Echo "isset test: <Br> ";
If (isset ($ var) n
{
Echo 'variable $ var exists! <Br> ';
} Else
{
Echo 'variable $ var does not exist! <Br> ';
}
Echo "empty test: <Br> ";
If (empty ($ var )){
Echo 'variable $ var value is empty <Br> ';
}
Else
{

Echo 'variable $ var value is not empty <Br> ';
}
Echo "Direct Test of variables: <Br> ";
If ($ var ){

Echo 'variable $ var exists! <Br> ';
}
Else {
Echo 'variable $ var does not exist! <Br> ';
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.