Awais Qarni
Awais Qarni

Reputation: 18006

What is meant by this php statement

Hello I have read following php statement from a blog but I am unable to understand its meaning. Is it treated as if condition or any thing else? Statement is

<?= ($name== 'abc' || $name== 'def' || $name== 'press') ? 'inner-pagehead' : ''; ?>

Upvotes: 1

Views: 202

Answers (3)

Gajahlemu
Gajahlemu

Reputation: 1263

You can read this as:

if($name=='abc' || $name=='def' || $name=='press') {
  echo 'inner-pagehead';
} else {
  echo '';
}

The <?= is the echo() shortcut syntax, then the (test)?true:false; is a ternary operation

Upvotes: 4

Stephen
Stephen

Reputation: 18964

This is what I would call a poorly written Ternary condition. it basically echos 'inner-pagehead' if the $name variable matches any of the three conditions. I would have done it like this:

<?php
    echo in_array($name, array('abc', 'def', 'press')) ? 'inner-pagehead' : '';
?>

Or, even better:

// somewhere not in the view template
$content = in_array($name, array('abc', 'def', 'press')) ? 'inner-pagehead' : '';

// later, in the view template
<?php echo $content; ?>

Upvotes: 1

roartechs
roartechs

Reputation: 1355

It is saying that if $name is any one of those 3 values ("abc","def", or "press"), then display the text "inner-pagehead". Otherwise, don't display anything.

Upvotes: 1

Related Questions