UNDERSTANDING AND USING IF STATEMENTS - Jun 19th, 2011
|
The if statement is one of the most powerful and valuable operators that you can use when designing your pages. You can test for many different scenarios and have your page output (or not output) specific code, depending on the outcome of your tests.
The problem is that many times, if you’ve made a mistake in your coding, you’ll get a blank page displayed in your browser and have no idea what your mistake was.
So here’s a short primer on the correct way to use this powerful tool.
The first rule to remember is that every (series of) if statement(s) must be closed with an <?php endif ?> in order for them to work correctly.
The second rule is that if you’re using a series of if statements, then the first one is always an “if”, the last is always “else” and any in the middle are “elseif”.
So, if you’re using only one if statement to test for a particular condition. Say to test whether the field “your_field” is empty, then you’d use:
In a single record viewer:
<?php if ($your_tableRecord['your_field']): ?> ...your code... <?php endif ?>
Or in a multi record viewer:
<?php foreach ($your_tableRecords as $record): ?> <?php if ($record['your_field']): ?> ...your code... <?php endif ?> <?php endforeach; ?>
YOU CAN ALSO PUT AN IF STATEMENT AROUND A BLOCK OF TEXT TO KEEP IT FROM BEING RUN IF THERE ARE NO RECORDS AVAILABLE.
<?php if ($your_tableRecord['your_field']): ?> <?php foreach ($your_tableRecord['your_field'] as $upload): ?>
... code to be run...
<?php endforeach ?> <?php endif ?>
IF YOU WANTED TO TEST FOR PARTICULAR CONTENTS IN “YOUR_FIELD”.
Say, if the contents of “your_field” equaled “apples” then, you’d use the exactly equal to “==” operator:
In a single record viewer:
<?php if ($your_tableRecord['your_field'] == 'apples'): ?> ...your code... <?php endif ?>
Or in a multi record viewer:
<?php foreach ($your_tableRecords as $record): ?> <?php if ($record['your_field'] == 'apples'): ?> ...your code... <?php endif ?> <?php endforeach; ?>
IF YOU WANTED TO TEST FOR THE ABSENCE OF PARTICULAR CONTENTS IN “YOUR_FIELD”.
Say, if the contents of “your_field” was anything other than “apples” then, you can use the not operator “!” :
In a single record viewer:
<?php if (!$your_tableRecord['your_field'] == 'apples'): ?> ...your code... <?php endif ?>
Or in a multi record viewer:
<?php foreach ($your_tableRecords as $record): ?> <?php if (!$record['your_field'] == 'apples'): ?> ...your code... <?php endif ?> <?php endforeach; ?>
TO USE A SERIES OF IF STATEMENTS TO TEST FOR A SERIES OF CONDITION, THEN YOU’D USE:
in a detail viewer:
<?php if ($your_tableRecord['your_field'] == 'apples'): ?> ...your field contains apples... <?php elseif ($your_tableRecord['your_field'] == 'bananas'): ?> ...your field contains bananas... <?php else: ?> ...your code... <?php endif ?>
And in a list viewer:
<?php foreach ($your_tableRecords as $record): ?> <?php if ($record['your_field'] == 'apples'): ?> ...your field contains apples... <?php elseif ($record['your_field'] == 'bananas'): ?> ...your field contains bananas... <?php else: ?> ...your code... <?php endif ?> <?php endforeach; ?>
YOU’RE NOT LIMITED TO A SINGLE OR A SINGLE FIELD CONDITION.
To test for multiple conditions, you can use:
(Note: The “!” works in this too)
<?php if ($your_tableRecord['field_a'] == 'apples' && $your_tableRecord['field_b'] == 'bananas' && $your_tableRecord['field_c'] == 'pears'): ?> ...Your record has all three... <?php endif ?>
Or
<?php foreach ($testRecords as $record): ?> <?php if ($record['field_a'] == 'apples' && $record['field_b'] == 'bananas' && $record['field_c'] == 'pears'): ?> ...Your record has all three... <?php endif ?> <?php endforeach; ?>
IF YOU WANTED TO TEST FOR THREE CONDITIONS IN A LIST VIEWER RECORD.
Say, “field_a” empty, “field_b” empty and “field_c” not empty, you could use:
<?php if (!$record['field_a'] && !$record['field_b'] && $record['field_c']): ?> ...your code... <?php endif ?>
IF YOU WANTED TO TEST FOR ANY OF A SERIES OF CONDITIONS.
Say a or b or c, you could use:
<?php if ($your_table Record['your_field'] == "apples" || $your_tableRecord['your_field'] == "bananas" || $your_tableRecord['your_other_field'] == "pears"): ?> Your code... <?php else: ?> Your code... <?php endif ?>
Or
<?php if ($record['your_field'] == "apples" || $record['your_field'] == "bananas" || $record['your_other_field'] == "pears"): ?> Your code... <?php else: ?> Your code... <?php endif ?>
IF YOU WANT TO TEST TO SEE IF A FIELD CONTAINS A PARTICULAR ALPHANUMERIC PATTERN
Like seeing if apples is included in a multi item list, you can use the strpos function. Theoretically the strpos function returns the numeric position of the first occurrence of the pattern that you’re searching for, however, before it can do that it has to decide if the pattern exists in the string.
To test for the pattern “apples” in the string “bananas, apples, pears, raisins” you could use something like:
<?php if (strpos($your_table['your_field'], 'apples')): ?> Your code... <?php endif ?>
This is by far not a comprehensive list of the possibilities for using if statements, but it should be enough to get you started.
Before leaving this topic, I was curious as to the rules for requiring an <?php endif; ?> to close an "if" statement.
Jason Sauchuk, a programmer with Interactive Tools cleared up the question:
"You only need <?php endif ?> if your "if" statement covers more than 1 <?php?> block.
Example:
<?php if($x==1?> ----some code--- <?php endif ?>
This could also be put into 1 <?php ?> block with no <?php endif ?>, as in:
<?php if($x==1){ ---some code-- } ?>
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.