Difference between php and php tags?

20

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

At certain times within 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 <?= ?

    
asked by anonymous 30.09.2015 / 15:27

6 answers

23

Yes, there is.

The <?php tag is the default for PHP file openings unless Short Tag , which allows you to open PHP with <? (which may conflict with XML statements)

The <?= tag is quite simple:

  

Note: This directive also affects the abbreviated form <?= before PHP > 5.4.0 , which is the same as <? echo . For use of this abbreviation it is   short_open_tag is active. From PHP 5.4.0 , <?= is always available.

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

Related to the code getting cleaner and readable.

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

Considerations

As you are studying Laravel and you have barred yourself from passing on values to views recommend taking advantage of and studying the Blade

In the blade output variables only using:

{{ $variable }}

Which is equivalent to:

echo $variable;

Much simpler, right?

    
30.09.2015 / 15:34
10

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

As of version 5.4 <?= returned to default.

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

    
30.09.2015 / 15:32
5

Yes, when you use <?=... it's like doing <?php echo... is just a shorthand way to keep the code cleaner.

    
30.09.2015 / 15:29
4

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

<?php echo        ; ?>

Prior to PHP 5.4 the short tags option should be enabled to be able 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>

is equivalent to

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

Prior to this change in PHP 5.4, the use of% implicit% was problematic, since the short opening tags echo were confused with XML statements, and usually the hosting usually keeps this option off.

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

    
10.12.2015 / 18:49
3

There is also asp_tags <%%> , which was created to make it easier for ASP programmers to migrate to PHP 15 years ago.

There were other purposes as well, in order to create a system of ASP and PHP compatible templates.

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

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

In PHP 7.0.0, the ASP and script tags were removed.

    
01.10.2015 / 05:49
1

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

It is meant to be simpler to write a control or repetition 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 (not mixed with HTML):

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