Difference between php <?php tags and <?=

Asked

Viewed 10,518 times

26

Well I’m learning Laravel and I was left with a doubt following some tutorials.

At certain moments within the view the tag is used : <?php foreach($produtos as $p): ?> and when will I get the data <?= $p->nome ?>.

Is there any difference between <?php and <?=?

6 answers

32


Yes, there is.

The tag <?php is the default for PHP file openings, unless enabled Short Tag, that lets you open PHP with <? (which may conflict with XML instructions)

Already the tag <?= It’s quite simple her purpose:

Note: This directive also affects the abbreviated form <?= prior to PHP > 5.4.0, which is the same as <? echo. For use of this abbreviation is required short_open_tag to be active. Provided PHP 5.4.0, <?= is always available.

Instead of using <?php echo $variable; ?> just use <?=$variable?>

Code affines become cleaner and readable.

I must point out that this is not from Laravel as described in the question, but from PHP.

Considerations

As he is studying Laravel and has taken over in this matter of passing values to views recommend to take advantage and study the Blade

In Blade give output of variables only using:

{{ $variable }}

Which is equivalent to:

echo $variable;

A lot simpler, right?

  • 1

    +1 for quoting Lade.

  • Blade is one of the next topics of the book I’m following. Vlw by tip.

  • The twig is also top. But it turns PHP into practically another language (roughly speaking)

  • @juniorb2ss complemented his answer with a small detail, to facilitate the concentration of other questions duplicated by the site.

14

Yes, <?php is the most correct option to open the php tag, because the server is not always with open_short_tag enabled, usually what happens is the php code to be printed on the screen and not executed.

Starting from version 5.4 <?= is back to standard.

Should I or should I not wear <?= ? the best answer is this flowchart taken from programmers

inserir a descrição da imagem aqui

7

The syntax <?= ?> is a shortcut to this:

<?php echo        ; ?>

Before PHP 5.4 the option short tags should be enabled to use this shortcut. In PHP 5.4 this option is always available.

So much so that this syntax is specified in echo documentation.

Example:

<p>Meu nome é <?=$nome?></p>

amounts to

<p>Meu nome é <?php echo $nome; ?></p>

Before this change of PHP 5.4, the use of echo implicit was problematic as the short opening tags <? ?>are confused with XML instructions, and usually hosts in general keep this option off.

As the sign of = would serve in theory for disambiguation in these cases, it was decided to enable this option separately from the configuration of short tags.

7

Yes, when you use <?=... it’s like I’m doing<?php echo... is just an abbreviated way to keep the code cleaner.

4

There is also asp_tags <%%>, which was created to facilitate ASP programmers migrating to PHP 15 years ago.

There were also other purposes such as creating a template system compatible with ASP and PHP.

In practice, it never happened. At least not as expected.

There’s another tag I’ve never particularly seen used <script language="php">.

In PHP 7.0.0, the ASP tags and script have been removed.

2

Just to complement with information, one of the above-mentioned forms is called by PHP alternative syntax, or alternative syntax.

It serves to simplify the were of writing a control or repeat structure, in case you have to "mix" PHP with HTML.

Example:

<?php foreach($produtos as $p): ?>
   <p> <?= $p->nome ?></p>
<?php endforeach ?>

Another example of alternative syntax (without mixing with HTML):

if ($produtos->isEmpty()) :
    echo "Nenhum produto encontrado";
endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.