1

I am a programmer with about 5 years of experience in PHP but very new to Laravel (or MVCs) and I just started learning Laravel 5 thorough the Laracasts.

While going through the video Passing Data Into Views, I came across the code <?= $name; ?> which performed the same task as <?php echo $name; ?> would do. Have I missed this usage all this time in PHP or is it something new? Or is it specific to Laravel?

Also, is it considered a good practice to use this syntax to print rather than using echo?

EDIT: I know what it does, I tested it. I was curious as to its usage and/or practice. Thanks for all the answers!

Acharya Anurag
  • 671
  • 6
  • 23
  • no it is not new ... =$var?> is short version ... most using in templates / views etc ... it works if short tags open is enabled see http://programmers.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php – donald123 Jun 23 '15 at 09:51
  • this is a setting in php.in. it is by default disabled and needed to be enabled by code or in php.in file – Meenesh Jain Jun 23 '15 at 09:59
  • 2
    @MeeneshJain Short hand tags have been default since 5.4.0, see: http://php.net/manual/en/migration54.new-features.php (= is now always available, regardless of the short_open_tag php.ini option.) – EJTH Jun 23 '15 at 10:08
  • Thank you all. I found out what it did by testing it, I just saw it for the first time today and got a bit confused. – Acharya Anurag Jun 23 '15 at 11:44

1 Answers1

3

The <?= $var; ?> is a short tag and is available for some time. In MVC's it is quite common to use and it is not bad, but not always the right choice. Some servers doesn't allow short tags at all. You can change this settings by enabling it in .htaccess, but until you try, you are not certain it will work.

<IfModule mod_php5.c>
  php_value short_open_tag 1
</IfModule>

Most of the times, you won't have to do anything to use short tags.

Sietse
  • 623
  • 1
  • 8
  • 23
  • Thank you. I guess there is no harm in using them in most cases. But I kind of prefer `echo` so I think I'll stick to that! – Acharya Anurag Jun 23 '15 at 11:45
  • 1
    Using echo is always safe. As EJTH mentioned it is enabled by default in PHP 5.4. For code running on a lower PHP version, you have one less thing to worry about! – Sietse Jun 23 '15 at 11:50
  • If you use seperate PHP files for your Views (MVC style) this notation is handy. – Roel Nov 03 '15 at 15:59