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
}
Srima
  • 115
  • 2
  • 4
  • 12
  • you should check array's `length` rather than comparing with `null` – Jigar Shah Sep 14 '18 at 08:20
  • check for `empty`? Sounds like a duplicate of https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php – Lars Mertens Sep 14 '18 at 08:21
  • if you want to check that array is empty you should use `empty($arry)` or `$arry === []` construction – Artem Ilchenko Sep 14 '18 at 08:21
  • 5
    Possible duplicate of [How to check whether an array is empty using PHP?](https://stackoverflow.com/questions/2216052/how-to-check-whether-an-array-is-empty-using-php) – Jigar Shah Sep 14 '18 at 08:25

6 Answers6

7
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?

Sand Of Vega
  • 2,297
  • 16
  • 29
3
if ( sizeof($arry) ) { // If more than 0
   // Do Something
} else { // If 0
   // Do Something else
}
Max S.
  • 1,383
  • 14
  • 25
Manoj Negi
  • 174
  • 1
  • 14
  • 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 { .... }` – Marcin Orlowski Feb 19 '21 at 09:58
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

Aleksandrs
  • 1,488
  • 1
  • 14
  • 34
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
}
Raghbendra Nayak
  • 1,606
  • 3
  • 22
  • 47
-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
Jay Teli
  • 530
  • 1
  • 11
  • 19
-2

this is not related to Laravel framework

if (count($arry) > 0) 
   Do Something
else
   Do Something else
Nisfan
  • 750
  • 6
  • 10