PHP Constructs Advance Database Management Systems Lab no.3.
-
Upload
dora-heath -
Category
Documents
-
view
243 -
download
0
Transcript of PHP Constructs Advance Database Management Systems Lab no.3.
![Page 1: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/1.jpg)
PHP Constructs
Advance Database Management Systems Lab no.3
![Page 2: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/2.jpg)
LAB Outline
• PHP Loops• PHP Functions• Practice of PHP Array functions from previous
Lab
![Page 3: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/3.jpg)
PHP Loops• While – 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 – 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 4: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/4.jpg)
While Loop• Syntax
while (condition) { code to be executed; }
• Example<?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; }?>
![Page 5: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/5.jpg)
While Loop
• Output of the example codeThe number is 1The number is 2The number is 3The number is 4The number is 5
![Page 6: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/6.jpg)
Do While Loop• Syntax
do { code to be executed; }while (condition);
• Example<?php
$i=1;do { $i++; echo "The number is " . $i . "<br />"; }while ($i<=5);
?>
![Page 7: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/7.jpg)
Do While Loop
• Output of the example codeThe number is 2The number is 3The number is 4The number is 5The number is 6
![Page 8: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/8.jpg)
For Loop
• Used when number of times a code/script (to be executed) is known in advance
• Syntaxfor (init; condition; increment) { code to be executed; }
• Parameters:– init: Mostly used to set a counter (but can be any code
to be executed once at the beginning of the loop)
![Page 9: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/9.jpg)
For Loop
– condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
– increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop)
• Note: Each of the loop parameters can be empty, or have multiple expressions (separated by commas)
![Page 10: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/10.jpg)
For Loop• Example
<?phpfor ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; }?>
• OutputThe number is 1The number is 2The number is 3The number is 4The number is 5
![Page 11: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/11.jpg)
For Each Loop• It is used to loop throuh arrays• Syntax
foreach ($array as $value) { code to be executed; }
– For every loop iteration, • the value of the current array element is assigned to $value • and the array pointer is moved by one, so on the next loop iteration, you'll be looking at the next array value.
![Page 12: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/12.jpg)
For Each Loop• Example
<?php$x=array("one","two","three");foreach ($x as $value) { echo $value . "<br />"; }
?>• Output
onetwothree
![Page 13: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/13.jpg)
PHP Functions
• In PHP, there are more than 700 built-in functions.
• A function will be executed by function call. • You may call a function from anywhere within
a page.• The function name can start with a letter or
underscore (not a number)
![Page 14: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/14.jpg)
PHP Functions
• An important use of function– To keep the browser from executing a script when
the page loads, you can put your script into a function.
![Page 15: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/15.jpg)
Creating a PHP Function• Syntax
function functionName(){code to be executed;}
• Example<?php
function writeCourse(){echo “Advanced DBMS";}echo “Course name is ";writeCourse();
?>
![Page 16: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/16.jpg)
Adding Parameters to Functions
• A parameter is just like a variable.• They are specified after the function name,
inside the parentheses• A function can take one or more parameters.
![Page 17: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/17.jpg)
Part of text
Concatenation operator
Example• <html>
<body><?phpfunction writeChocoName($name){echo $name . " Chocolate. <br />";} echo “The best is "; writeChocoName(“Milk");echo “Some people also like "; writeChocoName(“Dark");echo “Few may like "; writeChocoName(“White"); ?>
</body></html>
![Page 18: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/18.jpg)
• OutputThe best is Milk Chocolate.Some people also like Dark Chocolate.Few may like White Chocolate.
![Page 19: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/19.jpg)
Another Example (2 parameters)• <html>
<body><?phpfunction sum($x, $y){echo $x+$y ; }echo “The total score is: "; sum(10, 20);?>
</body></html>Output:The total score is 30
![Page 20: PHP Constructs Advance Database Management Systems Lab no.3.](https://reader036.fdocuments.in/reader036/viewer/2022082320/56649f155503460f94c2a772/html5/thumbnails/20.jpg)
Function Returning Value• <html>
<body><?phpfunction add($x,$y){$total=$x+$y;return $total;}
echo "1 + 16 = " . add(1,16);?></body></html>
Output:1 + 16 = 17