Short Tags

Yii2 is >=5.4 now, so as I see short echo tags were introduced.

Here’s a little question: is there a coding style rule about space after opening tag?

I mean, in this example


<?=Yii::$app->charset; ?>

there’s no space right after “<?=”, yet there is one before “?>”, which seems a little bit inconsistent to me.

Hi Yuri,

I think there is no style for the short echo tag yet. Before 5.4 it was almost forbidden to use it, by guru of the code :). I’ve rechecked all main codestyles and there is no any info for this, only questions in some discussions without unswers yet. So if it will be very popular and frequently asked, then maybe new rule will be added to code styles for this tag. In the oficial documentation we can see an example of this tag without any spaces at all.

PHP Tag <?php usually is on the separate line, so in theory it’s also possible for this <?= tag

<?=

Yii::$app->charset;

?>

*looks ugly I think)

I’m agree with you that version <?=Yii::$app->charset; ?> looks inconsistent and maybe version <?= Yii::$app->charset; ?> should be more correct. But lets see what the community will say. We are witnesses of the new era changes :)

So did I, and also haven’t found anything.

If there are no good reasons to remove this space, I vote for keeping it, because <?=Yii::blush: (nine strange symbols in a row) looks like black magic spell to me :)

Agreed, +1 vote.

Discussion is already on github:

and here is solution:

https://github.com/yiisoft/yii2/commit/c2dabfa78e5dfc8863b7137173e71afe9393187a ;)

Cool, thx guys.

Using short tags per se is not good practice. Firstly it is discouraged on the PHP site. Secondly, it can be turned off in the PHP ini file. Thirdly, the PHP version must be compiled with the short tag option.

Therefore, if a script with short tags is used on a site with the short tags not enabled in the config, or compiled into the interpreter, the script will fail.

http://php.net/manual/en/language.basic-syntax.phptags.php

You’ve missed a lot.

Starting with PHP 5.4, short echo tag <?= is always recognized and valid, regardless of the short_open_tag setting.

Great, now looks better. Thanks yii team for the quick and clean solution. But it still like ‘home made’ style(though maybe the most correct one), I think we should see psr-N soon:-)

I’ve corrected Yii2 code style to reflect latest changes.

So by default is Yii2 going to use the short echo syntax?

In all honestly I actually prefer the full syntax:


<?php echo $i; ?>

instead of (proposed):


<?= $i ?>

The full syntax just seems to makes more sense, if you get what I mean. It kind of encourages the notion of "doing things properly" - which is what Yii is all about.

I use full PHP tags everywhere and have been doing so ever since I started using Yii.

What do you guys think?

For example for me it doesn’t matter. I prefer to use standards in such situations, but there is no many info for this particular moment yet. But my thoughts are - if PHP 5.4 encourages to use this short code way, then it means something (for example that PHP community or at list Rasmus Lerdorf likes it))

I think it makes sense to keep things consistent. In PHP 5.4 only short ECHO tags are always on but short OPEN tags are not. I.e:

<?= $i ?> - this is OK

<? $i = 1 ?> - this is only OK if the ‘short_open_tag’ setting is set to ‘on’

So I don’t really see the point in using a mixture of short and long tags - I think it should just use the full syntax throughout.

Also if Yii2 is going to target 5.3 as a minimum requirement, then using the short echo tag may not work on some servers.

Full syntax sucks )

Compare the length of these:

{{

<?=

<?php

<?php echo

5.4

This thread is all about discussion of the new requirement. Now Yii2 requires php5.4 as a minimum version, so the short code is one of the features that php5.4 encourages to use.

That’s fine, expect my main concern is regarding consistency.

Full PHP tags will still need to be used to create sections of PHP code or variables. The only time where the short syntax will be used is when echoing out variables.

I just don’t see the point of using both formats.

I think you can use the full version of the tag if you prefer :). The most important differents is that it’s not a PHP tag (I mean PHP tags that show begin and end of the php code), its a short echo version. Previously this short echo version was a bad practice becouse it could be blocked with the same config option (if short PHP tags were disabled). Short PHP tags still is a bad practice, and they were flagged bad in coding standards not becouse it’s not beautiful to have full and short versions of the PHP tags in the same file, but due to some issues of the code with the short PHP tags. Now the short echo version works always and it requires some time before it will be reflected in the coding standards, but I have a feeling that it will be rather sooner than later (because we will see a lot of different views before this moment will have a standard).