1

I found following code in a project:

<?php echo $this->getToolbarHtml(); ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>

I thought it is ugly and replaced it with this:

echo $this->getToolbarHtml();
// List mode
if($this->getMode() != 'grid') {
    $_iterator = 0;
}

But this rendered the code useless and the page does not load as expected anymore.

If I replace

if($this->getMode() != 'grid') {
    $_iterator = 0;
}

with

if($this->getMode() != 'grid'):
    $_iterator = 0;

Then the site works again. So I thought that it is a ternary operator and changed the logic to this:

if($this->getMode() != 'grid') {
    //$_iterator = 0;
} else {
    $_iterator = 0;
}

But the page still does not load as expected.

What exactly is the : doing?

Black
  • 18,150
  • 39
  • 158
  • 271
  • 1
    That is just the PHP template syntax. It is intended to make templates more readable. – Simon Hessner May 17 '18 at 11:44
  • 3
    [There is and `endif` in the code. PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.](http://php.net/manual/en/control-structures.alternative-syntax.php) – deEr. May 17 '18 at 11:44
  • 6
    this is not about the ternary operator. it's php's alternative syntax – Derek May 17 '18 at 11:44
  • As @Derek pointed, it's php's [Alternative syntax for control structures](http://php.net/manual/en/control-structures.alternative-syntax.php) – Fanie Void May 17 '18 at 11:47
  • 1
    Have an upvote dude. – Heimi May 17 '18 at 11:47
  • Also https://stackoverflow.com/questions/4747761/what-does-mean-in-php and https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Gordon May 17 '18 at 11:48

0 Answers0