loops php 04

20
PHP Course PHP Course [email protected] Loops

Upload: it-big-dig

Post on 25-May-2015

1.335 views

Category:

Education


16 download

DESCRIPTION

PHP course Lecture 4 Contents PHP Array Functions while - loop do...while for - loop foreach

TRANSCRIPT

Page 1: Loops PHP 04

PHP Course

PHP Course [email protected]

Loops

Page 2: Loops PHP 04

Instructor Name: Mohamed Saad.

Email: [email protected]

Occupation: Web Developer In IT Big Dig.

PHP Course [email protected]

Page 3: Loops PHP 04

Contents• PHP Array Functions

owhile - loopodo...while o for - loop o foreach

PHP Course [email protected]

Page 4: Loops PHP 04

PHP Loopswhile - loop•Loops through a block of code while a specified condition is true.

do...while •Loops through a block of code once, and then repeats the loop as long as a specified condition is true.

for - loop•Loops through a block of code a specified number of times.

foreach •Loops through a block of code for each element in an array.

Page 5: Loops PHP 04

while - loops Syntax

o while (condition)  {  code to be executed;  }

PHP Course [email protected]

Page 6: Loops PHP 04
Page 7: Loops PHP 04

<html> <body>

<form action="while_loop.php" method="post"> Enter any number less than 10000:<input type="text" name="x"/> <input type="submit" name="submit" value="Go"/> </form> </body></html>

<?phpif (empty($_POST)===false){

$x=$_POST['x'];if ($x<10000){

$i=1;while($i<=$x){

echo $i . "<br>";$i++;

} }//end of nasted if

else{echo "You entered invalid value";}}//end of empty($_POST)

?>

Copy Codewhile_loop.php

PHP Course [email protected]

Page 8: Loops PHP 04

For LoopSyntax

o for (Counter; condition; increment)  {  code to be executed;  }

PHP Course [email protected]

Page 9: Loops PHP 04
Page 10: Loops PHP 04

<html> <body>

<form action="for_loop.php" method="post"> Enter any number less than 10000:<input type="text" name="x"/> <input type="submit" name="submit" value="Go"/> </form> </body></html>

<?phpif (empty($_POST)===false){

$x=$_POST['x'];if ($x<10000){

for ($i=1; $i<=$x; $i++){

echo $i . "<br>";}

}//end of nested ifelse{echo "You entered invalid value";}

}//end of empty($_POST) ?>

Copy CodeFor_loop.php

PHP Course [email protected]

Page 11: Loops PHP 04

Insert number and you will get

textboxes equals that number

Page 12: Loops PHP 04
Page 13: Loops PHP 04

<html> <body>

<form action="for_loop.php" method="post"> Enter any number less than 10000:<input type="text" name="x"/> <input type="submit" name="submit" value="Go"/> </form> <?php

if (empty($_POST)===false){

$x=$_POST['x'];if ($x<10000){

for ($i=1; $i<=$x; $i++){?>

<form action="for_loop.php" method="post"> <input type="text" name="<?php $i; ?>" value="<?php echo $i; ?>"/>

<?php }

}//end of nested ifelse{echo "You entered invalid value";}

}//end of empty($_POST) ?> <input type="submit" name="submit" value="Go"/></form></body></html>

Copy CodeFor_loop.php

PHP Course [email protected]

Page 14: Loops PHP 04

do...while StatementSyntax

o do  {  code to be executed;  }while (condition);

PHP Course [email protected]

Page 15: Loops PHP 04
Page 16: Loops PHP 04

For Each• When foreach first starts executing, the internal

array pointer is automatically reset to the first element of the array.

• In order to be able to directly modify array elements within the loop precede &$value.

Page 17: Loops PHP 04
Page 18: Loops PHP 04

<pre><?php$x = array(1, 2, 3, 4);print_r($x); echo "<br>";foreach ($x as &$value) { $value = $value * 2;echo $value;echo "<br>----------<br>";}//unset or it will equal 8unset($value);print_r($x); ?></pre>

Copy Codeforeeach.php

PHP Course [email protected]

Page 19: Loops PHP 04

This will not work<?php foreach (array(1, 2, 3, 4) as &$value) 

{     $value = $value * 2;

}?>

We have to create variable first.

[email protected]

Page 20: Loops PHP 04

We hope You enjoy This Tutorial.

For any Suggestions Please Email Us

[email protected]

PHP Course [email protected]

EndLoops