Notes of PHP Part 2


PHP IF Statment

If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.
The if statement is one of the most important features of any language including PHP. It allows for conditional execution of code fragments. In PHP IF statement structure is similar to that of C structure:

if (expr) { statement 1; statement 1; }

Note: If we have a single statement in the body of IF structure then there is no need of opening parentheses { and closing parenthesis }.
The following example would display a is bigger than b if $a is bigger than $b:

<?php if ($a > $b) echo "a is bigger than b"; ?>

if you want to to execute more then one statement than For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:

<?php if ($a > $b) { echo "a is bigger than b"; $b = $a; } ?>

if....else Statements in PHP
Instead of using two if statements we can use an if ... else statement. Like this:

if (expersion) { statement 1; statement 1; statement 1; } else { statement 1; statement 1; statement 1; }

In simple if statement if the expression return true then the statements in the body of if will be executed but if expression return false then do nothing. but in if else statement some thing is executed in both condition . if the expression return true then statements in the body of if will be executed otherwise statements in the body of else will be executed.
For example if we check the user name and use is tauqeer then its welcome otherwise user ask to login first.

<?php $user_name='Tauqeer'; if ($user_name=='Tauqeer') echo " you are welcome to learninghints.com"; else echo "sory please log in first"; ?>

if ... elseif Statements in PHP
if else if statement is use when one of several blocks of code to be executed. elseif is an extra conditional test within an IF statement. It's like doing a second IF test for a true or false value. The block of coding in an ELSEIF area will be executed only if the condition value of the IFELSE is true. This part goes after the IF and before the ELSE areas.
There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

<?php if (condition) { do this block of coding; if the condition carries a true value; } elseif (different condition) { do this block of coding; if the elseif condition carries a true value; } else { do this block of coding as a default; if all above conditions carried a false value; } ?> for example: <?php if ($x > $y) { echo "x is larger than y"; } elseif ($x == $y) { echo "x is equal to y"; } else { echo "x is smaller than y"; } ?>


Loops in PHP

For Loops In PHP
When we want to execute one or more then one statement more then one time then we use loop. You can programme without using loops. But it?s an awful lot easier with them. Consider this.
You want to print up the numbers 1 to 10. You could do it like this:
echo 1 2 3 4 5 6 7 8 9 10;
Fairly if we want to print thousand numbers? Are you really going to type them all out like that? It?s an awful lot of typing. An other batter way is loop.
A loop would make life a lot simpler. You use them when you want to execute the same code over and over again.
one of the most simple loop is for loop.
For Loops
Now we take an example to print 10 number with for loop.

<?PHP $start = 1; for($start; $start <= 10; $start++) { echo $counter . "<BR>"; } ?> The format for a For Loop is this: for (start value; end value; update expression) { some statements }

The first thing you need to do is type the name of the loop you?re using, in this case for. In between round brackets, you then type your three conditions:
Start Value The first condition is where you tell PHP the initial value of your loop. In other words, start the loop at what number? We used this:
$start = 1;
We assigning a value of 1 to a variable called $start. Like all variables, you can make up your own name. A popular name for the initial variable is the letter i . You can set the initial condition before the loop begins, like we did:
$start = 1;

for($start; $start <= 10; $start++) { Or you can assign your loop value right in the For Loop code: for($start = 1; start <= 10; start++) {

The result is the same ? the start number for this loop is 1
End Value
Next, you have to tell PHP when to end your loop. This can be a number, a Boolean value, a string, etc. as in our example start <= 10 means end value is 10

for($start;$start<=10;$start++) {

When the value of $start is 11 or higher, PHP will bail out of the loop.
now last thing is increment you must make an increment the loop value if you dont make an increment then loop stuck. the $start++ means increase the value of $start one time this is same as $start=start+1;
You can go down by one (decrement) by using the double minus symbol (--), but we won?t go into that.
So our whole loop reads Starting at a value of 1, keep going round and round while the start value is less than 11. Increase the starting value by one each time round the loop.?
Every time the loop goes round, the code between our two curly brackets { } gets executed:

While Loop In PHP

The structure of a while loop is more simple than a for loop, because you?re only evaluating the one condition. The loop goes round and round while the condition is true. When the condition is false, the programmed breaks out of the while loop. Here?s the syntax for a while loop:

while (condition) { statement } For example: $counter = 1; while ($counter < 11) { echo (" counter = " . $counter . "<BR>"); $counter++; }

foreach Loop In PHP

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:
First Syntax:

foreach (array_expression as $value) { statement }

The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).
For Example:

<?php $data = array(1, 2, 3, 4); foreach ($data as $value) { echo $value . '<br>"; } ?>

2nd Synatx:

foreach (array_expression as $key => $value) { statement } example of second synatx is : <?php $full_name = array( ); $full_name["David"] = "Gilmour"; $full_name["Nick"] = "Mason"; $full_name["Roger"] = "Waters"; $full_name["Richard"] = "Wright"; foreach ($full_name as $key_name => $key_value) { print "Key = " . $key_name . " Value = " . $key_value . "<BR>"; } ?>