Home  >  Article  >  Backend Development  >  How to determine whether an array is empty in php

How to determine whether an array is empty in php

(*-*)浩
(*-*)浩Original
2019-10-26 14:22:084250browse

How to determine whether an array is empty in php

PHP’s preferred method to determine if an array is empty: count($arr),size($arr); (Recommended learning: PHP video tutorial

Just use this function count. If the output is 0, then the array is empty. The following is a simple test code.

$arr= array("");
echo count($arr);
echo size($arr);
 
//输出1
 
$arr= array();
echo count($arr);
echo size($arr);
 
//输出0

PHP method to determine if an array is empty: empty($arr);

empty() The function is to determine whether the variable is empty. If the variable = empty, it returns TRUE , if it is not empty, return FALSE (not an empty value)

$arr= array("");
$result = empty($arr);
 
//$result = false
$arr = array();
$result = empty($arr);
 
//$result = true

Are these two methods sufficient to deal with the problem of judging whether simple arrays and multi-dimensional arrays are empty? Personally, I generally use empty() to judge whether the array is empty. , so that the code looks easier to understand.

The above is the detailed content of How to determine whether an array is empty in php. For more information, please follow other related articles on the PHP Chinese website!

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