When you are writing programs you will find that sometimes you need to perform code only if something has happened, in the previous
form post article we used an if statement to get the information from the form only if a used had submitted the form, this stopped the script from trying to get blank form information.
This shows how we can use conditional statements within scripts to specify when certain code should be run depending on the outcome of a condition that we set.
The main conditional statements in php are:
The if Statement can be used in the following format:
PHP Code:
<?php
if (condition)
{
// If condition is true Perform this code
}
else
{
// Else perform this code
}
?>
The above example shows how the if statements works, if the condition inside the brackets is met then the code is run with the {} brackets, if that condition is not met then the code in {} brackets after the else is run.
Sometimes we need to use multiple if statements because there is more than one condition, for this we can combine if and else to use the elseif statement, this works in the following:
...