Using <?= instead of echo in PHP (Laravel 5)? [duplicate] - php

This question already has answers here:
PHP echo vs PHP short echo tags
(6 answers)
What does '<?=' mean in PHP?
(8 answers)
Closed 7 years ago.
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!

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.

Related

Perl: How to do a PHP-style include? [duplicate]

This question already has answers here:
Is there any way to "auto-use" certain modules everytime I write a script?
(2 answers)
How can I export a list of modules with my own module?
(1 answer)
Closed 7 years ago.
In a PHP include, code is parsed as if it was written right into the source file. You get the same result as if you literally copy and pasted file A into file B.
I'm looking for a way to do this in Perl. The number of libraries I have to 'use' in every script is getting annoyingly large, and especially annoying is having to use v5.14 just so I can have the say function. I'm actually looking up how to alter the interpreter to always use the latest version of Perl at this moment, compatibility with the rest of the world be damned, but this won't solve the rest of my list of includes.
Edit: none of the linked answers answer the question.
The equivalent of PHP's include is do EXPR. There is also require EXPR, which is like require_once, and use, which will also call import on the package.
However that is probably not what you want. If you have a lot of .pl scripts without packages, you are dealing with legacy code. You need to be carefull what you require and include where.

<?= in PHP development [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does this symbol mean in PHP <?=
Reference - What does this symbol mean in PHP?
In some coding I've found, I've seen the author use <?= and ?> in his code. I'm wondering if this is some fancy PHP or another language. I'm eager to know the answer as I would love to learn off of this code. I believe it could be the Fuel PHP framework but I am not sure as there is no documentation for it. Thanks.
An example of it's use:
<?=SITEROOT?>
They are called short tags, but you should avoid using it as much as you can, because the short tags can be set to Off and then your script wont work, so use <?php tags,
http://php.net/manual/en/ini.core.php
It's just an alternate way to write the <?php ?> tags, think of it as a synonym of <?php echo SITEROOT; ?>.
I believe it is a configuration item, some servers have it turned on, others don't; so it isn't 100% portable, other then that though, it is standard php functionality

What are the arguments against the use of the "<?="? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
The <?= is one of the very few elegant things about PHP, IMO. Yet, there are people that deliberately avoid it (in favor of the much longer <?php echo). Why would they do that?
<?= is easier to use but some servers don't support short tags. Therefore, if you ever run into a server that doesn't support them, you need to replace all tags.
A more elaborate answer is already given: Are PHP short tags acceptable to use?
Because the feature isn't enabled by default in PHP, so if someone else uses the code who doesn't, the code breaks.
The problem is that not all servers support short tags
If you are developing an application for an controlled environment (for example, it will run only on your company server), then I don't see any problems with short tags
But, if it'll be a redistributable code, them you should open all tags explicitly <?php echo ?>
Many servers has got that <? "shortform" turned off.
The only sure way to have your php executed is using the <?php form. so you have to use <?php echo in code you're going to distribute or reuse.
Because by default servers aren't set up with php short-tag support, its something that needs to be toggled. if for some reason the server does not have short-tag support turned on, your code will error out. better to just add a couple characters, and aviod potential problems.
EDIT
search before posting: Are PHP short tags acceptable to use?

PHP opening tags question [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
I saw some people use
<?=
instead of
<?php echo
It does make all those
<td something><?php echo $result;?></td>
<td something><?php echo $result2;?></td>
shorter and easy to read (to me at least) but my question is: is it desirable to use this syntax? Or is it deprecated/discouraged/simply wrong?
Thanks!
Supposedly, some people have short tags disabled on their server, so this won't work.
It's best practice to use <?php echo $var; ?> instead, but some will argue that it's more readable to use short tags and that having them disabled is rare.
For best results, avoid them.
EDIT: Apparently PHP 5.4 will allow you to use <?= syntax regardless of server configuration (I was not aware). In that case, I still say - avoid it if you care about portability and different environments that may not be running 5.4, or may have short tags disabled.
<? ?> for PHP blocks is deprecated, but the PHP manual says <?= ?> is going to stick around (and from PHP 5.4 upwards, it'll be on always, even if short tags are off/removed). So, not deprecated, and common practice.
It can't be deprecated because sometimes ago it was available only with switched on short tags, but will be available always since PHP 5.4
As of me, <?= is more readable, but you (or your team) should choose your own style
<?= is known as short tags, PHP framework like CI support this and you can even add support for these in your php.ini files. That said, you should not use this tag at all. This is not so commonly encouraged and now a standard PHP tag with 5.4, using non standard tgas reduces the portability of your scripts. Time to correct my self, this is standard tag, I was stuck with 5.3 :(

Closing and opening the php tag [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: Opening/closing tags & performance?
this is probably a stupid question, but being extremely perfectionist i thought I'd ask just for curiosity:
Is there a perfomance issue if opening and closing the "php" tag to many times ?
I mean, is there a consequence performance speaking using this
<?php
$track_id = 418; //default
?>
<?php include('includes/popup_biography.php'); ?>
instead of this :
<?php
$track_id = 418; //default
include('includes/popup_biography.php');
?>
I don't know of any performance issue. There might be a very minor performance issue due to the emission of what is between the two statements, but I think it would be unnoticeable.
However, you should not break out of php for a different reason: this emission prevents you from sending any other headers with your request. I think that breaking out of php to print is an antiquated notion and should be avoided entirely. If you are going to do it, reserve it for printing the results of your logic (all done long before any printing, probably in a different file).
Take a look at PHP: Opening/closing tags & performance?
The performance differences are so minuscule that you shouldn't fret over it whatsoever.

Categories