PHPLoops - Statements that enable you to achieve repetitive tasks.
The while StatementThe 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 StatementA 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 Statementis 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 />";}?>
ArrayAn 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 timeare 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 ArraysIn 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 Functionsis 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 ValueA 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"?>

Php Loop

  • 1.
    PHPLoops - Statementsthat enable you to achieve repetitive tasks.
  • 2.
    The while StatementThewhile 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.
  • 3.
    Sample 1<?php$counter =1; while ( $counter <= 12 ) {print "Counter value is $counter"."<br />"; $counter++; } ?>
  • 4.
    The do...while StatementAdo...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);
  • 5.
    Sample 2<?php$num =1;do {print "Execution number: $num<br />\n";$num++;} while ( $num > 200 && $num < 400 );?>
  • 6.
    The for Statementissimply 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;}
  • 7.
    Sample 3<?phpfor ($counter=1; $counter<=12; $counter++ ) {print "$counter times 2 is".($counter*2)."<br />";}?>
  • 8.
    ArrayAn array isa 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 timeare 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");
  • 9.
    Sample 4<?php//arrary$users =array ("Bert", "Sharon", "Betty", "Harry");print $users[2];?>
  • 10.
    Associative ArraysIn anassociative array a key is associated with a value.$salaries["Bob"] = 2000; $salaries["Sally"] = 4000; $salaries["Charlie"] = 600; $salaries["Clare"] = 0;
  • 11.
    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"];?>
  • 12.
    Php Functionsis aself-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 } ?>
  • 13.
    Sample 6<?phpfunction myname(){ $name= "your-name";echo "$name <br />";}echo "My Name is <br />";myname();?>
  • 14.
    A Function ThatReturns a ValueA 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; }
  • 15.
    Sample 7<?phpfunction addNums($firstnum, $secondnum ) { $result = $firstnum + $secondnum; return $result; } print addNums(3,5); // will print "8"?>