Driice Driice -4 years ago 57
PHP Question

PHP 7 - Alternative Syntax else if that has HTML inside?

my apologies if there's an answer to this on SO somewhere. I searched around with no avail.

I'm wondering if there's alternative syntax for else if with HTML inside exists. I know there's such alternative syntax for if and else, but couldn't find anything in the PHP documentation for else if.

Right now I have this code:

<?php if($err) : ?>
<div class="alert-failure"><?php echo "Error: " . $err; ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert-success"><?php echo "Success: " . $success; ?></div>
<?php endif; ?>


But this seems illogical since my app won't be having an error if it's successful. Is there a way to use else if here? Thanks in advance!

Answer Source

Another way would be -

<?php ($err) ? '<div class="alert-failure">Error: ' . $err . '</div>' : 
(($success) ? '<div class="alert-success">Success: ' . $success . '</div>' : '') ?>

It is somewhat similar like -

if($err) { //... }
elseif($success) { //... }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download