PHP Conditional Markup

It can be hard to find the right result when you search for “PHP conditional markup”, so I’m helping myself (and maybe others) by posting this up here!

You can make document content conditional by enclosing it in a regular PHP if{}

<?php if ( $errorMsg ){  ?>
<div id="message">
<?php echo $errorMsg; ?>
</div>
<?php } ?>

But, for the sake of readability, the alternative syntax is often used, where the opening brace changes to a colon (:) and the closing brace changes to the keyword ‘endif;’

<?php if ( $errorMsg ):  ?>
<div id="message">
<?php echo $errorMsg; ?>
</div>
<?php endif; ?>

For more information, see the PHP documentation for Alternative syntax for control structures.

Hope that’s helpful! :)

Advertisements

About stegriff

Aspiring man of God. Professional computer geek. "Not a hipster".

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: