1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

PHP I.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
1 Chapter 5 – Handling HTML Controls in Web Pages spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Objectives Using functions to organize PHP code
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Uploading Files. Why? By giving a user the option to upload a file you are creating an interactive page You can enable users have a greater web experience.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Control Structures, Operators, and Functions.
PHP: Introduction By Trevor Adams.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
INTERNET APPLICATION DEVELOPMENT For More visit:
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
PHP H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Variables, Operators and Data Types. By Shyam Gurram.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
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.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
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.
PHP PHP: Hypertext Preprocesor Personal Home Page Tools.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
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.
Web Technology Introduction to PHP. PHP PHP stands for PHP Hypertext Preprocessor PHP is a Server-Side Web Scripting language, which executes on the web.
PHP - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
1 PHP Introduction Chapter 1. Syntax and language constructs.
Research Topics in Computational Science. Agenda Survey Overview.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
SESSIONS 27/2/12 Lecture 8. ? Operator Similar to the if statement but returns a value derived from one of two expressions by a colon. Syntax: (expression)
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
PHP WORKSHOP (Session 1) INFO 257 Supplement. Outline What is PHP ? PHP Language Basics PHP Exercise.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
PHP (Session 1) INFO 257 Supplement.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
DBW - PHP DBW2017.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
More Selections BIS1523 – Lecture 9.
In Class Program: Today in History
PHP Intro/Overview Bird Book pages 1-11,
PHP.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)

Format: if (expression) statement; Example: if ($a > $b) echo “a is larger than b”; Logical Expression: If Statement

or (if you have more than one line of code) if ($a > $b){ echo “a is larger than b”; }

EXAMPLE <?php $a = 4; $b = 3; if ($a > $b){ echo “a is larger than b”; } ?> Boolean Operators: >, >=, <, <=, ==, !=

EXAMPLE <?php $a = 3; $b = 4; if ($a > $b) { echo “a is larger than b”; } if ($a < $b){ echo “a is not larger than b”; } ?>

<?php $a = 3; $b = 4; if ($a > $b) { echo “a is larger than b”; } else { echo “a is not larger than b”; } ?>

EXAMPLE How to deal with different cases: <?php $a = 4; $b = 4; if ($a > $b) { echo “a is larger than b”; } elseif ($a == $b) { echo “a equals to b”; } else { echo “a is smaller than b”; } ?>

LOGICAL OPERATORS (AND) <?php $a = 5; $b = 4; ?> <?php $c = 20; $d = 1; if (($a > $b) && ($c > $d)){ echo “a is larger than b AND ”; echo “c is larger than d ”; } ?>

LOGICAL OPERATORS (OR) <?php $a = 5; $b = 4; ?> <?php $c = 1; $d = 20; if (($a > $b) || ($c > $d)){ echo “a is larger than b OR ”; echo “c is larger than d ”; } ?>

EXAMPLE If variable a is not set, then set equals to 100. <?php $a = 5; $b = 4; ?> <?php if (!isset($a)){ $a = 100; } echo $a; ?>

EXAMPLE If variable a is not set, then set equals to 100. <?php $a = 5; $b = 4; ?> <?php unset($a); if (!isset($a)){ $a = 100; } echo $a; ?>

If a is integer, then set its type equals to string. <?php $a = 5; $b = 4; ?> <?php if (is_int($a)) { settype($a, “string”); } echo gettype($a); ?>

Logical Expressions: Switch <?php switch ($a){ case 0: echo “a equals 0”; break; case 1: echo “a equals 1”; break; case 2: echo “a equals 2”; break; default: echo “a is not 0, 1 or 2”; break; } ?>

LOOPS Loops allow us to execute the code until the condition is satisfied. There are three main kinds of loops: while Loops for Loops foreach Loops

WHILE LOOPS The format of while loops is as under: while (expression) statement; The expression would be Boolean statement,

EXAMPLE <?php $count = 0; while ($count <= 10) { echo $count. “, ”; $count++; } echo “ Count: {$count}”; ?>

EXAMPLE <?php $count = 0; while ($count <= 10) { if ($count == 5) { echo “FIVE”; } else { echo $count. “, ”; } $count++; } echo “ Count: {$count}”; ?>

FOR LOOP The format of for loops is as under: for (expr1, expr2, expr3) statement; The expression would be boolean statement, Expression1 could be initializing statement. Expression2 could be condition/test. Expression3 could be increment/decrement.

EXAMPLE <?php for ($count = 0; $count <= 10; $count++ ) { echo $count. “, ”; } ?>

$_POST We use post method in a form. When we submit the button of form, all the data of form is send to $_POST array. We get this data on any other page from this super global array $_POST.

EXAMPLE Myform.php dataform.php <?php If($_POST) { $Name = $_POST[‘txtname’]; $Name =$_REQUEST[‘txtname’]; echo “Hello”. $Name; } ?>

$_GET T he data that we send through URL also known as query string is stored in this super global array $_GET. Query string started in URL after ? Sign. In a URL there may be more than one query string. stan&city=isb

EXAMPLE link.php Sending QueryString get.php <?php $siteName = $_GET[‘site’]; echo “Welcome to ”. $siteName; ?>

$_FILES When we upload a file or an image to server, it stores in $_FILES array. Example: User uploads a file from his computer c:\docs\project.zip Its size is 20,000 bytes PHP store in a temp folder, and gives it a random name. $_FILES[userfile][name] $_FILES[userfile][size] $_FILES[userfile][type]

EXAMPLE Upload_form.php Select image to upload:

Upload.php <?php move_uploaded_file($_FILES[' fileToUpload ']['tmp_name'], 'uploads/'.$new_file_name); $message = ‘Your file was accepted.'; ?>

FOREACH LOOP The format of foreach loops is as under: foreach ($array as $value) statement; The boolean test foreach loop is, do we have items still left in array. Foreach loop will go through every single element of an array. Foreach loop only works with array. Foreach loop works, it stores elements in array in a temporary variable

EXAMPLE <?php $agesArray = array(4, 8, 15, 17, 23, 42); ?> <?php // using each value foreach ($agesArray as $age) { echo $age. “, ”; } ?>

Syntax for associative array Foreach loop can take each array as a key value pair. foreach ($array as $key => $value) statement;

EXAMPLE <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php // using each key => value pair // key for 4 is 0, key for 8 is 1 and so on… foreach ($ages as $position => $age) { echo $position. “: ”. $age. “ ”; } ?>

<?php $prices = array(“Brand new computer”=>2000, “1 month in lynda.com training center”=>25, “Learning PHP”=> “priceless”); foreach ($prices as $key => $value) { if (is_int($value)) { echo $key. “: $”. $value. “ ”; } else { echo $key. “: ”. $value. “ ”; } ?>

LOOPS: Continue If we didn’t want to print let say 5 <?php for ($count=0; $count <=10; $count++) { if ($count == 5) { continue; } echo $count. “, ”; } ?> Output: 1, 2, 3, 4, 6, 7, 8, 9, 10,

Lets say we are looking through, a list of students in a database. We can perform lots of complex action on it. But the very first thing we check, whether or not the student is fresh man, s/w engineer jr. or senior. If we turns out, the student is fresh man, we don’t want anything else. Then we can have this check, are they fresh man, continue

LOOPS: Break If we didn’t want to print before 5 <?php for ($count=0; $count <=10; $count++) { if ($count == 5) { break; } echo $count. “, ”; } ?> Output: 1, 2, 3, 4,

If we don’t want comma (,) at the end of last number. <?php for ($count = 0; $count <= 10; $count++) { echo $count; if ($count == 10) { break; } echo “, ”; } ?> Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

POINTERS Computer maintains the pointer, that’s points to the current value of an array. By default its always the first value. When we looping through array, computer moves that pointer along the array to get each value.

EXAMPLE <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php echo “1: ”. current($ages). “ ”; // that will give us, the current place, the pointer is pointing. ?> Output: 1: 4

<?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php echo “1: ”. current($ages). “ ”; next($ages); echo “2: ”. current($ages). “ ”; reset($ages); echo “3: ”. current($ages). “ ”; ?> Output: 1: 4 2: 8 3: 15

EXAMPLE <?php $ages = array(4, 8, 15, 16, 23, 42); ?> <?php // while loop that moves the array pointer while ($age = current($ages)) { echo $age. “, ”; next($ages); } ?> Output: 4, 8, 15, 16, 23, 42,