Is there any difference between echoing an output line or closing php tags and writing out html code?

I am pretty sure that the answer to this question is: "No at all!" but I will ask anyway. If you have a conditional statement in PHP that calls the html line, is there a difference between the two examples:

<?php if ($output) { ?>
  <h2><?=$output;?></h2>
<?php } ?>

and

<?php if ($output) { echo "<h2>".$output."</h2>"; } ?>
+3
source share
10 answers

The answer is literally "No at all." Consider

<span>
<?php
  echo $myMessage;
?>
</span>

and

<?php
  echo "<span>\n";
  echo $myMessage;
  echo "</span>";
?>

I am going from memory (a few years ago), but at this point the Zend bytecode compiler produces almost identical output; The "literal" HTML was compiled into an echo statement containing text.

+6
source

, . , .

+9

<?= ?> , short_open_tags, .

+5

-,

echo "<h2>".$output."</h2>";

echo '<h2>', $output, '</h2>';

( )

+4

PHP ? , . , .

+3

-,

nano

<?php if ($output) { ?>
  <h2><?=$output;?></h2>
<?php } ?>
+2

. , php, ().

, , , , . html- .

+2

1, HTML - div .

.

+1

? .

+1

I should have known that he launched several different blocks in a loop of 10,000 iterations. Lesson: Do not worry. Write code to read and maintain. Use the MVC pattern ... :)

time : 2.66 microseconds

<? if ($output) { ?>
    <h2><?=$output;?></h2>
<? } ?>

time : 1.65 microseconds

<? if ($output) { echo "<h2>$output</h2>"; } ?>

time : 1.65 microseconds

<? if ($output) { echo "<h2>".$output."</h2>"; } ?>

time : 1.64 microseconds

<? if ($output) { echo '<h2>'.$output.'</h2>'; } ?>
+1
source

Source: https://habr.com/ru/post/1709585/


All Articles