php modul-2
TRANSCRIPT
![Page 1: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/1.jpg)
Control Statement
PHP - TUTORIAL
![Page 2: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/2.jpg)
Selection
●IF●IF … ELSE●ELSEIF●SWITCH
![Page 3: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/3.jpg)
Selection - IF●IF <expression>
statement-true;
●ex.
$name=”kris”;
IF ($name==”kris”)echo “Welcome Kris.”;
![Page 4: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/4.jpg)
Selection – IF … ELSE
●IF <expression>statement-true;
ELSEstatement-false;
![Page 5: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/5.jpg)
Selection – IF … ELSE●Ex.
$name=”Kris”;
IF ($name=”kris”)
echo “Welcome Kris.”;
ELSE
echo “You're not Kris.”;
![Page 6: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/6.jpg)
Selection - ELSEIF● IF <expression1>
statement-true1;
ELSEIF <expression2>
statement-true2;
ELSE
statement-false;
![Page 7: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/7.jpg)
Selection - ELSEIF
$name=”John”;
IF ($name==”Kris”)
echo “Welcome Kris.”;
ELSEIF ($name==”John”)
echo “Welcome John.”;
![Page 8: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/8.jpg)
Selection - SwitchSwitch (variable) {
case <value1>:statement-1;
break;
case <value2>:statement-2;
break;
default:statement-default;
break;
}
![Page 9: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/9.jpg)
Selection - Switch$destination=”Paris”;
switch ($destination) {
case “Tokyo”:
echo “Bring extra 1000 $US”;
break;
case “Egypt”:
echo “Bring bulletproof vests”;
break;
case “Paris”:
echo “Bring a camera”;
break;
default:
echo “Dont' bring anything”;
break;
}
![Page 10: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/10.jpg)
Loop
●While●Do While●For●Foreach
![Page 11: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/11.jpg)
Loop - While●While (expression-true) {
statement; }
●Ex.
$i=0;
While ($i<=10) {echo $i . “<br />”;
$i++;
}
![Page 12: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/12.jpg)
Loop – Do While
Do {
statement;
} while (expression);
![Page 13: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/13.jpg)
Loop – Do While
$a=0;
do {
echo $a . “<br/>”;
} while ($a>0);
![Page 14: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/14.jpg)
Loop - For●For (start; stop; increment) {
statement;
}●Ex.
For ($b=0; $b<=10; $b++) {
echo $b . “<br />”;
}
![Page 15: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/15.jpg)
Loop – Foreach● foreach ($array as $value){
statement;
}●Ex.
$colors = array("red","green","blue","yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
![Page 16: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/16.jpg)
Function● Is a block of code that can be executed
whenever we need it
● Function <name>(parameters) {
statement;
return
}
![Page 17: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/17.jpg)
Simple functionfunction say() {
echo “Nice to meet you.”;
}
echo “Hello Kris, ”;
say();
![Page 18: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/18.jpg)
Function with Parameters
function say($name) {
echo “<br />Nice to meet you, “. $name;
}
echo “Hello, John.”;
say(“John”);
![Page 19: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/19.jpg)
Simple functionfunction say() {
echo “Nice to meet you.”;
}
echo “Hello Kris, ”;
say();
![Page 20: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/20.jpg)
Function that returning values
function add($x, $y) {
$total = $x + $y;
return $total;
}
$num=0;
echo “My number is “ . $num . ”<br />”;
$num = add(5,6);
echo “Now, my number is “ . $num;
![Page 21: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/21.jpg)
Excercise1. Write a php program to determine which one is the biggest number
from 2 numbers
2. Write a php program to determine which one is the biggest number from 3 numbers
3. Write a php program which can determine a number is odd or even
4. Write number from 1 to 20 with while, do while, and for
5. Write even number from 1 to 20 with while, do while, and for
6. Write odd number from 1 to 20 with while, do while, and for
![Page 22: Php modul-2](https://reader034.vdocument.in/reader034/viewer/2022051212/5594b0dc1a28ab28648b4633/html5/thumbnails/22.jpg)
Homework● Write a php program to determine the letter grade from given number
input (hint: use a function to generate the letter grade)
● Add a function to exercise number 1, 2, and 3