<?= in PHP development [duplicate] - php

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

Related

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

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.

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 :(

Is using php shorthands bad? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
I'm just learning php and (been learning for about 6 months) and in a tutorial that I'm going through, it's using php shorthands, so when I looked it up on google, I came to this stack overflow question StackOverflow question where one of the popular answers says that shorthands are bad.
I know one of the following comments then suggest that it's not bad but I also remotely remember reading from a php book before that it's not always good to use them. So I'm a bit confused, are they bad or not?
It is generally a bad idea because of portability. All PHP configurations understand the <?php ?> tags, but not all are configured to use <? ?>.
Same thing goes for <? =$variable; ?> for printing.
They are not bad actually, but you can say that it's kinda a bit lazy sort of thing to do for a GOOD TECHNICAL programmer. Why I'm using this word "GOOD TECHNICAL" is because, since they know about the technicalities of PHP, then they should also know whether the shorthands will be of any use or not in the long run, whenever any adjustments is to be made regarding the fine tunings of the PHP Server.
But still, it's one of my views & may be it will not match with others' answers.
I personally never use them because I find it makes my code ugly, hard to read and harder to debug. I'm talking about shorthands like the one-line if statement.
Not every PHP configuration understands short open and ending tags, and not every programmer knows about shorthand notation so this might be a problem if you want to share code at some point. I wouldn't advise using it.
the code "<? ?>" depends on the "php.ini", you should change the state short open tag. While the code "<?php ?>" can run everytime everywhere, without any configuration.
I recommend you to use smarty template.It's perfectly easy to use. And code is beautiful.

<?php vs <? ...Does it matter? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Are PHP short tags acceptable to use?
<?php
//Some code
?>
or
<?
//Some code
?>
I know the first way is the proper way but PHP code isn't validated. So, besides it saving extra typing & bytes, does it matter?
update
Thanks for the replies...
I had no idea they were called short tags (hence why I didn't find the duplicate SO question)
I had no idea there was a specific server configuration option to allow/disallow the short tags.
Thanks again
It does, if anyone ever uses your code on a server where short tags are disabled. I work with several servers where they are. Other than that though, no. Using the short version makes your script less portable though for the above mentioned reason. This may or may not be an issue for you.
This is another issue entirely, but related. If you are trying to generate certain types of files from PHP (XML is the candidate that comes up most often for me) then having short tags can be an issue. For instance, the following causes a PHP syntax error:
<?xml version="1.0" ?>
You must instead write the following on a server that has short tags enabled:
<?php echo '<?xml version "1.0" ?>'; ?>
Gah!
If your project is likely to be deployed on different servers (open source software, for example) it is best to always use <?php
However, if you're like me, and always strive for maximum portability, use <?php even if you don't believe your software will ever leave your server. Most servers have short tags enabled.
However, if they have short tags disabled, and you use them, your PHP will be exposed to the world (if under the document root).
No difference it's just a matter of preference I think, plus why should it matter? it's just another 3 bytes.
Edit:
Forgot to say that you have to enable short hand in php.ini
the closing bracket is not compulsory
On some systems, the default option for the short_open_tags is off, so the latter doesn't work, while the former does, so it can completely break your website if you use the second. Personally, I just like to override the setting and use the second.
<? ?>
are short tags, if short tags are off it won't work.

Categories