It’s interesting to use <?
instead of <?php
?
Not currently. And honestly, I believe it never was. The tag <?php
exists at least since version 2 of the language (some places cite that since version 1) and since then its use is recommended. The existence of the tag <?
It was kind of a PHP project error to choose a tag that was already used by a long-known pattern, XML. In this case, it was not possible to add raw XML code inside a PHP file because it would give syntax error:
<?xml version="1.0" encoding="UTF-8"?>
<user>
<?
$arr = array("name" => "John", "age" => 99);
foreach ($arr as $field => $value)
{
echo "<" . $field . ">" . $value . "</" . $field . ">";
}
?>
</user>
In this case, the content xml version encoding="UTF-8"
would be treated as PHP code and, being invalid, would generate the previously mentioned syntax error.
Another negative point, and the main one currently, is the dependence on the option enabling short_open_tag
in the archive php.ini
. As much as it’s an easy change to make, the developer will have problems migrating his application between different servers. Each new server should be concerned about setting the correct configuration options.
Tag <?php
eliminates the two problems at once, so there is no reason to use <?
in place of <?php
.
Note: When the file is purely PHP, it is advised not to use the closing tag ?>
, because any blank space or line break after this will trigger the PHP output buffer. Often this is not the behavior expected by the developer and is a very difficult error to be debugged, especially when working with includes
of various files and there at the end you need to give the famous header("Location:")
, generating the even more famous error Cannot Modify header information.
Is there any special functionality when using <? ?>
?
No, none. The functionality of both is the same. The tag <?php
only exists to overcome the above problems.
What advantages and disadvantages to use <?
instead of <?php
?
Advantage: enter 3 characters less (if your editor no longer does this for you). Disadvantages: it can conflict with other languages, such as XML and depends on the server configuration. In short: use <?php
.
As to the use of <?=
in place of <?php echo
, is only about saving characters to code?
In fact, it is directly related to tag <?
, so much so that for versions prior to 5.4.0, when the option short_open_tag
if disabled, the print tag <?=
also was. Seeing that its syntax helps a lot when displaying a PHP variable in the middle of HTML, starting from version 5.4.0 this tag, <?=
, is always active, independent of the option in the settings and can be used without fear.
<div>Nome: <?= $nome ?></div>
It is worth remembering that the character ;
after the variable in this case is optional and thus the code snippet above is perfectly valid.
Note: the real tag
I believe it is important to make clear that the correct tag is <?php[whitespace]
and not just <?php
. It doesn’t seem to make a difference, but, for example, the Abas code would not be interpreted correctly and would be shown on the screen as text:
<?phpecho "Olá mundo" ?>
<?php/* comentário */ phpinfo(); ?>
If the tag was only <?php
, would work perfectly. But what that [whitespace]
means? It is any character that generates a horizontal or vertical spacing, including the white space itself (
), the tab (\t
) and line break (\n
).
Extra
Other tags that existed in PHP, such as <%, %>
, <%=
and <script language="php">
have been removed completely from PHP 7.0.0 and should not be used in any circumstances.
References
I believe it is not duplicated because there the main discussion is between
<?php
and<?=
, while here it was requested between<?php
and<?
. They’re closely related, but I don’t think it’s duplicate. There, for example, is not dealt with the similarities between the tags and disadvantages advantages of each.– Woss
@Danielomine the question is not what the difference is, but the advantages and disadvantages of the use. Perhaps it would fit more as related than duplicated. abs
– viana
When we mark as duplicate the questions may be different but the answers identical. This is the case here. All that has been answered here is the same as in the other.
– Daniel Omine