Home >Backend Development >PHP Tutorial >Introduction to PHP function to determine whether a string is empty_PHP tutorial

Introduction to PHP function to determine whether a string is empty_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:053520browse

There are many ways to determine whether a string is empty in php, such as equal to null == null, empty, and isset can all be used to determine whether a variable or string is empty. Let me tell you about them below. introduce.

String; determine whether the string is empty; output judgment; you can modify it

The code is as follows Copy code
 代码如下 复制代码

if (empty($C_char)) return false; //是否已设定

if ($C_char=='') return false; //是否为空

if (empty($C_char)) return false; //Whether it has been set

if ($C_char=='') return false; //Is it empty

Use==""
 代码如下 复制代码

$str = '';

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

Example

The code is as follows Copy code
代码如下 复制代码

$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';
}
?>

$str = ''; if($str==='') {//''==null ''==false ''!==false echo 'str is a NULL string.'; } ?> empty determines whether it is empty
The code is as follows Copy code
$var = 0;<🎜> // The result is true because $var is empty<🎜> if (empty($var)) {<🎜> echo '$var is either 0 or not set at all';<🎜> }<🎜> // Result is false because $var is 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 that determines whether a string is empty. In fact, it is not, and I suffered a lot because of it.
The empty() function is used to test whether the variable has been configured. If the variable already exists, is a non-empty string, or is non-zero, a false value is returned; otherwise

is returned.

true value. Therefore, when the value of the string is 0, true is also returned, which is to execute the statement inside empty. This is the trap.
For example: Assume $value = 0; then empty($value)=false.


To determine whether a string is empty, you can determine it like this: if ($value=="") ...
* Format: bool empty (mixed var)
* Function: Check whether a variable is empty
* Return value:
* If the variable does not exist, return TRUE
* If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, return

TURE
* If the variable exists and the value is not "", 0, "0", NULL,, FALSE, array(), var $var; and an object without any attributes, return

FALSE

isset() and empty() judgment methods:

The code is as follows
 代码如下 复制代码

function demo(){
  $var = _post['a'];//接受过来的参数 
echo   " isset测试:
" ;
if  ( isset ( $var ))n  

echo   ' 变量$var存在!
'  ;
}else
{
echo   ' 变量$var不存在!
'  ;
}
echo   " empty测试:
" ;
if  ( empty ( $var )){    
echo   ' 变量$var的值为空
' ;

else
{
  
echo   ' 变量$var的值不为空
' ;
}
echo   " 变量直接测试:
" ;
if  ( $var ){
     
echo   ' 变量$var存在!
' ; 
}  
else {
echo   ' 变量$var不存在!
' ;
}

Copy code
function demo(){ $var = _post['a'];//Accepted 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 )){ echo 'The value of variable $var is empty
' ;
} else {
echo 'The value of variable $var is not empty
' ; }
echo " Direct variable test:
" ;
if ( $var ){                       echo ' Variable $var exists!
' ; } else { echo 'Variable $var does not exist!
' ; } http://www.bkjia.com/PHPjc/628864.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628864.htmlTechArticleThere are many ways to determine whether a string is empty in php, such as equal to empty == null, empty, isset can be used to determine whether a variable or string is empty. Let me share it with you...
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