Php Loop

Preview:

Citation preview

PHP

Loops - Statements that enable you to achieve repetitive tasks.

The while Statement

The while statement looks similar in structure to a basic if statement:

while ( expression ) { // do something }

As long as a while statement's expression evaluates to true, the code block is executed repeatedly. Each execution of the code block in a loop is often called an iteration.

Sample 1

<?php$counter = 1; while ( $counter <= 12 ) {print "Counter value is $counter"."<br

/>"; $counter++; } ?>

The do...while Statement

A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it.

The test expression of a do...while statement should always end with a semicolon.

do-while loop first do's and secondly checks the while condition!

do { // code to be executed } while (expression);

Sample 2

<?php$num = 1;do {print "Execution number: $num<br />\n";$num++;} while ( $num > 200 && $num < 400 );?>

The for Statement

is simply a while loop with a bit more code added to it.  The common tasks that are covered by a for loop are:

Set a counter variable to some initial value. Check to see if the conditional statement is true. Execute the code within the loop. Increment a counter at the end of each iteration through the

loop.

for ( initialize a counter; conditional statement; increment a counter){

do this code; }

Sample 3

<?phpfor ( $counter=1; $counter<=12;

$counter++ ) {print "$counter times 2 is".

($counter*2)."<br />";}?>

Array

An array is a data structure that stores one or more values in a single value (bucket). 

The array() construct is useful when you want to assign multiple values to an array at one time

are indexed from zero by default, so the index of any element in a sequentially indexed array always is one less than the element's place in the list $users = array ("Bert", "Sharon", "Betty", "Harry");

Sample 4

<?php//arrary$users = array ("Bert", "Sharon",

"Betty", "Harry");print $users[2];?>

Associative Arrays

In an associative array a key is associated with a value.

$salaries["Bob"] = 2000; $salaries["Sally"] = 4000; $salaries["Charlie"] = 600; $salaries["Clare"] = 0;

Sample 5

<?php//associative arrary $salaries["Bob"] = 2000;$salaries["Sally"] = 4000;$salaries["Charlie"] = 600;$salaries["Clare"] = 0;

echo "Bob is being paid - $" . $salaries["Bob"] . "<br />";echo "Sally is being paid - $" . $salaries["Sally"] . "<br />";echo "Charlie is being paid - $" . $salaries["Charlie"] . "<br />";echo "Clare is being paid - $" . $salaries["Clare"];?>

Php Functions

is a self-contained block of code that can be called by your scripts. When called, the function's code is executed. You can pass values to functions, which they then work with. When finished, a function can pass a value back to the calling code. function myCompanyMotto(){ //some codes} ?>

Sample 6

<?phpfunction myname(){

$name = "your-name";echo "$name <br />";}echo "My Name is <br />";myname();?>

A Function That Returns a Value A function can return a value using the

return statement in conjunction with a value. return stops the execution of the function and sends the value back to the calling code.

function addNums( $firstnum, $secondnum ) {$result = $firstnum + $secondnum; return $result; }

Sample 7

<?phpfunction addNums( $firstnum, $secondnum ) { $result = $firstnum + $secondnum; return $result; } print addNums(3,5); // will print "8"?>

Recommended