What advantages and disadvantages to use? instead of? php? [duplicate]

5

According to the PHP documentation it is possible to enable short_open_tag in the php.ini file, in which you can use:

<? /* jon snow */ ?> 

instead of:

<?php /* jon snow */ ?> 

The questions are:

  • Is it interesting to use <? instead of <?php ?
  • Is there any special functionality when using <? ?> ?
  • What are the advantages and disadvantages of using <? instead of <?php ?
  • Regarding the use of <?= instead of <?php echo , is this only character saving for the code?
asked by anonymous 17.05.2017 / 05:24

1 answer

13
  

Is it interesting to use <? instead of <?php ?

Not currently. And honestly, I believe it never was. The <?php tag exists at least since version 2 of the language (some places mention it since version 1) and since then its use is recommended. The existence of the <? tag means that it was a PHP design error in choosing a tag that was already used by a pattern known to a long time, 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 xml version encoding="UTF-8" content would be treated as PHP code and, if invalid, would generate the previously mentioned syntax error.

Another negative point, and the main one currently, is dependence on enabling the short_open_tag option on the php.ini file. No matter how easy it is to change, the developer will have trouble migrating your application between different servers. For each new server, you should worry about setting the correct configuration options.

Using the <?php tag eliminates both problems at once, so there is no reason to use <? instead of <?php .

Note : When the file is purely PHP, you are advised not to use the ?> closing tag because any white space or line breaks after it will trigger the output buffer of the PHP. Often this is not the behavior expected by the developer and it is a very difficult error to be debugged, especially when working with includes of several files and at the end you need to give the famous header("Location:") , generating the even more famous error <

  

Is there any special functionality when using <? ?> ?

No, none. The functionality of both is the same. The <?php tag only exists to get around the above problems.

  

What are the advantages and disadvantages of using <? instead of <?php ?

Advantage: Enter 3 fewer characters (if your publisher no longer does this for you). Disadvantages: It can generate conflict with other languages, like XML and depends on the configuration of the server. In short: use <?php .

  

As for the use of <?= instead of <?php echo , is this just character saving for the code?

In fact, it is directly related to the <? tag, so much so that for versions earlier than 5.4.0, when the short_open_tag option was disabled, the <?= print tag was also. Seeing that its syntax helps a lot when displaying a PHP variable in the middle of HTML, starting with version 5.4.0 this tag, <?= , is always active, independent of the option in the settings and can be used without fear. p>

<div>Nome: <?= $nome ?></div>

It is worth remembering that the ; character after the variable, in this case, is optional and thus the above code snippet is perfectly valid.

Note: the actual

tag

I think it's important to make it clear that the correct tag is <?php[whitespace] and not just <?php . It does not seem to make a difference, but for example, lower code would not be interpreted correctly and would be shown on the screen as text:

<?phpecho "Olá mundo" ?>
<?php/* comentário */ phpinfo(); ?>

Ifthetagwasonly<?php,itwouldworkperfectly.Butwhatdoesthis[whitespace]mean?Thisisanycharacterthatgeneratesahorizontalorverticalspacing,includingthewhitespaceitself(%w/o%),tab(%w/o%),andlinewrap(%w/o%).

Extra

OthertagsthatexistedinPHP,suchas,\tand\nwerecompletelyremovedfromPHP7.0.0andshouldnotbeusedinanycircumstances.

References

17.05.2017 / 06:16