-2

I've var dumped my array:

array(4) { [0]=> string(72) "
1
" [1]=> string(57) "
2
" [2]=> string(63) "
›
" [3]=> string(63) "
»
" }

When I check:

empty($myArray);

It always says true. Why is this?

panthro
  • 19,109
  • 50
  • 152
  • 271

3 Answers3

1

Try to use

count($myArray) == 0

instead

Sharkman
  • 124
  • 11
-1
  • empty() function returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
  • The following things are considered to be empty, and return value would be true:
  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
-1

Alternatively,you can check with count() function like this:-

if((count($myArray) < 1) || (count($myArray)) === 0){
    echo 'Array is empty';
}else{
  echo 'There is Data in Array';
}
Mubo
  • 1,028
  • 8
  • 16
  • Not true. `$myArray` is variable, isn't it? Empty function works also for arrays: `The following things are considered to be empty: array() (an empty array)` – Marcin Nabiałek Jun 30 '14 at 10:33
  • I didn't say you can't use it. but the empty function works some times delivers not the expected result. if used count(), with arrays that is more reliable. What i have wrote is the exact explanation on the php site. If you you are more knowledgeable in it. prove me wrong. – Mubo Jun 30 '14 at 10:38
  • You can look for example at http://stackoverflow.com/questions/2216110/checking-for-empty-arrays-count-vs-empty?rq=1 – Marcin Nabiałek Jun 30 '14 at 10:40