1

I have an array and when I print the output like so print_r($userExists);

it returns Array ( ) I wrote this code to tell me if the array is empty or not:

if(isset($userExists)){
        echo 'exists';
    }else{
        echo 'does not exists';
    }

But regardless if the array is empty or not, it only returns exists What Am i doing wrong, when the array is populated, it looks like this Array ( [0] => Array ( [id] => 10 ) )

1
  • even an empty array is set. YOu can check with count($userExists) Commented Mar 27, 2014 at 16:41

4 Answers 4

7

Use

if( !empty( $userExists ) ) {
    echo 'exists';
}
else {
    echo 'does not exists';
}

or

if( count( $userExists ) ) {
    echo 'exists';
}
else {
    echo 'does not exists';
}

However is safer to use empty() as if that variable doesn't exists your script will not stop due to exception while count() does.

isset is "not working"* here since this variable is setted (so exists) even if is empty.
So, basically, isset will

Determine if a variable is set and is not NULL.


Last but not least, if you want to know which is "better" for code optimization, I could tell you a little "secret": `count()` doesn't need to traverse the array each time to know how many elements will be there since, internally, it store the elements number (as you can see under), so every call to `count()` function results in `O(1)` complexity.
ZEND_API int zend_hash_num_elements(const HashTable *ht)
{
    IS_CONSISTENT(ht);

    return ht->nNumOfElements;
}

zend_hash_num_elements is called from count() (take a look here)

from php manual



*(not working as you wish/need)

4
  • 1
    It might be worth pointing out that count($userExists) will produce an error if $userExists hasn't been set, whereas empty($userExists) won't. Commented Mar 27, 2014 at 16:45
  • @Styphon As I keep on telling you, it has been firmly established that the variable does exist, otherwise print_r would be throwing an error already and not giving back an empty array. Commented Mar 27, 2014 at 16:46
  • @Styphon: yes, you're right. I was updating my answer :) Commented Mar 27, 2014 at 16:48
  • You wanted to type !empty instead of empty in the first line of the code, didn't you? Commented Mar 27, 2014 at 16:50
1

use as below

if(isset($userExists) && count($userExists) > 0 ){
        echo 'exists';
    }else{
        echo 'does not exists';
    }

OR

You can check if the variable is an array and having some value

if(is_array($userExists) && count($userExists) > 0 ){
    echo 'exists';
}else{
    echo 'does not exists';
}
0
$userExists = array();

The variable exists, and it is set. That's what isset tests for.

What you want is:

if( $userExists) echo "exists";
3
  • @Styphon But I just established that it does exist, and so did OP by using print_r on it. Commented Mar 27, 2014 at 16:43
  • But you're ignoring the else, when it doesn't exist. When it doesn't exist that code will throw an error. That is bad advice to give the OP. Commented Mar 27, 2014 at 16:44
  • @Styphon ... huh? OP used print_r to debug a variable, which means we know it exists. I left off the else echo "doesn't exist" for brevity, since it's not important to the test itself. Commented Mar 27, 2014 at 16:46
0

You do not need an extra check for if!

if($array){
// Will execute only if there is any value inside of the array
}

By using if there is no need checking if any value is available! you are using 'isset' for variables that might not exist like $_GET value or $_SESSION etc.... 'empty' to check a string value

by php documentation empty works only in string and not arrays

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.