Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress PHP for WordPress PHP Basics for WordPress Custom Loops

Carlos Enrique Castañeda Gutiérrez
Carlos Enrique Castañeda Gutiérrez
13,886 Points

Why the IF statement use ": endif" instead of "{}" ?


I've been following the PHP development track before enter to this Wordpress Development Track.

In all previous PHP courses the "if statement" and "while blocks" uses brackets {} not ": endif " or ": endwhile".

Is this a convention in Wordpress Development? Can I use brackets instead or not?


1 Answer

This falls in the category of coding style. Some people like to use brackets some like to use the ":end..." notation. The advantage to the latter is more towards using PHP conditionals and loops where there is output to HTML. For example,

//Pure PHP with brackets
if ($condition) {
   echo "output";
//PHP and HTML with brackets
if ($condition) {?>
<?php } ?>
//PHP and HTML with ":end..." notation
<?php if ($condition):?>
<?php :endif } ?>

Some people prefer the last as the ":endif" gives a clue what opening construct to look for when matching control structures. The bracket could match any other bracket but an ":endif" can only match and "if" construct. You can use either it is up to your style.