Php Loop

937 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
937
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php Loop

  1. 1. PHP<br />Loops - Statements that enable you to achieve repetitive tasks.<br />
  2. 2. The while Statement<br />The while statement looks similar in structure to a basic if statement:<br /> while ( expression ) { <br /> // do something <br /> }<br />As long as a while statement&apos;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.<br />
  3. 3. Sample 1<br />&lt;?php<br />$counter = 1; <br />while ( $counter &lt;= 12 ) {<br />print &quot;Counter value is $counter&quot;.&quot;&lt;br /&gt;&quot;; <br />$counter++; <br />} <br />?&gt;<br />
  4. 4. The do...while Statement<br />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.<br />The test expression of a do...while statement should always end with a semicolon.<br />do-while loop first do&apos;s and secondly checks the while condition!<br />do { <br />// code to be executed <br />} while (expression);<br />
  5. 5. Sample 2<br />&lt;?php<br />$num = 1;<br />do {<br />print &quot;Execution number: $num&lt;br /&gt; &quot;;<br />$num++;<br />} while ( $num &gt; 200 && $num &lt; 400 );<br />?&gt;<br />
  6. 6. The for Statement<br />is simply a while loop with a bit more code added to it.<br /> The common tasks that are covered by a for loop are:<br />Set a counter variable to some initial value.<br />Check to see if the conditional statement is true.<br />Execute the code within the loop.<br />Increment a counter at the end of each iteration through the loop.<br />for ( initialize a counter; conditional statement; increment a counter){<br />do this code;<br />}<br />
  7. 7. Sample 3<br />&lt;?php<br />for ( $counter=1; $counter&lt;=12; $counter++ ) {<br />print &quot;$counter times 2 is&quot;.($counter*2).&quot;&lt;br /&gt;&quot;;<br />}<br />?&gt;<br />
  8. 8. Array<br />An array is a data structure that stores one or more values in a single value (bucket). <br />The array() construct is useful when you want to assign multiple values to an array at one time<br />are indexed from zero by default, so the index of any element in a sequentially indexed array always is one less than the element&apos;s place in the list<br />$users = array (&quot;Bert&quot;, &quot;Sharon&quot;, &quot;Betty&quot;, &quot;Harry&quot;); <br />
  9. 9. Sample 4<br />&lt;?php<br />//arrary<br />$users = array (&quot;Bert&quot;, &quot;Sharon&quot;, &quot;Betty&quot;, &quot;Harry&quot;);<br />print $users[2];<br />?&gt;<br />
  10. 10. Associative Arrays<br />In an associative array a key is associated with a value.<br />$salaries[&quot;Bob&quot;] = 2000; <br />$salaries[&quot;Sally&quot;] = 4000; <br />$salaries[&quot;Charlie&quot;] = 600; <br />$salaries[&quot;Clare&quot;] = 0; <br />
  11. 11. Sample 5<br />&lt;?php<br />//associative arrary<br />$salaries[&quot;Bob&quot;] = 2000;<br />$salaries[&quot;Sally&quot;] = 4000;<br />$salaries[&quot;Charlie&quot;] = 600;<br />$salaries[&quot;Clare&quot;] = 0;<br />echo &quot;Bob is being paid - $&quot; . $salaries[&quot;Bob&quot;] . &quot;&lt;br /&gt;&quot;;<br />echo &quot;Sally is being paid - $&quot; . $salaries[&quot;Sally&quot;] . &quot;&lt;br /&gt;&quot;;<br />echo &quot;Charlie is being paid - $&quot; . $salaries[&quot;Charlie&quot;] . &quot;&lt;br /&gt;&quot;;<br />echo &quot;Clare is being paid - $&quot; . $salaries[&quot;Clare&quot;];<br />?&gt;<br />
  12. 12. Php Functions<br />is a self-contained block of code that can be called by your scripts. When called, the function&apos;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.<br />function myCompanyMotto(){ <br /> //some codes<br /> } ?&gt;<br />
  13. 13. Sample 6<br />&lt;?php<br />function myname(){<br /> $name = &quot;your-name&quot;;<br />echo &quot;$name &lt;br /&gt;&quot;;<br />}<br />echo &quot;My Name is &lt;br /&gt;&quot;;<br />myname();<br />?&gt;<br />
  14. 14. A Function That Returns a Value<br />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.<br />function addNums( $firstnum, $secondnum ) {$result = $firstnum + $secondnum; <br /> return $result; <br /> }<br />
  15. 15. Sample 7<br />&lt;?php<br />function addNums( $firstnum, $secondnum ) {<br /> $result = $firstnum + $secondnum;<br /> return $result;<br /> }<br /> print addNums(3,5);<br /> // will print &quot;8&quot;<br />?&gt;<br />

×