11

Sorry for the silly question, but I ran across code that used:

<?=$MAP_OBJECT->printOnLoad();?>
<?=$MAP_OBJECT->printMap();?>
<?=$MAP_OBJECT->printSidebar();?>

Is there anything special about <?= over <?php or just plain <??

Gumbo
  • 594,236
  • 102
  • 740
  • 814
joslinm
  • 7,212
  • 6
  • 46
  • 66

8 Answers8

18

They are shorthand of <?php echo .... ?> known as short tags. You should avoid using them because:

  • They seem to be turned off on some servers
  • They can get you in trouble in terms of security
  • They can create conflict with processing instructions like <?xml ... ?>

Therefore you should never use them again, also have a look at:

PHP Short Open Tag: Convenient Shortcut or Short Changing Security?

Sarfraz
  • 355,543
  • 70
  • 511
  • 562
  • 6
    The `short_open_tag` ini option is **not** deprecated. – salathe Jun 11 '10 at 08:10
  • 2
    @salathe: See this with respect to your comment: http://stackoverflow.com/questions/3008614/should-short-tags-be-avoided-as-much-as-possible/3008626#3008626 – Sarfraz Jun 12 '10 at 09:14
  • who is this Dorward guy? is he PHP core team member? Same for the Priebsch one. They are nobody. you cannot use it as a proof. – Your Common Sense Jun 12 '10 at 09:48
  • 2
    @Col. Shrapnel: I would *naturally end this with you* by saying you should provide your comments there as well so that things get cleared up for me as well as him. Also I don't know like Dorwand how expert you are at PHP so your argument cancels your comments for me too. – Sarfraz Jun 12 '10 at 09:51
  • 2
    No, you are wrong. one who states any statement, ought to bring positive proof. You just have no proof beside empty blab of some ignorant persons. And not a single official word from PHP team. My proof is just absence of such a word. That's logic. Simple enough even for you – Your Common Sense Jun 12 '10 at 09:59
  • @Sarfraz Ahmed: Col. Shrapnel is right. You have to proof your statements. Because without a proof they are just assumptions. – Gumbo Jun 12 '10 at 10:28
  • 3
    @Col. Shrapnel, @Gumbo: I already provided a proof in my answer. You must have seen the slide number 47 at : http://www.slideshare.net/thinkphp/php-53-and-php-6-a-look-ahead – Sarfraz Jun 12 '10 at 11:15
  • I have already asked you: who is that Priebsch guy? Does he any right to speak for the PHP core team? The answer is **no**. – Your Common Sense Jun 12 '10 at 11:35
  • @Gumbo: I have also updated my answer, you see i don't involve myself in arguments with that guy for obvious reasons but because you favored him, i need to respond to you only not him and never him in fact. – Sarfraz Jun 12 '10 at 11:43
  • There is a long discussion about the possible deprecation of short tags here: http://www.mail-archive.com/internals@lists.php.net/msg41839.html – nico Jun 12 '10 at 11:51
  • @Col. Shrapnel: You missed third point right? It says **We will not add " – Sarfraz Jun 12 '10 at 11:54
  • @Sarfraz Ahmed: The slides are not a proof in my opinion. The author of that slides, “Ajax ajax”, is not an authority I trust. You should only cite sources that are trustworthy concerning information about PHP’s future development. – Gumbo Jun 12 '10 at 11:54
  • @Sarfraz Ahmed: The report says they will remove ` – Gumbo Jun 12 '10 at 11:56
  • @Gumbo: I again updated my answer and said in my comment to see the updated answer please, I believe confusion here created is between `` and ` – Sarfraz Jun 12 '10 at 11:56
  • @Sarfraz Ahmed: ` – Gumbo Jun 12 '10 at 12:01
  • 4
    @Sarfraz Ahmed: Now I'm confused, seeing as YOU were the one who changed the question title and body from `=` to ` – TooManyCooks Jun 12 '10 at 12:02
  • 1
    @TooManyCooks: Because that's what i believe he meant and also because of confustion betweeen `=` and ` – Sarfraz Jun 12 '10 at 12:05
  • @Sarfraz Ahmed: How about quoting this: “Which is one of the reasons we decided not to remove them in PHP 6.” – Rasmus Lerdorf, http://www.mail-archive.com/internals@lists.php.net/msg41853.html – Gumbo Jun 12 '10 at 12:12
15

Rather than talking about whether short_open_tags is deprecated or not we should talk about the advantages and disadvantages when using short open tags:

Advantages

Using the short open tags <? along with <?= is shorter and probably easier to write than the standard opening tags <?php and <?php echo respectively. That’s quite handy when using PHP directly in a template. (That’s probably also the reason why PHP has an alternative syntax for control structures.)

Disadvantages

Requires a specific configuration

When using short open tags you are required to have short_open_tags enabled. If you or your web hosting provider decides to disable short_open_tags, your application probably won’t work any more and you can have some serious security issues. Because if short_open_tags is disabled, only the standard opening tags <?php are recognized and everything inside short opening tags is treated as plain text. (See also the blog post referenced in Sarfraz Ahmed’s answer.)

This requirement makes your PHP application less portable if you aim to write applications that are not just for you. That’a also why many recommend not to use short open tags (including the PHP manual):

Note: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

As of PHP 5.4 <?= is always available, regardless of the short_open_tags option. <? on the other hand requires the option to be enabled.

Conflicts with XML processing instructions

Another issue is when using XML processing instructions like <?xml … ?>. When short_open_tags is enabled you cannot use them directly in your code but need to use PHP to output it:

If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>.

Otherwise PHP will choke on the xml in <?xml.

Now some last words about the deprecation: Currently short_open_tags is not deprecated. Otherwise the manual would state that explicitly. Additionally, Rasmus Lerdorf, inventor of PHP, wrote in a reply on the question “Is it true that short_open_tag is deprecated in PHP 6?” on the internals mailing list that there were several reasons not to remove short_open_tags in PHP 6:

Which is one of the reasons we decided not to remove them in PHP 6.

Community
  • 1
  • 1
Gumbo
  • 594,236
  • 102
  • 740
  • 814
12

<?= is a short tag that is pretty much equivalent to:

<?php echo 

As of PHP 5.4, this syntax is always available, but in earlier versions of PHP, it needs short_open_tag enabled. As for how to search for it on Stack Overflow, try code:"<?=".

Ry-
  • 199,309
  • 51
  • 404
  • 420
  • 2
    Plus to be able to use it, it must be set in php.ini file, so you may experience problems on some servers where this short tag is turned off. – Zefiryn May 21 '12 at 21:32
  • 1
    @Zefiryn: It's also worth noting that it isn't part of `short_tags` as of PHP 5.4. Hopefully people won't have to worry in the future :) – Ry- May 21 '12 at 21:33
7

It's a shorthand for <?php echo $MAP_OBJECT->printOnLoad(); ?>. Simpler way to write it when you're making PHP-based templates and things.

Be careful, though. My understanding (though I've never run into it myself) is that the shorthand version can be disabled on some servers.

derekerdmann
  • 16,379
  • 9
  • 67
  • 104
1

<?= is not one thing. It's actually <? and then = . As @derekerdmann has mentioned, this is an unrecommended configuration.

Give the following a look:

Community
  • 1
  • 1
Babiker
  • 16,998
  • 26
  • 70
  • 119
1

Just to correct all these misguiding answers:

short open tags are not going to be removed or deprecated.

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
1

Inside short tag you cannot write like this .

<?=
$number = "5";
$sum = 15 + "5";
?>

because it will print only the first output as 5.

Inside open tag you can write like this .

<?php
 echo $number ="5";
echo $sum = 15+"5";
?>

It will print both 5 and 20

its all about syntax.

sradha
  • 2,126
  • 22
  • 43
0

adding this due to duplicate questions

aka shortags is an alternative tag for php but works only on servers that have it enabled.

It allows you to write echo like this

However it is not recommended to use them and have been suggested to be removed or deprecated in php5.4+ along with register globals, safe mode and magic quotes etc.

So though you might use them, they are not recommended. 1. They are not portable since servers must have them enabled. 2. They can lead to spaghetti code easily since you can easily integrate html in your file. 3. Special methods have to be used when mixing them with xml declaration

They are however great for making templates along with other shorthand notations for loops and conditional checks.

frostymarvelous
  • 2,567
  • 27
  • 40
  • All very true (except they are not planning on being deprecated), but it's been [pretty well covered](http://stackoverflow.com/questions/3019696/what-is-the-difference-between-the-php-open-tags-and-php/3028708#3028708) – Wesley Murch Jun 23 '11 at 03:40
  • Ah. I guess I was swept along with the currents. Thanks. Btw, I only posted this because I typed the whole thing out and then the question was closed. – frostymarvelous Jun 23 '11 at 03:51