Repeating actions by using loops September 20, 2008
Posted by Nsh15 in Basic PHP programming, php.Tags: advanced for loop, Avoiding infinite loops, Breaking out of loops:, do while loops, For loop, loops in PHP, php, while loops
1 comment so far
Loops r used frequently in scripts to set up a block of statements that repeated. The loop ca repeated according to:
- Specific number of times
- until a certain condition is met
there r three types of loops:
1. For loop:
In this type of loops, we set up a counter and repeats a block of statement until the counter reaches a specific number. u set at the beginning a counter value, set the ending value and set how u want the counter to be incremented or decremented.
for (starting_value ; ending condition ; increment)
{
block of statement
}
- starting value: its the statement that sets up a variable to be ur counter and sets its initial value
- ending condition: This is the statement that sets ur ending value, as long as the condition is true, the loop will keep executed
- increment: The statement that increments ur counter
Notice that u can nest as much loops as u wish.
Example:
<?php
for ($i=1; $i<=5; $i++)
{
echo “This is line number “,$i,”<br>”;
}
?>
The output:
This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is line number 5
2. advanced for loop:
for ( beginning statements ; conditional statements ; ending statements )
{
block of statement
}
- beginning statements: they r executed once at the start of the loop, they can be any statements to set up any need values or other statements u want to execute before the loop starts
- the conditional statements: they r tested for each iteration of ur loop
- the ending statements: they executed once at the end of the loop
Each statements section can contain as many statements as u wish, just make sure to separate ‘em using commas, and to separate each section using semicolon ; and remember that any section can be empty
3. while loops:
A while loop continue repeating as long as certain conditions r true, first of all u set up a condition, the condition is tested at the top of each loop and it will repeats till the condition is false.
while( condition)
{
block of statements
}
Example:
in this example we have a variable called $array and it contain names, we will make a while loop to search for specific name if exist and if so to know how many times it exist.
in this example I used the if statement, if u r confused about it, u can check my previous post:
also I used arrays, if u ‘ve any problems with understand it, plz check: Creating arrays
The code for the example:
<?php
$array = array (“Ali”,”Tony”,”Moneer”,”Sameer”,”David”, “Sami”, “George”,”Joe”,”Alicia”,
“Sarah”, “sally”,”Moneka”, “Richeal”,”Phebi”,”Razan”,”Mellanie”,”Noor”,”Sabrina”,”Ali”,”Tony”,”Tony”);
$array_size= sizeof($array);
$counter=0;
$searchvalue=”Tony”;
$search_repeated=0;
while ($counter<=$array_size)
{
If ($array[$counter] == $search_value)
{
$search_repeated ++;
}
$counter ++;
}
if ($search_repeated ==0)
{
echo “The item “,$search_value, ” was not founded in the array”;
}
else
{
echo “The item “,$search_value, ” was founded “, $search_repeated , ” in the array”;
}
?>
The output:
The item Tony was founded 3 in the array
4. do while loops:
They r so similar to the while loops, except that the conditions r tested at the bottom of each loop. Therefor the loop will execute at least once if the conditions r false.
do
{
block of statements
} while (conditions);
Note1: Avoiding infinite loops:
U can reach a point where the loop will never ends, in this case u have a serious problem in ur loop, so make sure of the condition and that ur counter(for loops) is incremented. If u r using PHP for a web page it will stop by itself after 30 seconds (it will timed out).
Note2: Breaking out of loops:
there r two types to break out of the loop
- break: breaks completely from the loop and continue to the next statement after the loop
- continue: skips to the end of the loop where the condition is tested, id the condition is positive then the scripts continue from the beginning of the loop.
Using conditional statements September 20, 2008
Posted by Nsh15 in Basic PHP programming, php.Tags: conditional statements in PHP, conditions in PHP, if statement in PHP, php, switch statement in PHP
add a comment
Conditional statements executes a block of statements only when certain conditions r true.
1. Using If statement:
The general format of and if conditional statement is as follows:
if (condition)
{
block of statements
}
elseif (condition)
{
block of statements
}
else
{
block of statements
}
Explanations:
- if: The block of statements executes if the condition is true only and after executing it, the script moves to the next instruction skipping any elseif or else statements (only 1 part will execute). If the condition is false then it will move to line below the curly braes, whatever it’s elseif or else or another statement.
- elseif: This section is optional, u can use more than one elseif, they executes in the same way as the above if
- else: This section is also optional, only one else statement is allowed and this part doesn’t test any condition bcz PHP consider that when the script reach that point then no other statments above was executed and thus the else condition will be true.
Example:
we want to print the student grade with a message and with a grade symbol, like the following:
| grade range | Message | grade symbol |
| 99-89 | Excellent | A |
| 90-79 | Very good | B |
| 80-69 | Good | C |
| 70-59 | O.K | D |
| 60-49 | Poor | E |
| less than 50 | Failed! | F |
<?php
$grade_check= 75;
if ($grade_check<100 and $grade_check >89)
{
$grade= “A”;
$message = “Excellent”;
}
elseif ($grade_check <90 and $grade_check > 79)
{
$grade= “B”;
$message = “Very good”;
}
elseif ($grade_check <80 and $grade_check > 69)
{
$grade= “C”;
$message = “Good”;
}
elseif ($grade_check <70 and $grade_check >59)
{
$grade= “D”;
$message = “O.K”;
}
elseif ($grade_check <60 and $grade_check> 49)
{
$grade= “E”;
$message = “Poor”;
}
else
{
$grade= “F”;
$message = “Failed!”;
}
echo “for the student grade = “, $grade_check, “<br>”,$message, “ ( “,$grade, ” )<br>”;
?>
The output for three random grades:
for the student grade = 75
Good ( C )
for the student grade = 64
O.K ( D )
for the student grade = 95
Excellent ( A )
Note1: U can test 4 negative if statements that is the block of statements r executed when the condition is false, by adding exclamation point (!) at the beginning of the condition. i.e. if( ! $number >5) then the block of statements will execute only if the $number contains value less than or equal 5.
Note2: U can have nested if statement by adding if statement inside another if, and so on. this is called nesting. but remember that u can’t reach the inner if , if the outer if isn’t true … the block of statements will execute only if all the if’s r true in the path till reach that block!
1. Using Switch statement:
switch statement is a good option when u ‘ve a lot of conditions. The switch statement tests the value of one variable and then executes the block of variables that matching the value in the variable.
switch ($variable_name)
{
case value:
{
Block of statements
}
case value:
{
Block of statements
}
.
.
.
default:
{
Block of statements
}
}
- The default section is optional, works like else in the if statements, and it will exucute only it the variable value didn’t match any case statement, u can place it anywhere but it make more sense to add it at the end of the switch statement.
- The break statements r essentials, if u didn’t write it, if the case if true it will contiue to execute all the cases below it without checking the condition.
Example:
the same as the above example but we will write it using switch
<?php
$grade_check_s= 48;
switch ($grade_check_s)
{case ($grade_check_s <100 and $grade_check_s >89):
{
$grade_s= “A”;
$message_s = “Excellent”;
break;
}
case ($grade_check_s <90 and $grade_check_s >79):
{
$grade_s= “B”;
$message_s = “Very good”;
break;
}
case ($grade_check_s <80 and $grade_check_s >69):
{
$grade_s= “C”;
$message_s = “Good”;
break;
}
case ($grade_check_s <70 and $grade_check_s >59):
{
$grade_s= “D”;
$message_s = “O.K”;
break;
}
case ($grade_check_s <60 and $grade_check_s >49):
{
$grade_s= “E”;
$message_s = “Poor”;
break;
}
default:
{
$grade_s= “F”;
$message_s = “Failed!”;
}
}
echo “for the student grade = “, $grade_check_s, “<br>”,$message_s, “ ( “,$grade_s, ” )<br>”;
?>
The output for three random grades:
for the student grade = 48
Failed! ( F )
for the student grade = 86
Very good ( B )
for the student grade = 77
Good ( C )