PDA

View Full Version : Basic PHP Tutorial - 3



ctt1010
10-31-2014, 09:30 PM
In this tutorial I will be covering he useful if statement and the if else statement.

An if is used to check if a variable or piece of data matches what is defined. For example if I wanted to check if I had 3 apples, I would use an if statement. Say we had a variable, apples. It will be set a 5. We will take 2 and then check with an if statement if it equals 3.



<?php
$apples = 5;
$applesMin = $apples - 2;

if ($applesMin = 3) {
echo "I will be printed!";
}
?>


In this case the "I will be printed!" will be printed as we took 2 apples off the original 5.

What about the if.. else?

We can use this to do something if the if statement was not triggered. For example we will take 3 off the 5 apples, and check if it is 3.



<?php
<?php
$apples = 5;
$applesMin = $apples - 3;

if ($applesMin = 3) {
echo "3 apples!";
} else {
echo "More or less than 3 apples!;
}
?>


In this case the "More or less than 3 apples!" will be printed as there are now 2 apples as we took away 3.