Wednesday, February 13, 2013

PHP Control Structure Syntax

This syntax is particularly useful when writing template files. In a template, you may or may not be receiving a piece of data from your CMS, and have to adjust accordingly. If the content isn't there, you'll want to make sure that an ugly error message won't appear and that the template handles the omission gracefully.

The basic concept:
<?php (conditional php statement) : ?>
   <p>some html content</p>
<?php endConditional; ?>


In the example above the HTML markup will only be output to the browser if the conditional statement resolves to true. Below we'll check that the $data has a valid value ( not equal to "" ), before printing it to the screen.

<?php if($data->user != "") : ?>
   <p>Welcome, <?php print $data->user; ?>!</p>
<?php endif; ?>

If all goes well, the result will be:  Welcome, User!
If not, nothing is output.


No comments:

Post a Comment