Home

Welcome to Neocrisis

We bring you a fully interactive and fun experience on this site. To this effect, we have compiled a superb collection of articles that we hope you are going to find interesting.

Site / Forum Login

Syndicate

Total Users

357 registered
0 today
0 this week
4 this month
Last: ravaz911
mod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_countermod_vvisit_counter

Other Site Info



Site Status

eXTReMe Tracker

Tracker Stats #2
Invisible Counter

Donate

PHP intro guide - PART 6 PDF Print E-mail
(0 votes)
Written by zzchanli   
Saturday, 23 August 2008

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:

PHP Code:
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:

PHP Code:
$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:

PHP Code:
if(condition){
//code

PHP Code:
if(condition){
//code
}
else{
//code

PHP Code:
if(condition){
//code
}
elseif(
condition){
//code

PHP 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:

PHP Code:
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!

PHP Code:
$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!

Comments
Search RSS
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."





Facebook!Slashdot!StumbleUpon!Newsvine!Free social bookmarking plugins and extensions for Joomla! websites!
 
< Prev   Next >