I have some PHP experience, but hardly any WP theme writing experience…I’m just starting the journey.

All of the themes I have looked at so far in the .org repository have code that looks like this:

<?php if (condition) { ?>
..some html
<?php } else { ?>
..some other html
<?php } ?>

instead of:

<?php
if (condition) {
  echo ..some html
}
else {
  echo ..some other html
}
?>

I can see that when using an editor like notepad++ we’d rather have the html “naked” and not in an echo statement, to give the editor the ability to color-code and match up HTML tags with their ending tags, etc.

But I see a lot of templates that have a TON of PHP code and very little HTML — and they still wrap PHP tags around every line of code. To me, that makes it hard to read.

Is there a technical reason for that, or just a preference on the part of the developer?

2 s
2

As always, people copy paste whatever they learned from and IIRC the first style is the coding style of wordpress core.

As you said yourself, when there is a big block of generated HTML it is easier to look at (and balance tags) under the first style, but pragmatic people will use the second whenever that is not the case.

It is really just coding style preference nothing technical behind it.

Leave a Reply

Your email address will not be published. Required fields are marked *