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)