+ All Categories
Home > Technology > Php modul-2

Php modul-2

Date post: 02-Jul-2015
Category:
Upload: kristophorus-hadiono
View: 654 times
Download: 2 times
Share this document with a friend
22
Control Statement PHP - TUTORIAL
Transcript
Page 1: Php modul-2

Control Statement

PHP - TUTORIAL

Page 2: Php modul-2

Selection

●IF●IF … ELSE●ELSEIF●SWITCH

Page 3: Php modul-2

Selection - IF●IF <expression>

statement-true;

●ex.

$name=”kris”;

IF ($name==”kris”)echo “Welcome Kris.”;

Page 4: Php modul-2

Selection – IF … ELSE

●IF <expression>statement-true;

ELSEstatement-false;

Page 5: Php modul-2

Selection – IF … ELSE●Ex.

$name=”Kris”;

IF ($name=”kris”)

echo “Welcome Kris.”;

ELSE

echo “You're not Kris.”;

Page 6: Php modul-2

Selection - ELSEIF● IF <expression1>

statement-true1;

ELSEIF <expression2>

statement-true2;

ELSE

statement-false;

Page 7: Php modul-2

Selection - ELSEIF

$name=”John”;

IF ($name==”Kris”)

echo “Welcome Kris.”;

ELSEIF ($name==”John”)

echo “Welcome John.”;

Page 8: Php modul-2

Selection - SwitchSwitch (variable) {

case <value1>:statement-1;

break;

case <value2>:statement-2;

break;

default:statement-default;

break;

}

Page 9: Php modul-2

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

Loop

●While●Do While●For●Foreach

Page 11: Php modul-2

Loop - While●While (expression-true) {

statement; }

●Ex.

$i=0;

While ($i<=10) {echo $i . “<br />”;

$i++;

}

Page 12: Php modul-2

Loop – Do While

Do {

statement;

} while (expression);

Page 13: Php modul-2

Loop – Do While

$a=0;

do {

echo $a . “<br/>”;

} while ($a>0);

Page 14: Php modul-2

Loop - For●For (start; stop; increment) {

statement;

}●Ex.

For ($b=0; $b<=10; $b++) {

echo $b . “<br />”;

}

Page 15: Php modul-2

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

Function● Is a block of code that can be executed

whenever we need it

● Function <name>(parameters) {

statement;

return

}

Page 17: Php modul-2

Simple functionfunction say() {

echo “Nice to meet you.”;

}

echo “Hello Kris, ”;

say();

Page 18: Php modul-2

Function with Parameters

function say($name) {

echo “<br />Nice to meet you, “. $name;

}

echo “Hello, John.”;

say(“John”);

Page 19: Php modul-2

Simple functionfunction say() {

echo “Nice to meet you.”;

}

echo “Hello Kris, ”;

say();

Page 20: Php modul-2

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

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

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


Recommended