2952 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 200+ members
  • Has sold $125,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+12 more
DDStudios
says

I am learning wordpress right now and I’ve been suggested to write the code a certain way.

I trust the guy but I wanted to know what you guys think, what is better? What does user prefer?

So here’s an example :

This code :

<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

Compared to this one :

<?php if (have_posts()) {
    while (have_posts()) { 

    the_post(); ?> 

Do you guys always close your php tag even if there is more PHP coming?

Thanks!

444 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in United Kingdom
TahaH-Studio
says

For template files I use short tags eg. if (have_posts()) : while (have_posts()) : the_post();. Its just because its more organised in some way. But in functions.php and other fully php files I use the normal php codeing

1035 posts
  • Has referred 500+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Became a Top 20 Author of the Month
+7 more
ThemeBlvd
says

To keep things clean with a bunch of PHP mixed in with my HTML in complicated Wordpress themes, I do all my basic if/elses and loops by line.

So in your example, I would do:

< ?php if (have_posts()) : while (have_posts()) : the_post(); ? >

I don’t like seeing the PHP tags more than once in a line unless it’s mixed into some html like:

<a href="<?php the_permalink(); ?>" title="<?php the_title; ?>" />
69 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $100+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
vinoj
says

I don’t see any good reason for creating more and more php tags like the first one ;)

Bro as i know both codes are same, and they do exactly the same thing. However, I prefer to use second one as I can organize my codes well with that way, and it is easy to find any error with that code structure too. Both codes are same, only the structures are different.

374 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rohan
says

Dont close PHP tags when there’s no need to, but make sure you indent the code properly.

by
by
by
by
by
by