How can I add branching logic to my PHP code?
This is achieved with the if and else statements in PHP, and it is quite simple to do.
Here is an example of this in action, where you will get a different message depending on whether it is raining or not.
<?php
$itisraining = 1; //yes it is raining
if ($itisraining) { echo "It is raining, take an umbrella"; }
else { echo "No need for an umbrella-ella today!"; }
?>
Points to note: the curly braces { } enclose each section of logic, e.g. an if or an else code. These are not strictly mandatory and you'll often see them dropped for short pieces of logic on one line but it is best to use them for clarity and to stop confusion.
Run this program twice and the second time set the variable at the top to '0'. You will notice that the 'if' statement evaluates '0' to false, whereas '1' is true.
Comment on this Question and Answer >>>
ASK A QUESTION
More other PHP Questions
Can I spellcheck my text using PHP?How do you get an array of the date and time?
Can I include Javascript in my PHP code?
What is the differece between $message and $$message?
What are autoglobals?
