Domanda

I'm new to PHP, and most of the time I have been 'echo'ing my HTML. Today, I found this way of doing it, 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 it an accepted way? It seems to work every time. Thanks.

È stato utile?

Soluzione

It's mostly about how you like to read your code.

With IDE that has autocomplete/code suggestions it is best to use <?php if (...) { ?> so that autocompletion/suggestion features know when to use html and when php suggestions for your code. And of course it's nice to select coding style that is easy to read later.
As far as I know code highlight wont work, and should not work, inside

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

Different coding styles:

Other than what already mentioned there is no big differences. For example using

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

for inserting variables it is easier (less writing), code has smaller footprint and easier to read than this:

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

Short open tags / why not use:

There is also <?= ?> shortcut available but I have not used it since it requires PHP configuration (which is not always available/changeable/writable) value short_open_tag to be set. However PHP manual says Since PHP 5.4.0, <?= is always available.
See Are PHP short tags acceptable to use?
and PHP echo vs PHP short tags

And what's found under hood

Everything inside <?php ?> is parsed by PHP: Hypertext Preprocessor so someone may say that it takes much more resources to use echo '<html><here>'; but this is again wrong place to do optimization if you don't have over 100000 echo's or print's + starting <?php and ending ?> takes few instructions too as does echo so they are almost same when counting cpu instructions, at least with my installation.
See How exactly is a PHP script executed?
and php.net/manual/en/tokens.php
and PHP Compiler Internals slides by Sebastian Bergmann

Some tests about performance:

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

I really think that it is most about how you like to write/read your code. Sometimes it's better to write and later read
if (...) { echo "something"; }
and sometimes
<?php if (...) { ?>something<?php } ?>

Personally I prefer using echo "..."; when there is a lot of variables or only small piece of text printed out and ?>...<?php when outputting large chuncks of html or long text with only few variables. This makes code easier to read.

About accelerators mentioned in comments:

If I got it right, php does lexical analysis first and passes results to accelerator, from this point of view ?>text<?php and echo "text"; has big difference but it still does not change performance within normal website projects.
More clear: Accelerator itself does improve performance but about using ?>text<?php or echo "text"; does not make big difference.

Final answer:

Yes, it is widely accepted and used but I think (at least did not find anything clear from PHP docs) it is still undocumented way, However, as you already know it is not only way. Basically it does just same job as if using echo:

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

While trying to find official docs I have read these pages:
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
used google, and searched some forums.

It seems that clearly documented alternate way is not used so often:

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

UPDATE:

Official documentation about separating { and } by <?php and ?> within conditional statements is here: control-structures.intro and language.basic-syntax.phpmode.php together documented it, however there is demonstration only for alternate way to do it (see above).

Altri suggerimenti

It is. But it has also become a standard to omit the ending ?> in every file, so there is no trailing whitespace or line ending in the output.

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

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

<?php }

If enabled in your php.ini, you can also use short tags (<? instead of <?php)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top