Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 SERVER SIDE SCRIPTING

Similar presentations


Presentation on theme: "CHAPTER 5 SERVER SIDE SCRIPTING"— Presentation transcript:

1 CHAPTER 5 SERVER SIDE SCRIPTING
PART 1 OF 3 [Control Statement| Array| Function] Madam Hazwani binti Rahmat

2 CONTROL STATEMENT Control statement consist of a set of commands that executes if a specified condition is TRUE. Control statements are used to decide which lines of code are evaluated, or how many times to evaluate them. There are TWO different types of control statements: conditional statements loop statements

3 CONTROL STATEMENT Conditional statements are used to make decisions about which sections of code to evaluate.  Conditional statements perform different actions (decision) based on different conditions. PHP supports the following conditional statements: if statement  if...else statement  if...else if....else statement  switch statement 

4 CONTROL STATEMENT This statement is used to execute some code ONLY IF a specified condition is TRUE. There are TWO major parts to an if statement: Condition : an expression that will evaluate to be either TRUE or FALSE. Statement : the action (code) to be executed if the condition is TRUE. If the condition is TRUE, given statement(s) are executed. If expression is FALSE then NO statement would be executed. Syntax: if(condition) {   statement; }

5 CONTROL STATEMENT Example: Syntax: Condition : $a bigger than $b
Statement : display “$a is bigger than $b” Syntax: if (condition) { code to be executed if condition is TRUE; } <?php $a = 50; $b = $a - 10; // 50 – 10 if ($a > $b) {   echo(“$a is bigger than $b”); } ?> Output: 50 is bigger than 40

6 CONTROL STATEMENT This statement is used to execute some code if a specified condition is TRUE OR another code if the condition is FALSE. If the Condition is TRUE, it executes Statement1. If the Condition is FALSE, (Statement2) are executed . Syntax: if(condition) {   statement1; } else {   statement2; }

7 CONTROL STATEMENT Example: Syntax:
Condition (TRUE) : $name equals to Alan Statement 1: display “Hello Alan” Condition (FALSE) : $name not equals to Alan Statement 2: display “You are not Alan” Syntax: if (condition) { code to be executed if condition is TRUE; } else { code to be executed if condition is FALSE; } <?php $name = “Alex”; if ($name == “Alan”) {   echo(“Hello $name”); } else {   echo(“You are not $name“); } ?> Output: You are not Alex

8 Given statement is TRUE
CONTROL STATEMENT Example: Condition (TRUE) : $num equals to 3 Statement 1: display “Given statement is TRUE” Condition (FALSE) : $num not equals to 3 Statement 2: display “Given statement is FALSE” Syntax: if (condition) { code to be executed if condition is TRUE; } else { code to be executed if condition is FALSE; } <?php $num = 3; if ($num == 3) {   echo(“Given statement is TRUE”); } else {   echo(“Given statement is TRUE”); } ?> Output: Given statement is TRUE

9 CONTROL STATEMENT This statement is used to selects ONE of several blocks of code to be executed. Statements are executed based on the TRUE condition, if NONE of the condition is true then ELSE block is executed. Syntax: if(condition 1) {   statement1; } else if(condition 2) {   statement2; } else {   statement3; }

10 CONTROL STATEMENT Example: Syntax:
Condition 1: $name equals to Mdm Chan Statement 1: display “Hello Madam!” Condition 2: $name not equals to Jack Statement 2: display “Hello Jack!” Condition 3: $name not equals to Mdm Chan or Jack Statement 3: display “Hello!” Syntax: if(condition 1) {   statement1; } else if(condition 2) {   statement2; } else {   statement3; }

11 CONTROL STATEMENT Example: Syntax: Output: Hello Jack!
if(condition 1) {   statement1; } else if(condition 2) {   statement2; } else {   statement3; } <?php $name = “Jack”; if ($name == “Mdm Chan”) {   echo(“Hello $name!”); } else if ($name == “Jack”) {   echo(“Hello $name!“); } else {   echo(“Hello!“); } ?> Output: Hello Jack!

12 CONTROL STATEMENT This statement is used to perform DIFFERENT ACTIONS based on DIFFERENT CONDITIONS. The switch statement is used TO AVOID long blocks of if..elseif..else code. Each case is checked against the condition until a match is found. The break statements indicate end of that case. If nothing matches, a default condition will be used. Syntax: switch(n) { case label 1:statement1; break; case label 2:statement2; break; default:statement3; break; }

13 CONTROL STATEMENT Example: Case1: $color equals to red Syntax:
Statement 1: display “It’s red!” Case 2: $color equals to blue Statement 2: display “It’s blue!” Case 3: $color equals to green Statement 3: display “green!” Syntax: switch(n) { case label 1:statement1; break; case label 2:statement2; break; default:statement3; break; }

14 CONTROL STATEMENT Example: Syntax: Output: It’s red!
switch(n) { case label 1:statement1; break; case label 2:statement2; break; default:statement3; break; } <?php $color= “red”; switch($color) { case “red”: echo(“It’s red!”); break; case “blue”: echo(“It’s blue!”); break; default: echo(“It’s green!”); break; } ?> Output: It’s red!

15 CONTROL STATEMENT Loops in PHP are used to execute the same block of code a specified number of times or while a specified condition is true. Conditional statements perform different actions (decision) based on different conditions. PHP supports the following loops statements: for statement  while statement  foreach statement

16 CONTROL STATEMENT The for loop is used to execute the same statement for a specified number of times. The for loop is includes the following FOUR parts: initialization : initialize counter to a starting value. condition : test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will end. iteration : to increase or decrease your counter. statement : code to execute for each loop Syntax: for(init;cond;iteration) { statement; }

17 CONTROL STATEMENT Example: Syntax: Initialization: $i equals to 0
Condition: $i<3 Iteration: increase Statement: $a+2 Syntax: for(init;cond;iteration) { statement; } <?php $a = 0; for( $i=0; $i<3; $i++) { $a += 2; } echo("At the end a=$a"); ?> Output: At the end a=6

18 CONTROL STATEMENT The purpose of a while loop is to execute a statement or code block REPEATEDLY as long as expression is true. Once expression becomes false, the loop will be exited. There are TWO key parts to a while loop: condition conditional statement which must be TRUE for the while loop's code to be executed. Statement code within the block will be executed if the condition is TRUE. Syntax: while(condition) { statement; }

19 CONTROL STATEMENT Example: Syntax: Output:
while(condition) { statement; } <?php $i=1; while($i<=5) {   echo("The number is $i <br>“);   $i++; } ?> Output: The number is 1 The number is 2 The number is 3 The number is 4 The number is 5

20 FUNCTION To keep the script from being executed when the page loads, code can be placed into a function. A function is a piece of code which takes one more input in the form of parameter and does some processing and returns a value. This allows to only write the code once and save a lot of time and space. PHP offers several functions for different purpose. Syntax: function function_name(argument) { statement; }

21 CREATING A FUNCTION A function will ONLY be executed by a CALL to the function. A function can be called from anywhere within a page. A function’s name should start with keyword function and all the PHP code should be put inside { and } braces. Syntax: function function_name() { statement; }

22 CREATING A FUNCTION Example: Output: My name is Alex James Syntax:
function function_name() { statement; } <?php // creating a function function writeName() { echo(“Alex James“); } echo("My name is “); writeName(); // call the function ?> Output: My name is Alex James

23 ADDING FUNCTION PARAMETER
To add more functionality to a function, parameters are used. A parameter is just like a variable. Parameters are specified after the function name, INSIDE the parentheses. Syntax: function function_name(parameter) { statement; }

24 ADDING FUNCTION PARAMETER
Example: Syntax: function function_name(parameter) { statement; } <?php // creating a function function writeName($fname) { echo(“$fname Jones“); } echo("My name is “); // call the function, send argument writeName(“David”); ?> Output: My name is David Jones

25 ADDING FUNCTION PARAMETER
Example: Syntax: function function_name(parameter) { statement; } <?php // creating a function function addFunction($num1, $num2) { $sum = $num1 + $num2; echo(“Sum of the numbers : $sum”); } // call the function, send argument addFunction(10, 20); ?> Output: Sum of the numbers : 30

26 FUNCTION RETURN VALUES
To let a function return a value, the return statement are used. Return STOPS the execution of the function and sends the value back to the CALLING CODE. Syntax: function function_name(parameter) { statement; return $x; }

27 ADDING FUNCTION PARAMETER
Example: Syntax: function function_name(parameter) { statement; return $x; } <?php // creating a function function addFunction($num1, $num2) { $sum = $num1 + $num2; // return value to calling code return $sum; } // call the function, send argument (copies) echo(“ =” .addFunction(10, 20)); ?> Output: 10+20=30

28 PASSING ARGS BY REFERENCE
It is possible to pass arguments to functions by reference. This means that a reference to the variable is manipulated by the function rather than a copy of the variable's value. Any changes made to an argument in these cases will change the value of the original variable. Passing argument by reference are done by adding an ampersand to the variable name in either the function call OR the function definition. Syntax: function function_name(&parameter) { statement; return $x; }

29 ADDING FUNCTION PARAMETER
Syntax: function function_name(&parameter) { statement; return $x; } <?php $num1 = 10; $num2 = 5; echo(“Original num1 : $num1<br>”); echo(“Original num2 : $num2<br>”); // creating a function function addFunction($num1, &$num2) { $num1+= 1; $num2+= 2; } // call the function, send original values addFunction(&$num1, $num2); echo(“Updated num1 : $num1<br>”); echo(“Updated num2 : $num2”); ?> Output: Original num1 : 10 Original num2 : 5 Updated num1 : 11 Updated num2 : 7

30 ARRAY Arrays are special variables which can hold more than one value, each stored in its own numbered 'space' in the array. In PHP, the array() function is used to create an array: There are THREE types of array: Indexed arrays - Arrays with numeric index Associative arrays - Arrays with strings as index (keys) Multidimensional arrays - Arrays containing one or more arrays

31 $cars=array("Volvo","BMW","Toyota");
INDEXED ARRAY There are TWO ways to create indexed arrays: The index can be assigned automatically (index always starts at 0): $cars=array("Volvo","BMW","Toyota"); or the index can be assigned individually: $cars[]="Volvo"; $cars[0]="Volvo"; $cars[]="BMW"; $cars[1]="BMW"; $cars[]="Toyota"; $cars[2]="Toyota";

32 PRINTING INDEXED ARRAY
Indexed arrays are referred using the index number. Example: <?php $cars=array("Volvo","BMW","Toyota"); echo("I like " .$cars[0]. “, " .$cars[1]. “ and " .$cars[2]; echo(“<br>”); echo("The second element is $cars[2]“); ?> Output: I like Volvo, BMW and Toyota The second element is Toyota

33 INDEXED ARRAY AND LOOPS
Example: <?php $names=array(“John",“David",“Alex"); $x = 0; while ($x < 3) { $namenumber = $x + 1; echo("Name $namenumber is $names[$x]<br>“); ++$x; } ?> Output: Name 1 is John Name 2 is David Name 3 is Alex

34 $variableName=array(keyName1=>value1);
ASSOCIATIVE ARRAY The associative array indexes the contained elements by strings. The array are created using the “=” and the “>”, like: key=>”value”. There are TWO ways to create associative arrays: The index can be assigned automatically (index always starts at 0): $variableName=array(keyName1=>value1); Or the index can be assigned individually: $cars[keyName1]= value1; $cars[keyName2]= value2; $cars[keyName3]= value3;

35 PRINTING ASSOCIATIVE ARRAY
Indexed arrays are referred using the key. Example: <?php $age=array("Peter"=>35,"Ben"=>37,"Joe"=>43); echo("Peter is " .$age[Peter]. “years old“); echo(“Ben is $age[Ben] years old“); ?> Output: Peter is 35 years old Ben is 37 years old

36 CONTROL STATEMENT The foreach loop is used to loop through arrays.
For each loop iteration, the index of the current array element is assigned to $key For each loop iteration, the value of the current array element is assigned to $value Syntax: foreach($array as $key=>$value) { statement; }

37 ASSOCIATIVE ARRAY AND LOOPS
Example: <?php $age=array("Peter"=>35,"Ben"=>37,"Joe"=>43); foreach($age as $key=>$value) { $namenumber = $x + 1; echo(“Key=$key, value=$value“); echo(“<br>”); } ?> Output: Key=Peter, value=35 Key=Ben, value=37 Key=Joe, value=43

38 MULTIDIMENSIONAL ARRAY
In a multi-dimensional array, each element in the main array can also be an array. And, each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index. Example: two dimensional array to store marks of 3 students in 3 subjects: $marks = array( mohammad => array(physics => 35, maths => 30, chemistry => 39), qadir => array(physics => 30, maths => 32, chemistry => 29), zara => array(physics => 31, maths => 22, chemistry => 39) );

39 PRINTING MULTIDIMENSIONAL ARRAY
Example: <?php $marks = array( mohammad => array(physics => 35, maths => 30, chemistry => 39), qadir => array(physics => 30, maths => 32, chemistry => 29) ); foreach($marks as $key=>$value) { echo("Marks for $key in : <br>"); foreach($value as $key=>$value) echo($key.": " .$value. "<br>"); } echo("<br>"); ?> Output: Marks for mohammad in :  physics: 35 maths: 30 chemistry: 39 Marks for qadir in :  physics: 30 maths: 32 chemistry: 29


Download ppt "CHAPTER 5 SERVER SIDE SCRIPTING"

Similar presentations


Ads by Google