php empty(汉字)_PHP: empty - Manual

* Used for checking empty objects/array

* @uses How to check empty objects and array in php code

* @author Aditya Mehrotra

*/

/**

* Empty class

*/classEmptyClass{

}$obj= newstdClass();//or any other class empty object$emptyClassObj= newEmptyClass();$array= array();

if (empty($array)) {

echo'array is empty';//expected result} else {

echo'array is not empty';//unexpected result}

echo"
";

if (empty($obj)) {

echo'object is empty';//expected result} else {

echo'object is not empty';//unexpected result}

echo"
";

if (empty($emptyClassObj)) {

echo'EmptyClass is empty';//expected result} else {

echo'EmptyClass is not empty';//unexpected result}

echo"
";//Result SET 1

//array is empty      => expected result

//object is not empty => ouch what happened

//EmptyClass is not empty => ouch what happened

/**

* So what we do for checking empty object

* @solution use any known property or check property count

* Or you can use below method

* Count function will not return 0 in empty object

*/

//Below line print "Object count:1"echo'Object count:'.count($obj);

echo"
";/**

* This function is used to get object item counts

* @function getCount

* @access public

* @param object|array $var

* @return integer

*/functiongetCount($var) {$count=0;

if (is_array($var) ||is_object($var)) {

foreach ($varas$value) {$count++;

}

}

unset($value);

return$count;

}//Running code again with new logicif (getCount($array) ===0) {

echo'array is empty';//expected result} else {

echo'array is not empty';//unexpected result}

echo"
";

if (getCount($obj) ===0) {

echo'object is empty';//expected result} else {

echo'object is not empty';//unexpected result}

echo"
";

if (getCount($emptyClassObj) ===0) {

echo'EmptyClass is empty';//expected result} else {

echo'EmptyClass is not empty';//unexpected result}//Result SET 2

//array is empty    => expected result  ##everything is all right

//object is empty   => expected result  ##everything is all right

//EmptyClass is empty   => expected result  ##everything is all right

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值