admin管理员组

文章数量:1406178

I am trying to write an if condition with 3 statements but it keeps crashing the site. Here is my statement, can you please advise what the issue is.

<?php if (is_page ('20')){?>
print this
<?php elseif (is_page ('50')){?>
then print this
<?php } else { ?>
print this
<?php } ?>

I am trying to write an if condition with 3 statements but it keeps crashing the site. Here is my statement, can you please advise what the issue is.

<?php if (is_page ('20')){?>
print this
<?php elseif (is_page ('50')){?>
then print this
<?php } else { ?>
print this
<?php } ?>
Share Improve this question asked Nov 25, 2019 at 2:12 DarrenLeeDarrenLee 257 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

You're missing } before elseif:

<?php if (is_page ('20')){?>
print this
<?php } elseif (is_page ('50')){?>
then print this
<?php } else { ?>
print this
<?php } ?>

Remove the PHP tags and you'll see why:

if ( is_page( '20' ) ) {

elseif ( is_page( '50' ) ) {

} else {

}

You may write the statement in only one, short row:

print (is_page('20') ? "my output for page 20" : (is_page('50') ? "my output for page 50" : "my output for anything else") );

Important: Brackets must be in the right order like (statement ? if-result : else-result) where the "else-result" is a 2nd nested statement, so it looks like (statement-1 ? 1st-if-result : (statement ? 2nd-if-result : last-else-result) ) Good luck!

本文标签: Conditional statement with three condition