PHP judgment string for null function introduction _php tutorial

Source: Internet
Author: User
In PHP, there are a number of methods to judge the string is empty, such as equal to the empty = = Null,empty,isset can be used to determine whether a variable or string is empty oh, I would like to give you friends to introduce.

string; Determines whether the string is empty; You can do it in a little bit.

The code is as follows Copy Code

if (Empty ($C _char)) return false; Whether it has been set

if ($C _char== ") return false; is empty

Use = = ""

Cases

The code is as follows Copy Code

$str = ";

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


Empty to determine if NULL

The code is as follows Copy Code

$var = 0;
The result is true because the $var is empty
if (empty ($var)) {
Echo ' $var is either 0 or isn't set at all ';
}
The result is false because the $var has been set
if (!isset ($var)) {
Echo ' $var is not set at all ';
}
?>

On the face of it, it is easy to misunderstand that the empty () function is a function that determines whether a string is empty, in fact it is not, I eat a lot of losses.
The empty () function is used to test whether a variable has been configured. Returns a value of False if the variable already exists, a non-empty string, or nonzero;

A value of true. So, when the value of the string is 0 o'clock, it also returns True, which is the statement inside the empty. This is the trap.
such as: suppose $value = 0; Then empty ($value) =false.


Judging if the string is empty, you can say so: if ($value = = "") ...
* Format: BOOL empty (mixed Var)
* Function: Check if a variable is empty
* Return value:
* Returns TRUE if the variable does not exist
* If the variable exists and its value is "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties, the return

TURE
* If the variable exists and the value is not "", 0, "0", NULL, FALSE, Array (), Var $var; and objects that do not have any properties, the return

FALSE

Isset () and empty () determine the method:

The code is as follows Copy Code

Function Demo () {
$var = _post[' A '];//takes over the parameters
echo "Isset test:
" ;
if (Isset ($var)) n
{
Echo ' Variable $var exists!
' ;
}else
{
echo ' variable $var does not exist!
' ;
}
echo "Empty test:
" ;
if (empty ($var)) {
The value of ECHO ' variable $var is null
' ;
}
Else
{

echo ' variable $var value is not empty
' ;
}
echo "Variable direct test:
" ;
if ($var) {

Echo ' Variable $var exists!
' ;
}
else {
echo ' variable $var does not exist!
' ;
}

http://www.bkjia.com/PHPjc/628864.html www.bkjia.com true http://www.bkjia.com/PHPjc/628864.html techarticle in PHP, there are a number of ways to judge the string is empty, like equal to empty = = Null,empty,isset can be used to determine whether a variable or string is empty oh, let me give you friends to divide ...

  • 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.