Sunday, October 15, 2006

PHP Logic

by: Izzat Youssif

If else statements

Let's look at an example

if ($lesson = "html") {
echo "Welcome to html";
}

The above statement tells php to display Welcome to html if our Lesson variable is equal to html.
The condition is always stated next to the if in () . The results are stated after the { and } tells php that you are ending the if statement.

Next let's look at an else example

if ($lesson = "html") {
echo "Welcome to html";
}
elseif ($lesson = "php") {
echo "Welcome to php";
}
else {
echo "No lesson chosen";
}

Else statement came after an if statement. Notice else came in elseif and else formate. Elseif looks for other conditions if they existed. Else is the result as a last resort, if no other option is available. Both elseif and else follow the same structure as if statements.

Next , we'll talk about loops statments

The first type of loop statements are while statements

Let's look at an example below

$ls = 0;
while ($ls <>echo "$lesson" ;
$ls++;
}

While statement are used to loop through a collection of records or data. The above statement simply at the number of our $ls variable and from that number prints the chosen lesson. Again following the same structure as if else statements, condition next to the while and condition below { and ending with . The only differences is we incremented the variable within the statement.

Next we'll discuss for statements

For statements are used in the same essence as while statment, expect more emphasis is put on the variable as you'll in the example below.

for ($ls=0;$ls<$totalls;$ls++)
{
echo $ls['lesson'];
}

The above example, declares the ls variable again and compares it to total numbers of lessons. While ls is less than total number of lessons, we tell php to increment. Again following the same structure as if else statements, condition next to the for and condition below { and ending with } .

About The Author
Izzat Youssif is an online web designer and developer offering low cost website design and development service, as well as free website design and development. visit http://websitedevelopmentanddesign.com for more details!