Ternary Operator & IF statements

The most simple and most used expression is the IF statement. This simply says that if a given condition is true run a set of code.

[source:php]if ($a == 1) {
print “The variable a equals 1”;
}[/source]

The above statement of course is very very simple. The IF statement is a very powerful expression when used in the right way. You can expand on this to introduce an alternative set of code if the condition is false.

[source:php]if ($a == 1) {
print “The variable a equals 1”;
} else {
print “The variable a does not equal 1”;
}[/source]

So how can this be used from what’s already been learnt? If you look back to the post on Emailing Form Results you’ll see for the PHP processing page there was the code:

[source:php]< ?php $name = $_POST['name']; $from_email = $_POST['email']; $comments = $_POST['comments']; $mailbody = "From: ".$name."\n"; $mailbody .= "Email: ".$from_email."\n"; $mailbody .= "Comments: ".$comments; mail("toemail@domain.com", "Form Results from Web site", $mailbody, "From: ".$from_email); ?>[/source]

Here, as Oli very wisely commented, this script has no error checking whatsoever. Whilst there are even more security routines that can be put in place they are beyond the scope of this post so I will explain these later (before anyone points it out!) but for now I’m concentrating on the topic in hand, IF statements.

We can ensure with the above code that a name, an email and some comments have been completed using a few IF statements. We will also create an error counter to prevent any email from being sent if errors are found.

[source:php]< ?php //initialise the error variable $error = 0; // check that a name has been entered if (!empty($_POST['name'])) { $name = $_POST['name']; } else { $error++; } // check that an email has been entered if (!empty($_POST['email']) && $error == 0) { $from_email = $_POST['email']; } else { $error++; } // check that a comment have been entered if (!empty($_POST['comments']) && $error == 0) { $comments = $_POST['comments']; } else { $error++; } // if there are no errors send an email otherwise print a statement if ($error == 0) { $mailbody = "From: ".$name."\n"; $mailbody .= "Email: ".$from_email."\n"; $mailbody .= "Comments: ".$comments; mail("toemail@domain.com", "Form Results from Web site", $mailbody, "From: ".$from_email); } else { print "Not all fields were completed, please try again"; } ?>[/source]

Here I’ve used an new function empty() which returns TRUE if the variable within it is empty otherwise it returns FALSE. However I’ve used the NOT operator before it therefore saying “IF the posted variable is NOT empty run the first statement else run the second statement”. This isn’t necessary but it makes reading code easier when your main set of code is in the first IF section.

Another method which does the same but is more for small basic if statements is the Ternary Operator. This is much more efficient for use within outputting information.

[source:php]print “if the variable i is equal to 1 “. ($i==1 ? “message 1” : “message 2”);[/source]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *