1

I realise this may be a generic php question, but as it's related to WP I'm including it here.

If adding a simple conditional to my page.php template, do I need to include an 'else' (for other pages) and 'endif'?

<?php
if (is_tree(39)) : get_template_part('templates/partial/menu-buy');
elseif (is_tree(117)) : get_template_part('templates/partial/menu-programs');
?>

Other than an 'empty condition' (don't know if that's the right word) I can't see a way to include an 'else' for other pages.

My limited knowledge suggests it's probably good practice to finish with 'endif', but I've seen conditionals written with/without an 'endif' and hence wonder whether it's necessary.

UPDATE: Thanks to those who've answered, I now realise there's two different forms of syntax: http://php.net/manual/en/control-structures.alternative-syntax.php PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

3

You just need an else block:

//templating syntax
if($condOne):
    //do stuff if $condOne is true
elseif($condTwo):
    //do stuff if $condOne is false and $condTwo is true
else:
    //do stuff if both $condOne and $condTwo are false
endif;

//braces syntax
if($condOne){
    //do stuff if $condOne is true
}elseif($condTwo){
    //do stuff if $condOne is false and $condTwo is true
else{
    //do stuff if both $condOne and $condTwo are false
}

Regarding "it's probably good practice to finish with 'endif'", endif is only used when you are using the templating syntax:

if():
    //do stuff
endif;

the alternative is regular braces syntax:

if(){
    //do stuff
}

The former is preferable when you are outputting html by dropping out of php with a closing php tag, because an endif is easier to match up than a closing brace:

<?php if($someCondition):?>

   <h1>Hello <?=$userName?>, how are you today?</h1>
   <ul>
   <?php foreach($someArray as $key => $value):?>
       <li><?=$key?> : <?=$value?></li>
   <?php endforeach;?>
   </ul>

<?php endif;?>

VS:

<?php if($someCondition){?>

   <h1>Hello <?=$userName?>, how are you today?</h1>
   <ul>
   <?php foreach($someArray as $key => $value){?>
       <li><?=$key?> : <?=$value?></li>
   <?php }?>
   </ul>

<?php }?>

However when writting code that does not drop out of php mode, the latter is far more common and arguable easier to read:

$out=[];
if($someCondition){

    $out[$username]=[];

    foreach($someArray as $key => $value){

        $out[$username][] = $key . ' : ' . $value;

    }

}
  • Thanks for the comprehensive explanation. I can now understand much better. New to this, I hadn't realised there's two different forms of syntax (templating and braces), and think part of my confusion was having read something about braces being optional and seen some stuff written without them (example: <?php if (is_page('search')) echo 'test'; ?>). – glvr May 31 '17 at 5:23
2

Well @gulliver it's kinda opinion based question. See we actually use endif to end the if conditional. If you are in a condition when a thing could change based on a condition then you have to use the else statement. Like below-

if('statement' == true ) {
    // do only when the statement is true
} else {
    // do only when the statement is false
}
// execute the other codes.

Notice in above case you can skip the else block if you do return. Cause PHP will not execute anything after return. So the code will be-

if('statement' == true ) {
    // do only when the statement is true        
    return 0;
    // Rest of the part will not be executed.
}
// This part will only execute when the statement is false.

Now the above code blocks can be written like below with different procedure using if():, else and endif-

First code blocks-

if('statement' == true ) :
    // do only when the statement is true
else :
    // do only when the statement is false
endif;
// execute the other codes.

Second Code Blocks-

if('statement' == true ) :
    // do only when the statement is true        
    return 0;
    // Rest of the part will not be executed.
endif;
// This part will only execute when the statement is false.

Now decide yourself if you need to include else and/or endif. Ask any kinda question in you need to know further more.

1

endif went out of tyle years ago, should just avoid using it. Main reason that pops into my mind is lack of "block" detection by editors which make balancing ifs harder.

OTOH you should probably always have an else even if it is an empty one. The idea is that you show that you actually thought about what happens in the condition fails. Right now your snippet might have no output, is it intentional?

Best thing is probably use a linter that checks against the wordpress (or the wordpress.com VIP) coding standard. There is criticism against them, but having any coding standard is better then having none, and automatic tool that will test it helps you to actually follow it.

  • Yea even envato encourages the developers to use curly braces for coding. Having conditionals within conditionals with the if : endif; format without indentation = tears for the debugger – Jack Johansson May 31 '17 at 5:47
  • @Mark Kaplun...thanks. I'm confused on the 'empty else', having read 'remove that 'else' if it doesn't do anything - you shouldn't have one unless it is going to perform a specific set of tasks'. – glvr May 31 '17 at 5:57
  • whoever said that, is wrong. many bug come from not properly thinking about the "else" part, and having an empty else indicates you decided there is nothing to do there. In the end the point of coding standards are (also) to protect you against gotchas that the language might let you fall into. in the long term understanding the missing else has a reason and it is not an omission when reading the code 4 months after it was written is more important than saving two lines. – Mark Kaplun May 31 '17 at 6:34
  • @Mark Kaplun ... Thanks. As a novice in this, I'm appreciative of whatever helps me learn. Having re-searched more since your comment, I can better understand the differing yay/nay viewpoints... although empty {} could be interpreted as 'is this code incomplete?', the addition of a simple '// intentionally left blank' comment removes such ambiguity and seems a relatively sensible and easy thing to do. I already use a lot of explanatory comments in my html/css/etc, because it helps me when looking at it some time after originally written. – glvr May 31 '17 at 9:44
  • you can always use just an else ; but really this is more about coding style and your personal hurt points, there might not be a one size fits all here, and I am for sure not consistant myself :). This is why I recommended to follow other people style at least until you develop your own. – Mark Kaplun May 31 '17 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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