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;
}
}