hi i am working on a project of my college but my college server admin told me that we don’t use short_open_tag in our code what’s its mean? what is short open tag and what is alternate of it
thanks
Normally you write PHP like so: <?php PHP CODE HERE ?>. However if allow_short_tags directive is enabled you're able to use: <? PHP CODE HERE ?>. Also sort tags provides extra syntax: <?= $var ?> which is equal to <?php echo $var ?>.
Short tags might seem cool but they're not. They causes only more problems. Oh... and IIRC they'll be removed from PHP6.
You can try the tool at:
http://blog.eezgu.com/php-short-open-tag-sorunu-ve-cozumu.html
This is a PHP command line tool that converts short tags to normal PHP tags.
The page is not English but you can click the link in the post and download the tool.
Related
Even the official documentation used to tell us that PHP "short tags" (<? /*...*/ ?>) are "bad". However, since PHP 5.4, the echo variety <?= /*...*/ ?> is permanently enabled regardless of the short_open_tag setting.
What's changed?
Even if they were previously discouraged solely due to the unpredictable nature of whether short_open_tag is enabled on a shared hosting platform, surely that argument doesn't go away just because some subset of hosts will be running PHP 5.4?
Arguably this change to the language doesn't inherently signify a change in the recommendation that we should nonetheless avoid "short tags", but if they've gone to the trouble it would certainly seem like the PHP devs no longer "hate" them so much. Right?
The only logical conclusion I can draw at this time is that there must be some objective rationale for the introduction of this change in PHP 5.4.
What is it?
Short open tags are not always enabled since PHP 5.4. The documentation talks about the short echo tags. Which is a different thing. (short open tags are <? style tags, short echo tags are <?= style tags, for echo-ing).
Then why are they enabled by default now? Well, there are a lot of scripts out there, where it benefits to use <?= $somevar ?> instead of <?php echo $somevar ?>. And because the short echo tags aren't as bad as the short open tags, they chose to always enable the short echo tags. Because now developers (of frameworks and CMS-es) can count on them (or rather, when PHP 5.4 becomes mainstream).
However, the short open tags are still influenced by the short_open_tag setting in your php.ini.
Only short echo tag (<?=) is enabled permanently, not short open tags (<?).
It's because short echo tag is really handy when you're creating HTML templates (or any other view templates) and without that you have to write a lot more (like <?php echo $var; ?> instead of just <?= $var ?>).
Note: Starting with PHP 5.4, short echo tag <?= is always recognized and valid, regardless of the short_open_tag setting.
All that this is saying, is that <?= is always valid, and not <?
The reason is that < ? is used in XML documents and enabling short_open_tags will generate errors in XML codes.
But, < ?=, just like < ?php is not XML open tag and is safe to use.
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 <??
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?
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.
<?= 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:"<?=".
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.
<?= is not one thing. It's actually <? and then = . As #derekerdmann has mentioned, this is an unrecommended configuration.
Give the following a look:
Are PHP short tags acceptable to use?
PHP echo vs PHP short tags
Just to correct all these misguiding answers:
short open tags are not going to be removed or deprecated.
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.
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.
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 <??
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?
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.
<?= 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:"<?=".
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.
<?= is not one thing. It's actually <? and then = . As #derekerdmann has mentioned, this is an unrecommended configuration.
Give the following a look:
Are PHP short tags acceptable to use?
PHP echo vs PHP short tags
Just to correct all these misguiding answers:
short open tags are not going to be removed or deprecated.
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.
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.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Are PHP short tags acceptable to use?
What does “<?=” mean when seen in PHP
is there any difference in using <? ?> to signify a php block, or using <?php ?> ?
if there is not, why would anyone use <?php ?
figure the file extension of .php would give plenty of info about what type of code you are looking at.
The first is called short-open tags and second one is safe open and close tags.
You could enable/disable short open tags in php.ini using short_open_tag setting.
The short tags should be avoided, have a look at:
PHP Short Open Tag: Convenient Shortcut or Short Changing Security?
Servers must be configured to also use <?, so it is considered best practice to use <?php for portability reasons.
From the manual ( http://www.php.net/manual/en/language.basic-syntax.phpmode.php ):
There are four different pairs of
opening and closing tags which can be
used in PHP. Two of those,
and ,
are always available. The other two
are short tags and ASP style tags, and
can be turned on and off from the
php.ini configuration file. As such,
while some people find short tags and
ASP style tags convenient, they are
less portable, and generally not
recommended.
<?php can always be used. <? can only be used if the short_open_tag directive is turned on.
short_open_tag tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. 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"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).
Note: This directive also affects the shorthand <?=, which is identical to <? echo. Use of this shortcut requires short_open_tag to be on.
-- Description of core php.ini directives
As others have mentioned, this directive is often turned off so for portability reasons I prefer using <?php ?>. If this is not an issue, there shouldn't be much difference other than that if the directive is turned on you can also use the <?= shorthand thingy.
I have never personally run into this issue, but support for <? ?> is spotty when moving to different servers. I prefer to just stick to <?php ?> for clarity and consistency.
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.
And also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards.
Always use <?php ?> because <? ?>:
will not work in coming PHP versions
could be mixed with XML definitions. (XML always starts with <?xml ...)
is not enabled on many shared hosting sites.
short tags <? ?> , only work in older php versions.
There is no difference language-wise, but many shop prefer the use of <?php because the simple <? opening tag can be found in XML files, which can lead to confusion for the interpreter.
Edit: I thought this was still an issue: http://terrychay.com/article/short_open_tag.shtml
I just upgraded to php5.3 and when I execute:
php myfile.php
I see the source code for myfile.php.
Any idea why this is happening? I've never seen anything like it before!
In 5.3
short_open_tag
is disabled by default. So if you use
<? ?>
instead of
<?php ?>
it can cause issues in the future (provided you just re enable the flag in php.ini). From what I have read they are still debating removal from 6.
Are you sure you didn't forget the opening PHP tag at the begining of the file ?
ie :
<?php
echo "hello, world!";
(Note I used "full" open tags ; ie "<?php", and not short open tags "<?" -- maybe you used short open tags, and those are disabled ? If I remember correctly, they are disabled by default, in PHP 5.3)
and not :
echo "hello, world!";
(If that doesn't help : can you post a short code sample that reproduces the problem ?)