echo vs html with <?= ?>

I see that all Yii 2.0 code from the documentation looks like this:


<?php

use yii\helpers\Html;

use yii\widgets\LinkPager;

?>

<h1>Countries</h1>

<ul>

<?php foreach ($countries as $country): ?>

    <li>

        <?= Html::encode("{$country->name} ({$country->code})") ?>:

        <?= $country->population ?>

    </li>

<?php endforeach; ?>

</ul>


<?= LinkPager::widget(['pagination' => $pagination]) ?>

why is that prefered to this:


<?php

use yii\helpers\Html;

use yii\helpers\LinkPager;


echo '<h1>Countries</h1>';

echo '<ul>';


foreach($countries as $country)

{

    echo '<li>'.Html::encode("{$country->name} ({$country->code})").'</li>';

}


echo '</ul>';

echo LinkPager::widget(['pagination' => $pagination]);

Or, at least, this:




<?php

use yii\helpers\Html;

use yii\helpers\LinkPager;

?>


<h1>Countries</h1>

<ul>


<?php

foreach($countries as $country)

{

    echo '<li>'.Html::encode("{$country->name} ({$country->code})").'</li>';

}

?>


</ul>

<?php echo LinkPager::widget(['pagination' => $pagination]); ?>

Because the first version is easier to read and maintain by about 1000%.

  • Proper indentation.

  • Syntax highlighting, syntax checking and autocompletion in your IDE/editor.

  • No ugly, complex string concatenations.

Regarding syntax highlighting and auto completion, PhpStorm does it in both options. And that string concatenations are not complex at all, in fact, they’re the most basic.

BUT, you’re right about proper indentation, it looks more clear in that aspect! And that for sure will help on maintain!

Maybe you should take a look at Smarty or other template engine.

It’s not a good idea to keep all HTML in PHP code.In the past,short open tag (like <?=$var ?>)is not recommended.but since PHP 5.3(maybe),code like


<?=$var?>

is recommended(it’s not a short open tag).

If you use STYLE-3,you will find that it’s hard to change the layout of the loop body.And maybe you will be crazy if your loop body contains js code with some single quotes.

Just embrace STYLE-1!

Yes, PHPStorm is the best PHP IDE I have ever worked with but even with PHPStorm some more advanced code intelligence features are unavailable for HTML in PHP string literals.

The concatenation in your example is a simple one but it gets ugly quickly when you have to generate dynamic HTML attributes too.

I maintain a legacy application where all HTML is echoed as PHP string. It has large view files built on complex data structures and it’s a nightmare to modify the code even in cases as simple as wrapping a block of HTML in a <div>.

Please, smarty and other “templating” systems are shit. PHP by it self IS a templating system, no need to add another useless layer on top of it. “But designers wan’t to edit the templates…” - you say? Yeah right, when was the last time, a “designer” produced a usable piece of a template? It’s all dandy and cool, until it gets ugly real fast, and a template makes things only worse. You wan’t to keep other logic out of views? Then just do it, don’t call queries in a view only because you can…