Decisions, Loops, and Arrays

Slides:



Advertisements
Similar presentations
Creating PHP Pages Chapter 7 PHP Decisions Making.
Advertisements

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.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
IDK0040 Võrgurakendused I harjutus 07: PHP: Operators, Switch, Forms Deniss Kumlander.
Decisions, Loops, and Arrays Achmad Arwan, S.Kom.
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.
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.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
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.
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to PHP.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
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.
Working with Loops, Conditional Statements, and Arrays.
CSI 3125, Preliminaries, page 1 Control Statements.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
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.
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.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
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.
Click to edit Master subtitle style 03/02/11 Web And Coding Club IIT Bombay PHP (pre hypertext processor) By: Saurabh Goyal Apekshit Sharma.
PHP using MySQL Database for Web Development (part II)
Session 2 Basics of PHP.
>> Fundamental Concepts in PHP
Expressions and Control Flow in PHP
CHAPTER 4 DECISIONS & LOOPS
เอกสารประกอบการบรรยายรายวิชา Web Technology
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Loops in Java.
CHAPTER 5 SERVER SIDE SCRIPTING
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
JavaScript: Control Statements I
ITM 352 Flow-Control: Loops
Struktur Control : Decision
Basic PHP Lecture by Nutthapat Keawrattanapat
Arrays, For loop While loop Do while loop
Loop Control Structure.
Objectives In this chapter, you will:
Conditional Statements & Loops in JavaScript
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Control Structures Part 1
Basics (Cont...).
Conditional statement & LOOPS
BIT116: Scripting Lecture 6 Part 1
PROGRAM FLOWCHART Iteration Statements.
PHP an introduction.
Decision making and control functions
PHP PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA),
PHP CONDITIONAL STATEMENTS
LOOPING STRUCTURE Chapter - 7 Padasalai
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Decisions, Loops, and Arrays Achmad Arwan, S.Kom

IF Statements In PHP we have the following conditional statements: if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false if...elseif....else statement - use this statement to select one of several blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed

The IF Statements The if Statement Use the if statement to execute some code only if a specified condition is true. Syntax if (condition) code to be executed if condition is true; <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; ?> </body> </html>

The if...else Statement Use the if....else statement to execute some code if a condition is true and another code if a condition is false. if (condition)   code to be executed if condition is true; else   code to be executed if condition is false; <html> <body> <?php $d=date("D"); if ($d=="Fri")   echo "Have a nice weekend!"; else   echo "Have a nice day!"; ?> </body> </html>

The if...else Statement If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces: <html> <body> <?php $d=date("D"); if ($d=="Fri")   {   echo "Hello!<br />";   echo "Have a nice weekend!";   echo "See you on Monday!";   } ?> </body> </html>

The if...elseif....else Statement Use the if....elseif...else statement to select one of several blocks of code to be executed. if (condition)   code to be executed if condition is true; elseif (condition)   code to be executed if condition is true; else   code to be executed if condition is false; <html> <body> <?php $d=date("D"); if ($d=="Fri")   echo "Have a nice weekend!"; elseif ($d=="Sun")   echo "Have a nice Sunday!"; else   echo "Have a nice day!"; ?> </body> </html>

The PHP Switch Statement <html> <body> <?php $x=1; switch ($x) { case 1:   echo "Number 1";   break; case 2:   echo "Number 2";   break; case 3:   echo "Number 3";   break; default:   echo "No number between 1 and 3"; } ?> </body> </html> Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1:   code to be executed if n=label1;   break; case label2:   code to be executed if n=label2;   break; default:   code to be executed if n is different from both label1 and label2; }

PHP Loops In PHP, we have the following looping statements: 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

The while Loop The while loop executes a block of code while a condition is true. Syntax while (condition)   {   code to be executed;   } <html> <body> <?php $i=1; while($i<=5)   {   echo "The number is " . $i . "<br />";   $i++;   } ?> </body> </html>

The do...while Statement The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. Syntax do   {   code to be executed;   } while (condition); <html> <body> <?php $i=1; do   {   $i++;   echo "The number is " . $i . "<br />";   } while ($i<=5); ?> </body> </html>

The for Loop The for loop is used when you know in advance how many times the script should run. Syntax for (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) 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 iteration) <html> <body> <?php for ($i=1; $i<=5; $i++)   {   echo "The number is " . $i . "<br />";   } ?> </body> </html>

The foreach Loop The foreach loop is used to loop through 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. <html> <body> <?php $x=array("one","two","three"); foreach ($x as $value)   {   echo $value . "<br />";   } ?> </body> </html>

Arrays An array is a set of variables that all have the same name but a different index. Each member of the array is called an element. Each element's name must be followed by its index in square brackets: $nama[1]=“Adi”; $nama[2]=“Dina”;

Initialization of Arrays $author[0]="William Shakespeare"; $author = array ("William Shakespeare", "Franz Kafka"); $author = array (1=>"William Shakespeare", "Franz Kafka"); $author = array (“wl”=>"William Shakespeare", “Fk”=>"Franz Kafka");

Iterating through an Array for ($counter=1; $counter<51; $counter++) { echo"<BR>$author[$counter]"; } Or $counter = 1; while ($counter < 51) { echo"<br> $author[$counter]"; $counter = $counter + 1; }

current() and key() Functions PHP uses a pointer to keep track of which element it's at when it moves through an array. The pointer indicates the element that is currently being used by the script. You can use the current() function to view the value of that element. you can use the key() function to find out its index value. (Key is another name for index.)

current() and key() sample $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; $current_index_value = key($director); echo ($current_index_value); $current_contents = current($director); echo ($current_contents);

next() and prev() Functions These functions enable you to navigate through arrays by moving the pointer to the next or previous element in the array. next()  move to next array previous()  move to previous array

next() and prev() Functions $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; $director[]="Alfred Hitchcock"; next($director); $current_index_value = key($director); echo ($current_index_value); prev($director);

list() and each() Functions use the list() and each() functions to return only the elements in the array that contain data. Syntax while (list($element_index_value, $element_contents) = each($director)) { echo "<br>$element_index_value - $element_contents"; }

list() and each() Functions sample $director[4]="Orson Welles"; $director[1]="Carol Reed"; $director[93]="Fritz Lang"; $director[24]="Jacques Tourneur"; while (list($MickeyMouse, $DonaldDuck) = each ($Director)) { echo "<BR>$MickeyMouse - $DonaldDuck"; }

sort() function sort() is the most basic of the sorting functions. It takes the contents of the array and sorts them into alphabetical order. The function requires only an array name to sort the array: sort(ArrayName)

sort() sample <?php $director = array ("Orson Welles","Carol Reed", "Fritz Lang", "Jacques Tourneur"); echo “Before sort”; while (list($IndexValue, $DirectorName) = each ($Director)){ echo "<BR>$IndexValue - $DirectorName"; } sort($director); ?>

Multidimentional array <?php $shop = array( array( Title => "rose",                        Price => 1.25,                       Number => 15                      ),                array( Title => "daisy",                        Price => 0.75,                       Number => 25,                     ),                array( Title => "orchid",                        Price => 1.15,                       Number => 7                      )              ); ?> Title Price Number rose 1.25 15 daisy 0.75 25 orchid 1.15 7

Multidimentional Arrays <?php echo "<h1>Manual access to each element</h1>"; echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />"; echo $shop[1][0]." costs ".$shop[1][1]." and you get ".$shop[1][2]."<br />"; echo $shop[2][0]." costs ".$shop[2][1]." and you get ".$shop[2][2]."<br />"; echo "<h1>Using loops to display array elements</h1>"; echo "<ol>"; for ($row = 0; $row < 3; $row++) {     echo "<li><b>The row number $row</b>";     echo "<ul>";     for ($col = 0; $col < 3; $col++)     {         echo "<li>".$shop[$row][$col]."</li>";     }     echo "</ul>";     echo "</li>"; } echo "</ol>"; ?>

<?php echo “Thanks”; ?>