M275 - Web Development using PHP and MySQL

Similar documents
Chapter 2: Functions and Control Structures

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Introduction. C provides two styles of flow control:

PHP 5 if...else...elseif Statements

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Controlled Assessment Task. Question 1 - Describe how this HTML code produces the form displayed in the browser.

PHP Reference. To access MySQL manually, run the following command on the machine, called Sources, where MySQL and PhP have been installed:

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Chapter 9 - JavaScript: Control Structures II

Computational Expression

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

What is PHP? [1] Figure 1 [1]

c122sep2214.notebook September 22, 2014

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

Working with JavaScript

Chapter 10 JavaScript/JScript: Control Structures II 289

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Creating HTML files using Notepad

The PHP language. Teaching you everything about PHP? Not exactly Goal: teach you how to interact with a database via web

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Theory of control structures

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

5. Control Statements

Computational Physics - Fortran February 1997

Bourne Shell Reference

PHP. Interactive Web Systems

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

HTMLnotesS15.notebook. January 25, 2015

LECTURE 5 Control Structures Part 2

Decision Making in C

Mobile Site Development

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

8. Control statements

Suppose for instance, that a client demands the following page (example1.php).

Lecture 12. PHP. cp476 PHP

Programming for Experimental Research. Flow Control

Dynamism and Detection

Lab 8 CSE 3,Fall 2017

Flow Control. CSC215 Lecture

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Accelerating Information Technology Innovation

Javascript Lesson 3: Controlled Structures ANDREY KUTASH

1. Please, please, please look at the style sheets job aid that I sent to you some time ago in conjunction with this document.

Formatting & Style Examples

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

INFS 2150 Introduction to Web Development and e-commerce Technology. Programming with JavaScript

Objectives. Introduction to JavaScript. Introduction to JavaScript INFS Peter Y. Wu, RMU 1

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

Software Design & Programming I

M275 - Web Development using PHP and MySQL

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Branches, Conditional Statements

Chapter 7: Javascript: Control Statements. Background and Terminology. Background and Terminology. Background and Terminology

Title: Jan 29 11:03 AM (1 of 23) Note that I have now added color and some alignment to the middle and to the right on this example.

Important Points about PHP:

recall: a Web page is a text document that contains additional formatting information in the HyperText Markup Language (HTML)

Information Science 1

Options. Real SQL Programming 1. Stored Procedures. Embedded SQL

ECE 202 LAB 3 ADVANCED MATLAB

Boolean evaluation and if statements. Making decisions in programs

ECE 122. Engineering Problem Solving with Java

COMS 469: Interactive Media II

CSC 121 Computers and Scientific Thinking

PHP Syntax. PHP is a great example of a commonly-used modern programming language.

Control structures in C. Going beyond sequential

CSC Web Programming. Introduction to JavaScript

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS111: PROGRAMMING LANGUAGE II

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

SELECTION. (Chapter 2)

Language Fundamentals Summary

Chapter 4 The If Then Statement

Chapter 4: Programming with MATLAB

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

Computer Programming: C++

Html basics Course Outline

PHP. Introduction. PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Assignments (4) Assessment as per Schedule (2)

PHP: The Basics CISC 282. October 18, Approach Thus Far

$this->dbtype = "mysql"; // Change this if you are not running a mysql database server. Note, the publishing solution has only been tested on MySQL.

Conditionals and Loops

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

CGS 3066: Spring 2015 JavaScript Reference

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

These are notes for the third lecture; if statements and loops.

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Copyright 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 11 Introduction to PHP

Using htmlarea & a Database to Maintain Content on a Website

A Balanced Introduction to Computer Science, 3/E

Transcription:

Arab Open University Faculty of computer Studies M275 - Web Development using PHP and MySQL Chapter 6 Flow Control Functions in PHP Summary This is a supporting material to chapter 6. This summary will never substitute original text book. Haifaa Elayyan 2/8/2013

I. Switching Flow 1) The if Statement Why it being used : Flow Control Functions in PHP. The if statement is a way of controlling the execution of a statement that follows it (that is, a single statement or a block of code inside braces). How it works : The if statement evaluates an expression found between parentheses. If this expression results in a true value, the statement is executed. Otherwise, the statement is skipped entirely. if (expression) { // code to execute if the expression evaluates to true 2) Using the else Clause with the if Statement When working with an if statement, you might want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code: if (expression) { // code to execute if the expression evaluates to true else { // code to execute in all other cases : An if Statement 2: $mood = happy ; 3: if ($mood == happy ) { 4: echo Hooray! I m in a good mood! ; 5: 6?> When you access this script through your web browser, it produces the following output: Hooray! I m in a good mood! 2 P a g e

If you change the assigned value of $mood to sad or any other string besides happy, and then run the script again, the expression in the if statement evaluates to false, and the code block is skipped. The script remains silent, which leads to the else clause. 3) Using the else Clause with the if Statement When working with an if statement, you might want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code: if (expression) { // code to execute if the expression evaluates to true else { // code to execute in all other cases : An if Statement That Uses else 2: $mood = sad ; 3: if ($mood == happy ) { 4: echo Hooray! I m in a good mood! ; 5: else { 6: echo I m in a $mood mood. ; 7: 8:?> 4) Using the elseif Clause with the if Statement You can use an if...elseif...else clause to test multiple expressions (the if...else portion) before offering a default block of code (the elseif portion): if (expression) { // code to execute if the expression evaluates to true elseif (another expression) { // code to execute if the previous expression failed // and this one evaluates to true else { // code to execute in all other cases An if Statement That Uses else and elseif 2: $mood = sad ; 3: if ($mood == happy ) { 4: echo Hooray! I m in a good mood! ; 3 P a g e

5: elseif ($mood == sad ) { 6: echo Awww. Don t be down! ; 7: else { 8: echo I m neither happy nor sad, but $mood. ; 9: 10:?> 5) The switch Statement The switch statement is an alternative way of changing flow, based on the evaluation of an expression switch (expression) { case result1: // execute this if expression results in result1 break; case result2: // execute this if expression results in result2 break; default: // execute this if no break statement // has been encountered hitherto NOTE: It is important to include a break statement at the end of any code that will be executed as part of a case statement. Without a break statement, the program flow continues to the next case statement and ultimately to the default statement.in most cases, this results in unexpected behavior, likely incorrect! : A switch Statement 2: $mood = sad ; 3: switch ($mood) { 4: case happy : 5: echo Hooray! I m in a good mood! ; 6: break; 7: case sad : 8: echo Awww. Don t be down! ; 9: break; 10: default: 11: echo I m neither happy nor sad, but $mood. ; 12: break; 13: 14:?> 4 P a g e

6) Using the?: Operator The?: or ternary operator is similar to the if statement, except that it returns a value derived from one of two expressions separated by a colon. (expression)? returned_if_expression_is_true : returned_if_expression_is_false; Using the?: Operator 2: $mood = sad ; 3: $text = ($mood == happy )? I am in a good mood! : I am in a $mood mood. ; 4: echo $text ; 5:?> II. Loops 1) The while Statement The while statement looks similar in structure to a basic if statement, but has the ability to loop: while (expression) { // do something A while Statement 2: $counter = 1; 3: while ($counter <= 12) { 4: echo $counter. times 2 is.($counter * 2). <br /> ; 5: $counter++; 6: 7:?> Out put : 1 times 2 is 2 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 5 times 2 is 10 5 P a g e

6 times 2 is 12 7 times 2 is 14 8 times 2 is 16 9 times 2 is 18 10 times 2 is 20 11 times 2 is 22 12 times 2 is 24 2) The do...while Statement A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it: do { // code to be executed while (expression); The do...while Statement : 2: $num = 1; 3: do { 4: echo The number is:.$num. <br /> ; 5: $num++; 6: while (($num > 200) && ($num < 400)); 7:?> 3) The for Statement Definition ; for (initialization expression; test expression; modification expression) { // code to be executed NOTE: Infinite loops are, as the name suggests, loops that run without bounds. If yourloop is running infinitely, your script is running for an infinite amount of time. This behavior is very stressful on your web server and renders the web page unusable. Using the for Statement 2: for ($counter=1; $counter<=12; $counter++) { 3: echo $counter. times 2 is.($counter * 2). <br /> ; 4: 6 P a g e

5:?> Output : 1 times 2 is 2 2 times 2 is 4 3 times 2 is 6 4 times 2 is 8 5 times 2 is 10 6 times 2 is 12 7 times 2 is 14 8 times 2 is 16 9 times 2 is 18 10 times 2 is 20 11 times 2 is 22 12 times 2 is 24 III. Breaking Out of Loops with the break Statement Both while and for statements incorporate a built-in test expression with which you can end a loop. However, the break statement enables you to break out of a loop based on the results of additional tests. The following example creates a simple for statement that divides a large number by a variable that is incremented, printing the result to the screen. A for Loop That Divides 4000 by 10 Incremental Numbers 2: for ($counter=1; $counter <= 10; $counter++) { 3: $temp = 4000/$counter; 4: echo 4000 divided by.$counter. is....$temp. <br /> ; 5: 6:?> When you access this script through your web browser, it produces the following output: 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.444444444 4000 divided by 10 is... 400 Using the break Statement 2: $counter = -4; 3: for (; $counter <= 10; $counter++) { 7 P a g e

4: if ($counter == 0) { 5: break; 6: else { 7: $temp = 4000/$counter; 8: echo 4000 divided by.$counter. is....$temp. <br /> ; 9: 10: 11?> NOTE: Dividing a number by 0 does not cause a fatal error in PHP. Instead, PHP generates a warning and execution continues. When you access this script through your web browser, it produces the following output: 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 1) Skipping an Iteration with the continue Statement The continue statement ends execution of the current iteration but doesn t cause the loop as a whole to end. Instead, the next iteration begins immediately : Using the continue Statement 2: $counter = -4; 3: for (; $counter <= 10; $counter++) { 4: if ($counter == 0) { 5: continue; 6: 7: $temp = 4000/$counter; 8: echo 4000 divided by.$counter. is....$temp. <br /> ; 9: 10:?> When you access this script through your web browser, it produces the following output: 4000 divided by -4 is... -1000 4000 divided by -3 is... -1333.33333333 4000 divided by -2 is... -2000 4000 divided by -1 is... -4000 4000 divided by 1 is... 4000 4000 divided by 2 is... 2000 4000 divided by 3 is... 1333.33333333 4000 divided by 4 is... 1000 4000 divided by 5 is... 800 8 P a g e

4000 divided by 6 is... 666.666666667 4000 divided by 7 is... 571.428571429 4000 divided by 8 is... 500 4000 divided by 9 is... 444.44444444444 4000 divided by 10 is... 400 2) Nesting Loops Loops can contain other loop statements, as long as the logic is valid and the loops are tidy. The combination of such statements proves particularly useful when working with dynamically created HTML tables. Listing 6.12 uses two for statements to print a multiplication table to the browser. Nesting Two for Loops 2: echo <table style=\ border: 1px solid #000;\ > \n ; 3: for ($y=1; $y<=12; $y++) { 4: echo <tr> \n ; 5: for ($x=1; $x<=12; $x++) { 6: echo <td style=\ border: 1px solid #000; width: 25px; 7: text-align:center;\ > ; 8: echo ($x * $y); 9: echo </td> \n ; 10: 11: echo </tr> \n ; 12: 13: echo </table> ; 14:?> 3) Code Blocks and Browser Output Imagine a script that outputs a table of values only when a variable is set to the Boolean value true. The following example shows a simplified HTML table constructed with the code block of an if statement A Code Block Containing Multiple echo Statements 2: $display_prices = true; 3: if ($display_prices) { 4: echo <table border=\ 1\ >\n ; 5: echo <tr><td colspan=\ 3\ > ; 6: echo today s prices in dollars ; 7: echo </td></tr> ; 8: echo <tr><td>\$14.00</td><td>\$32.00</td><td>\$71.00</td></tr>\n ; 9: echo </table> ; 10: 11:?> 9 P a g e

If the value of $display_prices is set to true in line 2, the table is printed. For the sake of readability, we split the output into multiple echo() statements, and once again use the backslash to escape any quotation marks used in the HTML output IV. Summary In this chapter, you learned about control structures and the ways in which they can help to make your scripts flexible and dynamic. Most of these structures reappear regularly throughout the rest of the book. You learned how to define an if statement and how to provide for alternative actions with the elseif and else clauses. You learned how to use the switch statement to change flow according to multiple equivalence tests on the result of an expression. You learned about loops in particular, the while and for statements and you learned how to use break and continue to prematurely end the execution of a loop or to skip an iteration. You learned how to nest one loop within another and saw a typical use for this structure. You also looked at a technique for using PHP start and end tags in conjunction with conditional code blocks, to alleviate having to escape (use the backslash in front of) special characters such as the quotation mark and dollar sign. 10 P a g e