0

Which is better...

<?php if (is_page()): ?>
<?php else: ?>
<?php endif; ?>

or

<?php if (is_page()) { ?>
<?php } else { ?>
<?php } ?>

closed as off-topic by cjbj, fuxia Jan 2 '17 at 15:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

As far as I can tell, there's no performance difference. And both styles are acceptable by WordPress Coding Standards as you can read in:

Braces should always be used, even when they are not required:

and

Note that requiring the use of braces just means that single-statement inline control structures are prohibited. You are free to use the alternative syntax for control structures (e.g. if/endif, while/endwhile)—especially in your templates where PHP code is embedded within HTML

I always use the brace styles beucause I think is more widely known by PHP programmers of different levels and makes the block "hierarchy" much clearer and easier to understand.

Bottom line: you should choose what is more convenient to you. And whichever you choose, be consistent and stick to it.

0

I really don't know which is better. Exploring the different conditional statements syntax.

I believe they are all just alternative syntax and have no real performance difference. You can have:

<?php if (is_page()): ?>
<?php else: ?>
<?php endif; ?>

OR:

<?php if ( is_page() ) { ?>
<?php } else { ?>
<?php } ?>

also ternary operator:

<?php echo is_page() ? "Is page" : "Not a page"; ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.