0

With Magento design files, there seems a very strong preference to using the alternative php syntax (example below). What are the technical advantages? or what is the technical appeal if it is only a style preference?

<div class="page-title title-buttons">
    <h1><?php echo $this->__('Shopping Cart') ?></h1>
    <?php if(!$this->hasError()): ?>
    <ul class="checkout-types">
    <?php foreach ($this->getMethods('top_methods') as $method): ?>
        <?php if ($methodHtml = $this->getMethodHtml($method)): ?>
        <li><?php echo $methodHtml; ?></li>
        <?php endif; ?>
    <?php endforeach; ?>
    </ul>
    <?php endif; ?>
</div>
  • Duplicate of http://stackoverflow.com/questions/1394478/php-echo-vs-openclose-tag – Nate Aug 31 '12 at 13:30
  • possible duplicate of [what's the significant benefit of using alternative colon syntax for if, foreach, while etc in PHP?](http://stackoverflow.com/questions/6451736/whats-the-significant-benefit-of-using-alternative-colon-syntax-for-if-foreach) – Ja͢ck Aug 31 '12 at 15:56

4 Answers4

2

There is no "advantage" or "disadvantage". It comes down to preference and maintainability.

REMEMBER => You should really be SEPARATING PHP and HTML syntax. Keep your PHP and your HTML in separate files - this is for maintainability and lots of other reasons (theres lots of info out there if you search). There is no way you can completely separate the two things... you're always going to need to insert PHP variables into HTML and stuff, but this should be kept to a minimum.

Therefore I prefer to use that alternative syntax in template files in Wordpress, Codeigniter and other things because the way I see it you're inserting PHP into HTML in template files, so lets keep those PHP blocks insular.

i.e. to me:

<?php

if(something){
    // this is a block of PHP
    // if you want to output html in here 
    // it seems right to do
    echo "<div>Here is some HTML</div>";
}

?>

and vice versa:

<div>
    <!-- this is a template file and is written in html -->
    Hello <?=$username?> <!-- inserting PHP into HTML -->
    <?php if(something): ?> <!-- keep it insular - in its own separate blocks, don't mix the two -->
        <button>A button</button>
    <?php endif; ?>
</div>

Again, there is no "technical advantage" its just cleaner and better (IMO). This is mixing PHP and HTML too much:

<div><!-- straight html -->

    <?php
        if(something){
            echo "<button>A Button</button>"; <!-- html output via php -->
        }
    ?>

</div>

And this is just plain terrible:

<div>

    <?php
        if(something){
            ?>
                <button>A Button</button>
                <!-- make your mind up! -->
            <?php
        }
    ?>

</div>
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
0

In short, it looks cleaner, and you can see what each closing brace corresponds to (ish). You can still use <?php if(foo) { ?> and <?php } ?>, but it doesn't looks as clean.

slugonamission
  • 9,562
  • 1
  • 34
  • 41
0

The advantages are really only that the code (specifically the HTML) becomes clear and syntax-highlighted in your code editor, which can help you spot typos/bugs etc. It's also a lot cleaner as you won't have curly braces everywhere and the code can all be indented nicely.

Otherwise it would all come across as a PHP string and your editor won't apply any styling to it as such.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
0

There's any number of reasons the magento team could have done it this way.

Your IDE will have an easier time indenting things if you like to auto format which makes it more readable. You also get to write plain html with whichever quotes you want rather than have to try and put it in "..".

I'm not sure about the hit you receive in performance doing it the alternative way. I will try to figure that out for you, but it won't be much if anything.

Nate
  • 578
  • 2
  • 8