15

Click here to load reader

Php Loop

  • Upload
    lotlot

  • View
    755

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Php Loop

PHP

Loops - Statements that enable you to achieve repetitive tasks.

Page 2: Php Loop

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.

Page 3: Php Loop

Sample 1

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

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

Page 4: Php Loop

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);

Page 5: Php Loop

Sample 2

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

Page 6: Php Loop

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; }

Page 7: Php Loop

Sample 3

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

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

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

Page 8: Php Loop

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");

Page 9: Php Loop

Sample 4

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

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

Page 10: Php Loop

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;

Page 11: Php Loop

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"];?>

Page 12: Php Loop

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} ?>

Page 13: Php Loop

Sample 6

<?phpfunction myname(){

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

Page 14: Php Loop

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; }

Page 15: Php Loop

Sample 7

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