2

I am struggling with displaying some content depending on if an array does have a value or not.Every time the code in the else part is executed. What's wrong here? Is there any syntax error with this code? I'm using php laravel.

foreach($now as $v)
{
        $arry[$c++]=$v->Code; 
}
if($arry==null){
     Do Something
}
else{
     Do Something else
}
4
  • you should check array's length rather than comparing with null Commented Sep 14, 2018 at 8:20
  • check for empty? Sounds like a duplicate of stackoverflow.com/questions/2216052/… Commented Sep 14, 2018 at 8:21
  • if you want to check that array is empty you should use empty($arry) or $arry === [] construction Commented Sep 14, 2018 at 8:21
  • 5
    Possible duplicate of How to check whether an array is empty using PHP? Commented Sep 14, 2018 at 8:25

7 Answers 7

8
if($arry) {
     echo 'The array is not empty';
}
else {
     echo 'The array is empty';
}

For more: How can you check if an array is empty?

Sign up to request clarification or add additional context in comments.

Comments

3

Better to do if (!empty($arry)) {}

P.S. yes if (!$arry) do the same, but every person which is not familiar with php or even have little understanding of programming, should understand the code. Whats mean "not array", but if it will be "not empty array" is more clear. It is very straight forward.

Clean code

Comments

2
if ( sizeof($arry) ) { // If more than 0
   // Do Something
} else { // If 0
   // Do Something else
}

1 Comment

Note that in PHP sizeof() is NOT what you may think it is based on your experience from i.e. C. It's just alias for count(). Luckily, since IIRC PHP5 array counter is maintained internally, but prior that change it was really iterating over all the elements counting. So beware underlying implementation. Also this code is showing the intent. You do not care how many elements you have. You want to know it is not empty, so you should have written if (!empty($arry)) {... not empty....} else { .... }
1
 if(!$users->isEmpty()){
        return response()->json(['status' => 1, 'data' => $users]);
    }else{
        return response()->json(['status' => 0, 'msg' => 'no employees found']);
    }

Comments

0

Always check array before iterate in foreach and check with count function to check its value

if(isset($now) && count($now)>0){
    foreach($now as $v) {
            $arry[$c++]=$v->Code; 
    }
}
if(count($arry)>0){
     Do Something
}
else{
     Do Something else
}

Comments

-1

You can use empty() or count() or sizeof() as below :

$a = [];
if(empty($a)) {
    echo 'empty' . PHP_EOL;
} else {
    echo '!empty' . PHP_EOL;
}
if(count($a) == 0) {
    echo 'empty' . PHP_EOL;
} else {
    echo '!empty' . PHP_EOL;
}
if(sizeof($a) == 0) {
    echo 'empty' . PHP_EOL;
} else {
    echo '!empty' . PHP_EOL;
}

echo empty($a) . PHP_EOL; // 1
echo count($a) . PHP_EOL; // 0
echo sizeof($a) . PHP_EOL; // 0

Output : 
empty
empty
empty
1
0
0

Comments

-2

this is not related to Laravel framework

if (count($arry) > 0) 
   Do Something
else
   Do Something else

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.