2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.

Slides:



Advertisements
Similar presentations
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
Advertisements

Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: The Basics.
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.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Lights, Camera, ActionScripts Web Design ½ Shade Adetoro.
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.
IST Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., Which contains three.
An Introduction to PHP The University of Tennessee at Chattanooga C. Daniel Chase “An introduction to basic PHP use with a focus on the power of dynamic.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
What is PHP? IDIA Fall 2014 Bridget M Blodgett.
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.
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.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
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 supports.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP: The Basics.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
OperatorstMyn1 Operators An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value.
FP512 WEB PROGRAMMING 1 PREPARED BY: PN. NUR SYUHADA BINTI MOHAMAD.
 Arithmetic Operator :  Increment / Decrement Operator :  Assignment Operator :  Comparision Operator :  Logical Operator :  Ternary Operator : 
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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,
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
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.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
PHP Workshop ‹#› أطلق إبداعك 2 أطلق إبداعك 2 مدرس معتمد من مركز زووم PHP: Moving On..
PHP using MySQL Database for Web Development (part II)
Chapter 7 JavaScript: Control Statements, Part 1
>> Fundamental Concepts in PHP
ITM 352 Expressions, Precedence, Working with Strings Class #5
PHP Introduction.
PHP.
Objectives In this chapter, you will:
Basics (Cont...).
C Programming Getting started Variables Basic C operators Conditionals
Presentation transcript:

2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap

2010/11 : [2]Building Web Applications using MySQL and PHP (W1)PHP Recap We need to remember.. Variables, arrays. Control structures. Logic. Functions.

2010/11 : [3]Building Web Applications using MySQL and PHP (W1)PHP Recap The manual will help! If in doubt, refer to the PHP manual.

2010/11 : [4]Building Web Applications using MySQL and PHP (W1)PHP Recap Fundamentals PHP is embedded within xhtml pages within the tags: The short version of these tags can also be used:, but should be avoided as most servers don’t support them Each line of PHP is terminated with a semi-colon.

2010/11 : [5]Building Web Applications using MySQL and PHP (W1)PHP Recap Comments <?php // this is a comment echo ‘Hello World!’; /* another multi-line comment */ ?>

2010/11 : [6]Building Web Applications using MySQL and PHP (W1)PHP Recap Variables $ followed by a variable name <?php $name = ‘Phil’; $age = 23; echo $name; echo ’ is ‘; echo $age; // Phil is 23 ?>

2010/11 : [7]Building Web Applications using MySQL and PHP (W1)PHP Recap " or ' There is a difference between strings written in single and double quotes. In a double-quoted string any variable names are expanded to their values. In a single-quoted string, no variable expansion takes place.

2010/11 : [8]Building Web Applications using MySQL and PHP (W1)PHP Recap " or ' <?php $name = ‘Phil’; $age = 23; echo “$name is $age”; // Phil is 23 ?>

2010/11 : [9]Building Web Applications using MySQL and PHP (W1)PHP Recap Expressions Using variables within expressions to do something is what PHP is all about. <?php $name = ‘Ian’; echo $name; ?> Expression Operator

2010/11 : [10]Building Web Applications using MySQL and PHP (W1)PHP Recap String Operators Use a dot to concatenate two strings: e.g. $firstname = ‘Gerard’; $surname = ‘Luskin’; // displays ‘Gerard Luskin’ echo $firstname.’ ‘.$surname;

2010/11 : [11]Building Web Applications using MySQL and PHP (W1)PHP Recap Arithmetic Operators ExampleNameResult $a + $b Addition Sum of $a and $b. $a - $b Subtraction Difference of $a and $b. $a * $b Multiplication Product of $a and $b. $a / $b Division Quotient of $a and $b. $a % $b Modulus Remainder of $a divided by $b.

2010/11 : [12]Building Web Applications using MySQL and PHP (W1)PHP Recap Comparison Operators ExampleNameResult $a == $b Equal TRUE if $a is equal to $b. $a != $b Not equal TRUE if $a is not equal to $b. $a <> $b Not equal TRUE if $a is not equal to $b. $a < $b Less than TRUE if $a is strictly less than $b. $a > $b Greater than TRUE if $a is strictly greater than $b. $a <= $b Less than or equal to TRUE if $a is less than or equal to $b. $a >= $b Gtr than or equal to TRUE if $a is greater than or equal to $b.

2010/11 : [13]Building Web Applications using MySQL and PHP (W1)PHP Recap Incrementing/Decrementing ExampleNameEffect $a++ Post-increment Returns $a, then increments $a by one. $a-- Post-decrement Returns $a, then decrements $a by one. You might also see these, but we strongly recommend that you don’t use them: --$a Pre-decrement Decrements $a by one, then returns $a. ++$a Pre-increment Increments $a by one, then returns $a.

2010/11 : [14]Building Web Applications using MySQL and PHP (W1)PHP Recap Logical Operators ExampleNameResult $a and $b And TRUE if both $a and $b are TRUE. $a or $b Or TRUE if either $a or $b is TRUE. $a xor $b Xor TRUE if either $a or $b is TRUE, but not both. !$a Not TRUE if $a is not TRUE. $a && $b And TRUE if both $a and $b are TRUE. $a || $b Or TRUE if either $a or $b is TRUE.

2010/11 : [15]Building Web Applications using MySQL and PHP (W1)PHP Recap An array Normal Variable, no key: $name = ‘Gerard’; Array Variable, multiple pieces with ‘keys’: $name[0] = ‘Gerard’; $name[1] = ‘Dionisis’; $name[2] = ‘Ian’; … The ‘key’

2010/11 : [16]Building Web Applications using MySQL and PHP (W1)PHP Recap Array keys Array keys can be strings as well as numbers.. $surname[‘gerard’] = ‘Luskin’; $surname[‘ian’] = ‘Harrison’; Notice the way that the key is specified, in square brackets following the variable name.

2010/11 : [17]Building Web Applications using MySQL and PHP (W1)PHP Recap Working with arrays.. Create Array (automatic keys): $letters = array('a','b','c','d'); The array keys are automatically assigned by PHP as 0, 1, 2, 3 i.e. $letters[1] has value ‘b’ Create Array (explicit keys): $letters = array(10=>’a’,13=>’b’); i.e. $letters[13] has value ‘b’

2010/11 : [18]Building Web Applications using MySQL and PHP (W1)PHP Recap Working with arrays… Create array (component by component): $letters[10] = ‘a’; $letters[13] = ‘b’; Access array component: echo $letters[10]; // displays a echo $letters[10].$letters[13]; // displays ab

2010/11 : [19]Building Web Applications using MySQL and PHP (W1)PHP Recap Working with arrays… Note that trying to echo an entire array will not display the data. To print an entire array to screen (for debug, for example) use the function print_r instead. echo $letters; print_r($letters);

2010/11 : [20]Building Web Applications using MySQL and PHP (W1)PHP Recap If … To do something depending on a comparison, use an if statement. if (comparison) { expressions; // do if TRUE } NB: Notice the curly brackets – these are important!

2010/11 : [21]Building Web Applications using MySQL and PHP (W1)PHP Recap If example <?php $a = 10; $b = 13; if ($a<$b) { echo ‘ a is smaller than b ’; } ?>

2010/11 : [22]Building Web Applications using MySQL and PHP (W1)PHP Recap Extending IF statements It is possible to add extra optional clauses to if statements.. if (comparison) { expressions; // do if TRUE } else { expressions; // do otherwise }

2010/11 : [23]Building Web Applications using MySQL and PHP (W1)PHP Recap Extending IF statements if (comparison1) { expressions; } elseif (comparison2) { expressions; } else { expressions; }

2010/11 : [24]Building Web Applications using MySQL and PHP (W1)PHP Recap An example.. $a = 10; $b = 13; echo ‘ ’; if ($a<$b) { echo ‘a is smaller than b’; } elseif ($a==$b) { echo ‘a is equal to b’; } else { echo ‘a is bigger than b’; } echo ‘ ’;

2010/11 : [25]Building Web Applications using MySQL and PHP (W1)PHP Recap While loops Might want to do something repeatedly while a comparison is true.. while (comparison) { expressions; }

2010/11 : [26]Building Web Applications using MySQL and PHP (W1)PHP Recap Example Lets count to 10! Displays 1,2,3,4,5,..,10: $i = 1; while ($i <= 10) { echo $i++; }

2010/11 : [27]Building Web Applications using MySQL and PHP (W1)PHP Recap For loop Sometimes we want to loop around the same bit of code a number of times.. Use a for loop. for (expr1; expr2; expr3) { statements; } expr1 evaluated/executed initially expr2 evaluated at beginning of each iteration (Continues if TRUE ) expr3 evaluated/executed at end of each iteration

2010/11 : [28]Building Web Applications using MySQL and PHP (W1)PHP Recap For loop example To count from 1 to 10: for ($i=1; $i<=10; $i++) { echo $i; } initialise Continue if true Execute at end of loop

2010/11 : [29]Building Web Applications using MySQL and PHP (W1)PHP Recap Foreach loop A foreach loop is designed for arrays. Often you want to loop through each item in an array in turn.. $letters = array(‘a’,’b’,’c’); foreach ($letters as $value) { echo $value; } // outputs a,b,c in turn

2010/11 : [30]Building Web Applications using MySQL and PHP (W1)PHP Recap require, include require('filename.ext') Includes and evaluates the specified file Error is fatal (will halt processing) include('filename.ext') Includes and evaluates the specified file Error is a warning (processing continues) require_once / include_once If already included won’t be included again

2010/11 : [31]Building Web Applications using MySQL and PHP (W1)PHP Recap Code Re-use Often you will want to write a piece of code and re-use it several times (maybe within the same script, or maybe between different scripts). Functions are a very nice way to encapsulate such pieces of code..

2010/11 : [32]Building Web Applications using MySQL and PHP (W1)PHP Recap What is a function? A function takes some arguments (inputs) and does something with them (echo, for example, outputs the text input to the user). As well as the inbuilt PHP functions, we can define our own functions..

2010/11 : [33]Building Web Applications using MySQL and PHP (W1)PHP Recap Definition vs. Calling There are two distinct aspects to functions: Definition: Before using a function, that function must be defined – i.e. what inputs does it need, and what does it do with them? Calling: When you call a function, you actually execute the code in the function.

2010/11 : [34]Building Web Applications using MySQL and PHP (W1)PHP Recap Function Definition A function accepts any number of input arguments, and returns a SINGLE value. function myfunction($arg1,$arg2,…,$argN) { statements; return $return_value; }

2010/11 : [35]Building Web Applications using MySQL and PHP (W1)PHP Recap Example Function to join first and last names together with a space.. function make_name($first,$last) { $fullname = $first.’ ‘.$last; return $fullname; }

2010/11 : [36]Building Web Applications using MySQL and PHP (W1)PHP Recap Calling functions.. Can be done anywhere.. myfunction($arg1,$arg2,…,$argN) or $answer = myfunction($arg1,$arg2,…,$argN) e.g. echo make_name(‘Gerard’,’Luskin’); // echoes ‘Gerard Luskin’

2010/11 : [37]Building Web Applications using MySQL and PHP (W1)PHP Recap Functions: Return Values Use return() Causes execution of function to cease Control returns to calling script To return multiple values Return an array If no value returned NULL

2010/11 : [38]Building Web Applications using MySQL and PHP (W1)PHP Recap ‘Scope’ A function executes within its own little protected bubble, or local scope. What does this mean? Its means that the function can’t ‘see’ any of the variables you have defined apart from those passed in as arguments.. Each new function call starts a clean slate in terms of internal function variables.

2010/11 : [39]Building Web Applications using MySQL and PHP (W1)PHP Recap In other words.. Variables within a function Are local to that function Disappear when function execution ends Variables outside a function Are not available within the function Unless set as global Remembering variables Not stored between function calls Unless set as static

2010/11 : [40]Building Web Applications using MySQL and PHP (W1)PHP Recap Exercise Hands On Exercise PHP Recap: Procedural Code

2010/11 : [41]Building Web Applications using MySQL and PHP (W1)PHP Recap Review Recapped variables, arrays. Recapped control structures, logic. Recapped functions. For more, look back at your notes from the P1 course and consult the PHP manual.