Trey
Trey

Reputation: 394

Php if statement checking value of sometime undefined object

I am using smarty template system and am trying to do this for an if statement

{if isset($left_column_size) && !empty($left_column_size) && $category->name ne "Health"}

This works but if no category object is set for the given page I get php notices:
Undefined index: category
Trying to get property of non-object

What I really want is something as follows

{if isset($left_column_size) && !empty($left_column_size) {if isset($category)}&& $category->name ne "Health"{/if}}

If I try this I get the error Smarty Compiler: Syntax error in template code too many shorthand attributes. Does anyone know what the right thing to do here to get rid of those php error notices is? Thanks

Upvotes: 0

Views: 74

Answers (2)

Trey
Trey

Reputation: 394

I ended up solving this by doing:

{if isset($left_column_size) && !empty($left_column_size) && 
( empty($category) || (!empty($category) &&$category->name ne "Health Topics"))}

Upvotes: 0

mjohns
mjohns

Reputation: 369

It looks like you were on the right track. Try something along these lines

if(!empty($left_column_size) &&
  ((!empty($category) && $category->name ne "Health") 
    || empty($category))

Upvotes: 1

Related Questions