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!

4
  • no it is not new ... <?=$var?> is short version ... most using in templates / views etc ... it works if short tags open is enabled see programmers.stackexchange.com/questions/151661/… Commented Jun 23, 2015 at 9: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 Commented Jun 23, 2015 at 9:59
  • 2
    @MeeneshJain Short hand tags have been default since 5.4.0, see: php.net/manual/en/migration54.new-features.php (<?= is now always available, regardless of the short_open_tag php.ini option.) Commented Jun 23, 2015 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. Commented Jun 23, 2015 at 11:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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!
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!
If you use seperate PHP files for your Views (MVC style) this notation is handy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.