Starting to use PHP. A formatting question

I’ve been working with PHP a bit more lately and I have a question about formatting.

So take this chunk of code for example:

<?php
   $menu = new WP_Query(array('post_type'=>'menu'));
   while($menu->have_posts()) : $menu->the_post();
?>
<h1><?php the_field('menu-title'); ?></h1>
<h2><?php the_field('item-title'); ?></h2>
<p><?php the_field('item-description'); ?></p>
<p><?php the_field('item-price'); ?></p>
<?php endwhile; ?>

How would you format the above? Do you typically keep all php on one line or split it up, e.g.

<?php endwhile; ?>

or

<?php
   endwhile(); 
?>

Is there a right way to do this, or is it mostly preference?

I don’t know much PHP, but for WIW, it looks inefficient to have all those opening and closing PHP parts. You can do this with just one.

I see. That comes down the including or separating the HTML from the PHP, doesn’t it? What’s the preferred option?

I would probably do it like so:

(Note that I used indentation to make the while loop visually obvious, and I removed the statement-ending semi-colons, because when there’s just one statement between PHP tags, they aren’t necessary.)

<!-- Doesn't seem like this should be in the template, but this is WP, so maybe there's no choice? -->
<?php $menu = new WP_Query(array('post_type' => 'menu')) ?>

<?php while ($menu->have_posts()): ?>
    <?php $menu->the_post() ?>
    <h1><?php the_field('menu-title') ?></h1>
    <h2><?php the_field('item-title') ?></h2>
    <p><?php the_field('item-description') ?></p>
    <p><?php the_field('item-price') ?></p>
<?php endwhile ?>

…the root of all evil. :wink:

One vs many PHP tags, it turns out, doesn’t have a significant impact.

It’s actually considered bad practice to output HTML code from PHP. So the OP has gone the most efficient route. The down side is, of course, legibility which suffers when having lots of opening and closing PHP tags in one code snippet. A balance should therefore be taken between legibility and efficiency (which is up to the developer).

But in regards to the OP, it is just preference. You may wish to keep blocks of PHP code on separate lines to its opening and closing tags and put single PHP lines inline why their opening and closing tags (which is what I do), or go another route. Choose the style that looks the most legible to you, since you are the once who is developing it!

I like that formatting, looks clean and logical. Thanks.

Thanks for the replies everyone! :smile:

Maybe you can use a simple template-system like twig → http://twig.sensiolabs.org/

PS: I have created a even more simple php-wrapper for twig, take a look :wink:

→ Twig-Wrapper: https://github.com/voku/twig-wrapper
→ Demo-Code: github.com/voku/twig-wrapper-example

Good responses so far, I would only go a little bit further than @Jeff_Mott and use php short tags - there are very few servers where they are turned off:

<? $menu = new WP_Query(array('post_type' => 'menu')) ?>

<? while ($menu->have_posts()): ?>
    <? $menu->the_post() ?>
    <h1><? the_field('menu-title') ?></h1>
    <h2><? the_field('item-title') ?></h2>
    <p><? the_field('item-description') ?></p>
    <p><? the_field('item-price') ?></p>
<? endwhile ?>

But note that your functions like the_field() have to actually echo() the content for this to work. It is generally better practice not to echo stuff in your functions (unless they are purely template functions) so if you have a function that grabs a field value it is better if it returns a value - then you can use this function in other contexts as well. Then you would need this code in your template:

<? $menu = new WP_Query(array('post_type' => 'menu')) ?>

<? while ($menu->have_posts()): ?>
    <? echo $menu->the_post() ?>
    <h1><? echo the_field('menu-title') ?></h1>
    <h2><? echo the_field('item-title') ?></h2>
    <p><? echo the_field('item-description') ?></p>
    <p><? echo the_field('item-price') ?></p>
<? endwhile ?>

This is more to type but the remedy is the pretty “short echo tag” which is <?=... ?>. The short echo tag is always available since php 5.4 so I would use this code to be compatible everywhere:

<?php $menu = new WP_Query(array('post_type' => 'menu')) ?>

<?php while ($menu->have_posts()): ?>
    <?=$menu->the_post() ?>
    <h1><?=the_field('menu-title') ?></h1>
    <h2><?=the_field('item-title') ?></h2>
    <p><?=the_field('item-description') ?></p>
    <p><?=the_field('item-price') ?></p>
<?php endwhile ?>
2 Likes

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)