Its time fellow programmers to learn the most critical part to php, the if-elseif-else statement and loops. Before tackling this section make sure you have a firm grasp on the operators section, this is where they will be needed!
so what is a if-elseif-else statement? well its comprised of three statements. Combined they have a syntax of:
if(condition){
//code here for this situation
}
elseif(another condition){
//first case was not true, so different code here
}
else{
//if all else fails do this code
}
Basically all this mumbo jumbo does is let use certain codes at certain times. the condition can use any of the comparison operators given in the appropriate situation. Here is a more practical example:
$today = 'monday';
if($today == 'friday' || $today == 'saturday'){
echo('fridays and saturdays are fun');
}
elseif($today == 'sunday'){
echo('the week has officially begun');
}
else{
echo('today is another boring weekday. BOOOO!');
}
Its really simple isn't it? in english this statement would say "Todays date is monday. if today is friday or saterday I am going to say that friedays and saturdays are fun. however, if today is sunday I am going to say the week has officially begun. otherwise I am going to say today is another boring weekday. BOOOO!"
The nice thing about these statements is that only the if statement is required! Therefore you do not need to use the entire structure. A second bonus is their is no limit on how many elseifs you can use in one statement. you can use infinite statements in any particular script to!
if-else is a completely legal structure in php aswell. here is a list of possibilities:
if(condition){
//code
}
else{
//code
}
if(condition){
//code
}
elseif(condition){
//code
}
if(condition){
//code
}
elseif(condition){
//code
}
elseif(condition){
//code
}
elseif(condition){
//code
}
else{
//code
}
All of those are acceptable but those are just the begining you could even do this:
if(condtion){
if(condition){
//code
}
else{
//code
}
}
Cool isn't it? Thats the control structure for many programing languages, though minor variations may be required! Now I would like to talk about something called a loop. First you have the scenario to count to 100 in php. do you really wanna type echo('1'); 100 times? I don't think so that would take forever! So programmers came up with something that is called a loop! It lets you preform a action over and over again!
So how do we make a loop? Its simple really! but before you write a loop you must consider that there are two kinds of loops FOR loops and WHILE loops. Both are equally useful here is the structure of both, you will notice they are similar to your if-elseif-else statements!
$i = 0;
while($i <= 100){
echo("$i");
$i++;//putting ++ after a variable adds one to that variable
}
for($num = 0; $num <= 100; $num++){
echo("$num");
}
Thats it, Thats all their is to loops! Now heres a challenge write a script to echo off the song 99 beers on the wall, see if you can do it! Remember there was a TON covered in this lesson so you may need to read it a few times! this is completely normal! Do not panic if you do not get it at first, just post here if you have any questions I will do my best to answer them! ENJOY!