This question already has answers here:
What does '<?=' mean in PHP?
(8 answers)
Closed 7 years ago.
I was upgrading a client's site the other day and noticed that the previous developer had used <?=$foo?> to echo out variable $foo in the code. I know from my days in VBScript that using <%=foo%> works to write out variables to the screen but I had never seen it in PHP nor can I find any documentation on it in Google (part of this is probably because I don't know what this shorthand is technically called).
Is this ok to use or is this deprecated? Does anyone have any further information on this method of echo'ing variables in PHP?
Thanks!
From the manual
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 inline. Otherwise, you
can print it with PHP, for example: '; ?>. Also, if disabled, you must use the long form of
the PHP open tag ().
Note: This directive also affected the shorthand <?= before PHP 5.4.0,
which is identical to <? echo. Use of this shortcut required
short_open_tag to be on. Since PHP 5.4.0, <?= is always available.
asp_tags
Enables the use of ASP-like <% %> tags in addition to the usual <?php ?> tags. This includes the variable-value printing shorthand of <%= $value %>.
Related
This question already has answers here:
How to enable PHP short tags?
(21 answers)
Closed 5 years ago.
im trying to setup my php server to use
<?
echo 'ok';
?>
syntaxis without 'php' inside opening tag. Now my code works like this
<?php
echo 'ok';
?>
How can i achieve this?
I tried to install latest version of php, but it still not working.
ANSWER:
Set short_open_tag=On in your php.ini and restart the server.
CAVEAT:
Short open tags are not used according to PHP coding standard PSR-1 which states:
PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations.
The reason for this as mentioned by #Magnus Eriksson is that there may be situations where you do not have access to edit the php.ini and therefore code that uses short tags will be rendered unusable in those environments. Adhere to the standard for maximum portability!
You need to edit your php.ini file and turn short_open_tags on
short_open_tag = On
This question already has answers here:
Difference between <?php and <?
(7 answers)
Closed 6 years ago.
I've got some old legacy code written in PHP 5.3.
All PHP blocks in this code are like
<? some_php_code_here ?>
and my Apache just ignores them, while works good with
<?php some_php_code_here ?>
Why old code contains invalid blocks, and why this old code works good on old server ? It's really hard to google something with so much special symbols, sorry for stupid question
<? is called a php short tag
Here is what the manual has to say about it
PHP also allows for short open tag <? (which is discouraged since it
is only available if enabled using the short_open_tag php.ini
configuration file directive, or if PHP was configured with the
--enable-short-tags option).
You can enable them at runtime with <?php ini_set('short_open_tag',1) ?>
But if you do so you won't be able to use inline xml like so <?xml ?>
and instead you can use
<?php echo '<?xml version="1.0"?>'; ?>
As a side note I do often use:
<?= "Hello world" ?>
<?= 'foo' ?> is equivalent to <?php echo 'foo' ?> and looks much cleaner in template files in my opinion
Since php version 5.4, <?= is no longer tied to short_open_tag but prior to that it was, so care should be taken if you might be running your code on older php versions
This question already has answers here:
Difference between <?php and <?
(7 answers)
Closed 8 years ago.
Sometimes when I use 'php codes' working with <?php and sometimes working with <? as this code
<?php
if(isset($_GET['url']))
{
echo 'There is a url!';
}
?>
Doesn't work to remove php after and use <?, but sometimes working in another projects!
I want to know there are different between <?php and <? ?
They do the same thing. However, you should use <?php unless you're completely sure that you will have full control over the configuration of your server. I say this because short tags (<?) can be disabled on certain hosts and using short tags can make your code less portable.
The first is a safe open and close tag variation, the second is the so called short-open tag. The second one is not always available, use the first option if it's possible. You could check the availability of short open tags in php.ini, at the short_open_tag.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Are PHP short tags acceptable to use?
How to enable PHP short tags?
I came across some PHP code that is being used on a site. The odd thing is the php code is enclosed like this:
<? ?>
not
<?php ?>
How can I get it to run on my server without the 'php' in there?
take care,
lee
The opening tag <? is known as the short tag. It is not recommended because it requires a certain setting to function on servers. Specifically, you need to enable short_open_tag in your php.ini file.
Configure your server to allow short_open_tags; see the documentation for details.
You can use <? ?> with short_open_tag turned on in php.ini.
Are PHP short tags acceptable to use?
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
What is the difference between the PHP open tags “<?=” and “<?php”/“<?”?
Reference - What does this symbol mean in PHP?
Is there really any difference in writing the syntax like <?php ?> or as <? ?>.
I was under the impression that it really didn't matter as long as the file had an .php extension. But sometimes I seem to run into problems with the syntax.
so whats the lowdown?
The latter is called shorttags.
You can disable those tags in php's configuration, apart from that there is not really a difference.
Because you can disable them, you should not use them, as they can cause lots of unexpected problems when you happen to run your scripts on servers where you can't enable them. Another reason, as noted by Sjoerd in the comment is, that they can cause conflicts when you have an XML header for instance.
If you enable short tags though, you get a shorthand syntax for the echo function: <?= $string ?>
It's enabled using the short_open_tag setting in php.ini. You can read about it here.
Update
As of PHP 5.4.0 alpha1:
<?= is now always available regardless of the short_tags setting
(Rasmus)
The below will work on all servers.
<?php // some logic in here ?>
<?php echo "cat"; ?>
The below will work on servers that have their php.ini properly configured for short tags.
<? // some logic in here ?>
<?= "cat" ?>
one little dangerous thing about shorttags is that if the php.ini is set incorrectly your sourcecode is available on the internet. so