Tutorial
Introduction
This tutorial will teach you how to use an if-else statement.
An if-else statement is nothing more than a condition.
See it as if you are blackmailing some one, IF you don't give me my dollar back ... I'll tell mum what you did last weekend,
ELSE ... I will let you get away with it.
Step 1 - Why would I use an if-else statement ?
if-else statements are very useful to put a condition to a specific action you would like to perform.
for example:
Somebody wants to register to your website, but they have to be older than 13.
An if else statement is going to take care of that for you.
Code:
<?php
$name = "Diederik";
$age = 12;
$minimalage = 13;
echo "
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>Hello world!</title>
</head>
<body>";
if($age < $minimalage) // If his age is lower than minimal age
{
echo "Sorry, you are too young to register.";
}
else
{
echo "Congratulations ".$name.",<br> you are ".$age."
and the minimal age is ".$minimalage;
}
echo"
</body>
</html>";
?>
In the example above here you can see that you would only be allowed to continue if your age is 13 or above.
Step 2 - Checking with operators
What are operators?
Operators are also a type of condition you could use in an if-else statement.
This website will show you operators you can use in PHP.
http://www.w3schools.com/PHP/php_operators.asp
Some examples:
Code:
<?php
$number = 5;
if($number <= 6) // if $number is smaller or equals 6
{
}
if($number == 6) // if $number equals 6
{
}
if($number !== 6) // if $number does NOT equals 6
{
}
?>
Step 3 - Get a logic vision on what you have to do.
This tutorial will teach you how an if-else statement works.
But to actually use it in your scripts will be different ofcourse,
you need to understand the possibilities of an if else statement and that there are no boundries.
To finish this tutorial i will try and explain to you how the system works once more by using an image.
Hopefully this will help you.

You now know how to use an if-else statement in your PHP script.
| « Previous | Next » |
Comments
Flamer - 2008-11-24
questions: how are you ?
Remco - 2008-11-24
Lol Flamer, I didn't mean that
but I'm fine thanks lol how are you
but I'm fine thanks lol how are you
Flamer - 2008-11-24
Im okay! Thanks for asking!
Anyways, are you going to create a forum as well ?
Anyways, are you going to create a forum as well ?
Remco - 2008-11-24
Haha, that's gd.
I might create a forum after my site is finished, as you can see i just made an update to my site so you can edit comments, just creating the delete function now.
and when my todo list is done, i could make a forum, not that hard i suppose lol.
But untill then I'm going to work on the list that i have placed in my news message.
I might create a forum after my site is finished, as you can see i just made an update to my site so you can edit comments, just creating the delete function now.
and when my todo list is done, i could make a forum, not that hard i suppose lol.
But untill then I'm going to work on the list that i have placed in my news message.
Flamer - 2008-11-25
Like: wtf!? je smilies zijn best lastig weetje, niet echt waarbij je denkt "makkelijk"
Flamer - 2008-11-27
BTW Maybe its useful to tell the folks that you can use nested IF/ELSE statements?
Example:
If you change the value of $i then the IF/ELSE statement will check if its a integer. If so then you go to the code in the IF statement. I placed some other code in here to decide if the integer is somewhere between 0/10 10/20 or 20/30. If the value is above 30 then the ELSE statement will tell ya that.
I hope this is usefull.
Example:
Code:
<?PHP
$i = 39;
IF(is_int($i))
{
echo "This is number: ".$i."<br />";
IF($i <= 10)
{
echo "Its value is smaller then 10 or equals it.";
}
ELSEIF($i > 10 && $i < 20)
{
echo "This value is between the numbers 10 and 20.<br />";
}
ELSEIF($i >= 20 && $i < 30)
{
echo "This value is between the numbers 20 and 30.<br />";
}
ELSE
{
echo "The value is above 30.";
}
}
ELSE
{
echo "This isnt a number!";
}
?>
If you change the value of $i then the IF/ELSE statement will check if its a integer. If so then you go to the code in the IF statement. I placed some other code in here to decide if the integer is somewhere between 0/10 10/20 or 20/30. If the value is above 30 then the ELSE statement will tell ya that.
I hope this is usefull.
Flamer - 2008-11-27
Why does the code appears with red syntax coloring ? Its totally right...
Remco - 2009-03-11
hey, it's a little bug in the comment filter.
I don't really have time to fix it now, the code looks fine yes.
I also have to change the width of the comment boxes, right now im working on my next tutorial about using the POST.
I don't really have time to fix it now, the code looks fine yes.
I also have to change the width of the comment boxes, right now im working on my next tutorial about using the POST.
You have to be logged in to write a comment.



