CHAPTER 5 SERVER SIDE SCRIPTING

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Creating PHP Pages Chapter 7 PHP Decisions Making.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
ITC 240: Web Application Programming
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CSCI 116 Week 3 Functions & Control Structures. 2 Topics Using functions Using functions Variable scope and autoglobals Variable scope and autoglobals.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
INTERNET APPLICATION DEVELOPMENT For More visit:
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
PHP Constructs Advance Database Management Systems Lab no.3.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
JavaScript, Sixth Edition
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
Oleh: Ahmad Ramadhani, S.Kom
Session 2 Basics of PHP.
Expressions and Control Flow in PHP
REPETITION CONTROL STRUCTURE
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Functions.
8th Semester, Batch 2008 Department of Computer Science SSUET.
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Agenda Control Flow Statements Purpose test statement
Introduction to MATLAB
Arrays, For loop While loop Do while loop
Introduction to Web programming
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
CS139 October 11, 2004.
Arrays.
Objectives In this chapter, you will:
Basics.
MCS/BCS.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Basics (Cont...).
Decisions, Loops, and Arrays
Conditional statement & LOOPS
PHP an introduction.
ASP control structure BRANCHING STATEMENTS
PHP PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA),
CIS 136 Building Mobile Apps
PHP Array.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

CHAPTER 5 SERVER SIDE SCRIPTING PART 1 OF 3 [Control Statement| Array| Function] Madam Hazwani binti Rahmat http://fsktm2.uthm.edu.my/hazwani/v2/

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

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 

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; }

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

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; }

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

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

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; }

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; }

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!

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; }

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; }

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!

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

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; }

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

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; }

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

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; }

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; }

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

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; }

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

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

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; }

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(“10 + 20 =” .addFunction(10, 20)); ?> Output: 10+20=30

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; }

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

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

$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";

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

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

$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;

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

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; }

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

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) );

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