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

Similar documents
Princess Nourah bint Abdulrahman University. Computer Sciences Department

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

PHP Introduction. Some info on MySQL which we will cover in the next workshop...

Form Processing in PHP

M275 - Web Development using PHP and MySQL

PHP with data handling

INTERNET PROGRAMMING. Software Engineering Branch / 4 th Class Computer Engineering Department University of Technology

SCRIPTING, DATABASES, SYSTEM ARCHITECTURE

Hello everyone! Page 1. Your folder should look like this. To start with Run your XAMPP app and start your Apache and MySQL.

B. V. Patel Institute of BMC & IT 2014

Flow Control. Copyleft 2005, Binnur Kurt

Chapter 2: Functions and Control Structures

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.

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

LECTURE 5 Control Structures Part 2

Lecture 2 Unix and PHP. INLS 523 Web Databases Spring 2013 Rob Capra

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

WEBD 236 Web Information Systems Programming

ITS331 IT Laboratory I: (Laboratory #11) Session Handling

CS Homework 6. Deadline. Purpose. How to submit. Important notes. Problem 1. Spring CS Homework 6 p. 1

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

Submitting forms (client-side)

Chapter 7:- PHP. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Introduction to Databases. Key Concepts. Calling a PHP Script 5/17/2012 PHP I

PHP Hypertext Preprocessor

CPSC 481: CREATIVE INQUIRY TO WSBF

; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc. magic_quotes_runtime = Off

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

Subject Name: Advanced Web Programming Subject Code: (13MCA43) 1. what is PHP? Discuss different control statements

Web Programming/Scripting: PHP and AJAX Refresher

Flow Control 10. Flow Control. Content. if Statement. Conditional Statements. if Statement. Example. if, if/else, switch for, while, do/while

UNIT-VI CREATING AND USING FORMS

A QUICK GUIDE TO PROGRAMMING FOR THE WEB. ssh (then type your UBIT password when prompted)

PIC 40A. Lecture 19: PHP Form handling, session variables and regular expressions. Copyright 2011 Jukka Virtanen UCLA 1 05/25/12

BEGINNER PHP Table of Contents

PHP. M hiwa ahamad aziz Raparin univercity. 1 Web Design: Lecturer ( m hiwa ahmad aziz)

Q1. What is JavaScript?

Manju Muralidharan Priya. CS4PM Web Aesthetics and Development WEEK 12

COMS 469: Interactive Media II

Course Topics. The Three-Tier Architecture. Example 1: Airline reservations. IT360: Applied Database Systems. Introduction to PHP

JavaScript s role on the Web

Using PHP with MYSQL

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

Computer Programming: C++

Build a Subfile with PHP

SMS GATEWAY API INTEGRATION GUIDE

PHP. control structures - loops indexed arrays. Recap: PHP files are processed top to bottom in sequence. Starting at the top

Zend PHP 5.3 Certification Exam.

Unit IV- Server Side Technologies (PHP)

Databases HTML and PHP I. (GF Royle, N Spadaccini ) HTML/PHP I 1 / 28

Final Exam. IT 3203 Introduction to Web Development. Rescheduling Final Exams. PHP Arrays. Arrays as Hashes. Looping over Arrays

Branches, Conditional Statements

Database Connectivity using PHP Some Points to Remember:

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

Multimedia im Netz Online Multimedia Winter semester 2015/16. Tutorial 03 Minor Subject

<form>. input elements. </form>

IS 2150 / TEL 2810 Introduction to Security

Important Points about PHP:

Activity 1.1: Indexed Arrays in PHP

User authentication, passwords

CMPS 401 Survey of Programming Languages

1 Form Basics CSC309

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

4th year. more than 9 years. more than 6 years

Chapter 4: Programming with MATLAB

USQ/CSC2406 Web Publishing

CS4604 Prakash Spring 2016! Project 3, HTML and PHP. By Sorour Amiri and Shamimul Hasan April 20 th, 2016

Security issues. Unit 27 Web Server Scripting Extended Diploma in ICT 2016 Lecture: Phil Smith

CERTIFICATE IN WEB PROGRAMMING

Server-side Web Development (I3302) Semester: 1 Academic Year: 2017/2018 Credits: 5 (50 hours) Dr Antoun Yaacoub

PHP can also increase your productivity enormously, both in development time and maintenance time.

HTML Forms. By Jaroslav Mohapl

Lecture 5 Security and User Input. INLS 760 Web Databases Spring 2013 Rob Capra

Database Systems Fundamentals

Integrate Word and the Web Vocabulary

CSE 154 LECTURE 19: FORMS AND UPLOADING FILES

Introduction. Server-side Techniques. Introduction. 2 modes in the PHP processor:

Web Programming with PHP

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Course Topics. IT360: Applied Database Systems. Introduction to PHP

COM1004 Web and Internet Technology

Executing Simple Queries

Mini-Matlab Lesson 5: Functions and Loops

blink.html 1/1 lectures/6/src/ form.html 1/1 lectures/6/src/

Week 13 Thursday (with Page 5 corrections)

CSE 154 LECTURE 9: SUBMITTING DATA (POST)

PHP. MIT 6.470, IAP 2010 Yafim Landa

Quick Guide to TraiTel Outbound IVR. Traitel Telecommunications Ltd 2012 Telephone: (61) (2) Page 1

Dynamic Form Processing Tool Version 5.0 November 2014

cwhois Manual Copyright Vibralogix. All rights reserved.

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

CMPT 165 INTRODUCTION TO THE INTERNET AND THE WORLD WIDE WEB

Course Syllabus. Course Title. Who should attend? Course Description. PHP ( Level 1 (

Practice Test 3. 2) The onload event handler is used, most often, in association with which tag? a) a b) p c) body d) form

An Online Interactive Database Platform For Career Searching

Programming for the Web with PHP

Introduction. C provides two styles of flow control:

CSE 154 LECTURE 9: SUBMITTING DATA (POST)

CICS 515 b Internet Programming Week 2. Mike Feeley

HTML 5 Form Processing

Transcription:

PHP 5 if...else...elseif Statements Conditional statements are used to perform different actions based on different conditions. PHP Conditional Statements 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. In PHP we have the following conditional statements: if statement - executes some code only if a specified condition is true if...else statement - executes some code if a condition is true and another code if the condition is false if...elseif...else statement - specifies a new condition to test, if the first condition is false switch statement - selects one of many blocks of code to be executed PHP - The if Statement The if statement is used to execute some code only if a specified condition is true. if (condition) { code to be executed if condition is true; The example below will output "Have a good day!" if the current time (HOUR) is less than 20: Page 1 of 11

$t = date("h"); if ($t < "20") { echo "Have a good day!"; PHP - The if...else Statement Use the if...else statement to execute some code if a condition is true and another code if the condition is false. if (condition) { code to be executed if condition is true; else { code to be executed if condition is false; The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise: $t = date("h"); if ($t < "20") { echo "Have a good day!"; else { echo "Have a good night!"; PHP - The if...elseif...else Statement Use the if...elseif...else statement to specify a new condition to test, if the first condition is false. Page 2 of 11

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; The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!": $t = date("h"); if ($t < "10") { echo "Have a good morning!"; elseif ($t < "20") { echo "Have a good day!"; else { echo "Have a good night!"; The PHP switch Statement 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; case label2: code to be executed if n=label2; case label3: Page 3 of 11

code to be executed if n=label3;... default: code to be executed if n is different from all labels; This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found. $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; case "blue": echo "Your favorite color is blue!"; case "green": echo "Your favorite color is green!"; default: echo "Your favorite color is neither red, blue, or green!"; PHP Loops Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal codelines in a script, we can use loops to perform a task like this. In PHP, we have the following looping statements: while - loops through a block of code as long as the specified condition is true Page 4 of 11

do...while - loops through a block of code once, and then repeats the loop as long as the 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 PHP while Loop The while loop executes a block of code as long as the specified condition is true. while (condition is true) { code to be executed; The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++): $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; The PHP do...while Loop The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Page 5 of 11

do { code to be executed; while (condition is true); The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5: $x = 1; do { echo "The number is: $x <br>"; $x++; while ($x <= 5); Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time. The example below sets the $x variable to 6, then it runs the loop, and then the condition is checked: $x = 6; do { echo "The number is: $x <br>"; $x++; while ($x<=5); The PHP for Loop The for loop is used when you know in advance how many times the script should run. Page 6 of 11

for (init counter; test counter; increment counter) { code to be executed; Parameters: init counter: Initialize the loop counter value test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment counter: Increases the loop counter value The example below displays the numbers from 0 to 10: for ($x = 0; $x <= 10; $x++) { echo "The number is: $x <br>"; The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. 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, until it reaches the last array element. The following example demonstrates a loop that will output the values of the given array ($colors): $colors = array("red", "green", "blue", "yellow"); Page 7 of 11

foreach ($colors as $value) { echo "$value <br>"; PHP - A Simple HTML Form The example below displays a simple HTML form with two input fields and a submit button: <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html> When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this: <html> <body> Welcome echo $_POST["name"]; <br> Your email address is: echo $_POST["email"]; </body> </html> The output could be something like this: Welcome John Your email address is john.doe@example.com Page 8 of 11

The same result could also be achieved using the HTTP GET method: <html> <body> <form action="welcome_get.php" method="get"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit"> </form> </body> </html> and "welcome_get.php" looks like this: <html> <body> Welcome echo $_GET["name"]; <br> Your email address is: echo $_GET["email"]; </body> </html> The code above is quite simple. However, the most important thing is missing. You need to validate form data to protect your script from malicious code. GET vs. POST Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3,...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. $_GET is an array of variables passed to the current script via the URL parameters. Page 9 of 11

$_POST is an array of variables passed to the current script via the HTTP POST method. When to use GET? Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. GET may be used for sending non-sensitive data. Note: GET should NEVER be used for sending passwords or other sensitive information! When to use POST? Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. Moreover POST supports advanced functionality such as support for multipart binary input while uploading files to server. However, because the variables are not displayed in the URL, it is not possible to bookmark the page. Page 10 of 11

* required field. Name: * E-mail: * Website: H.W: PHP Form Validation Comment: Gender: Female Male * Submit Your Input: Page 11 of 11