Is this the right way to host HTML in PHP?

I am new to PHP and most of the time I was an echo in my HTML. Today I found this way to do this, which makes things 1000 times easier:

<?php if(get_field('field_name')){ ?>

   <p>HTML can go here without echo'ing it</p>

<?php } ?>

But is this an acceptable way? It seems to work every time. Thank.

+3
source share
3 answers

This is mainly about how you like reading your code.

An IDE that has autocompletion / code suggestions is best used <?php if (...) { ?>, so the autocompletion / suggestion functions know when to use html and php suggestions for your code. And, of course, it's nice to choose a coding style that is easy to read later.
As far as I know, code highlighting does not work and should not work inside

echo '<div class="c"><h2>...';

:

, , . ,

echo "<div>$var1<h1>$var2</h1>...";

(less writing), , :

<?php 
// <= Some code...
?><div><?php echo $var1; ?><h1><?php echo $var2; ?></h1>...

/ :

<?= ?> , , PHP ( // ) short_open_tag. PHP Since PHP 5.4.0, <?= is always available.
PHP ?
PHP echo vs PHP

<?php ?> PHP: Hypertext Preprocessor, - , echo '<html><here>'; , - , 100000 echo print + <?php ?> , echo, cpu, , .
PHP script?
php.net/manual/en/tokens.php
PHP

:

1,77 .: 1 000 000 $var = rand(0, 1); echo 'Foo Bar '.$var;
1,61 .: 1 000 000 $var = rand(0, 1); ?>Foo Bar <?php echo $var; ?><?php
1.83s.: 1 000 000 $var = rand(0, 1); echo "Foo Bar $var";

, , / . ,
if (...) { echo "something"; }

<?php if (...) { ?>something<?php } ?>

echo "...";, ?>...<?php html . .

, :

, php , ?>text<?php echo "text"; , - -.
: , ?>text<?php echo "text"; .

:

, , , ( PHP docs) , , , , , , echo:

if ( get_field('field_name') ) {
   echo "<p>HTML can go here by echo'ing it</p>";
}

:
php.net/manual/en/control-structures.alternative-syntax.php
php.net/manual/en/language.basic-syntax.phpmode.php
php.net/manual/en/language.types.string.parsing.complex
google .

, alternate :

<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>

UPDATE:

{ } <?php ?> : control-structures.intro language.basic-syntax.phpmode.php , (. ).

+2

. ?> , .

<?php if(get_field('field_name')){ ?>

   <p>HTML can go here without echo'ing it</p>

<?php }

php.ini, (<? <?php)

0

.

-1

All Articles