Jump to content

Why does empty returns false?


totoff
 Share

Recommended Posts

hello forum,

i assume i'm overlooking something very obvious, but can't get it. i try to output markup depending on whether a page has been checked as "featured" or not:

<?php $featurednews = $pages->find("featured=1, template=gmk-news, limit=2, sort=-date"); ?>
<?php if (!empty($featurednews)): ?>
output my markup ...
<?php endif; ?>

this outputs my markup even if no page exists with featured=1 (checkbox checked). in other words it returns !empty = true or empty = false although the array must be empty.

sorry if this is a dumb question, still newbie with php :( ...

thanks for your help.

Link to comment
Share on other sites

Hi totoff,

empty() doesn't tell you anything useful for objects, returning always false even for objects with no properties at all (as of PHP 5.0.0). See http://php.net/manual/en/function.empty.php for more details.

$pages->find() always returns a PageArray, which is an object although it behaves very much like a plain PHP array. To see if any items were found, you can use count() method like this:

if($featurednews->count() > 0) {
 // ...
}

Using a PHP function - count($featurednews) - would work just as well.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...