PHP Hypertext Preprocessor

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

Lecture 12. PHP. cp476 PHP

PHP. Interactive Web Systems

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

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

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

CERTIFICATE IN WEB PROGRAMMING

CS6501 IP Unit IV Page 1

PHP INTERVIEW QUESTION-ANSWERS

PHP by Pearson Education, Inc. All Rights Reserved.

Web Scripting using PHP

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

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

CSCB20 Week 8. Introduction to Database and Web Application Programming. Anna Bretscher* Winter 2017

PHP: Hypertext Preprocessor. A tutorial Introduction

Web Scripting using PHP

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

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

PHP 1. Introduction Temasek Polytechnic

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

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

WINTER. Web Development. Template. PHP Variables and Constants. Lecture

PHP 5 Introduction. What You Should Already Know. What is PHP? What is a PHP File? What Can PHP Do? Why PHP?

Part I. Web Technologies for Interactive Multimedia

Web Engineering (Lecture 08) WAMP

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

Important Points about PHP:

Programming Language Basics

Programming for the Web with PHP

B. V. Patel Institute of BMC & IT 2014

Lecture 7 PHP Basics. Web Engineering CC 552

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

Software and Programming 1

VLC : Language Reference Manual

CSCB20 Week 8. Introduction to Database and Web Application Programming. Anna Bretscher* Winter 2016

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

DC71 INTERNET APPLICATIONS JUNE 2013

COMP284 Scripting Languages Lecture 9: PHP (Part 1) Handouts

Key Differences Between Python and Java

PERL Scripting - Course Contents

Full file at

Server side basics CS380

Introduction to Programming Using Java (98-388)

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

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

DATABASE SYSTEMS. Introduction to web programming. Database Systems Course, 2016

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

// Introducing PHP. Overview of Dynamic Sites with PHP and Top 5 Best Practices. WebTechNY September 10 th, 2008

Array. Prepared By - Rifat Shahriyar

egrapher Language Reference Manual

Pathologically Eclectic Rubbish Lister

Exam Actual. Higher Quality. Better Service! QUESTION & ANSWER

COMP284 Scripting Languages Lecture 14: JavaScript (Part 1) Handouts

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

And Parallelism. Parallelism in Prolog. OR Parallelism

Software and Programming 1

CSC Web Programming. Introduction to JavaScript

String Computation Program

CPSC 481: CREATIVE INQUIRY TO WSBF

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

PHPoC vs PHP > Overview. Overview

PHPoC Language Reference > Overview. Overview

Setting Up a Development Server What Is a WAMP, MAMP, or LAMP? Installing a WAMP on Windows Testing the InstallationAlternative WAMPs Installing a

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

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Database Systems Fundamentals

cis20.2-spring2010-sklar-lecii.4 1

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

easel LANGUAGE REFERENCE MANUAL

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Science II (20082) Week 1: Review and Inheritance

Scripting Languages Perl Basics. Course: Hebrew University

Exam Questions

Java+- Language Reference Manual

Object-Oriented Programming

Fall 2017 CISC124 9/16/2017

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

CHIL CSS HTML Integrated Language

PHP & My SQL Duration-4-6 Months

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

String Computation Program

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

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Client-Side Web Technologies. JavaScript Part I

Algorithms and Programming I. Lecture#12 Spring 2015

CGS 3066: Spring 2015 JavaScript Reference

TML Language Reference Manual

Database Systems Fundamentals

Alpha College of Engineering and Technology. Question Bank

C-LANGUAGE CURRICULAM

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

A Web-Based Introduction

CSCI 1061U Programming Workshop 2. C++ Basics

FRAC: Language Reference Manual

Lecture 2, September 4

PHP Dynamic Web Pages, Without Knowing Beans

Best PHP Training in PUNE & Best PHP Training Institute in MAHARASHTRA

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

Transcription:

PHP Hypertext Preprocessor A brief survey Stefano Fontanelli stefano.fontanelli@sssup.it January 16, 2009 Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 1 / 30

PHP: what is it? 1 Acronym for PHP Hypertext Preprocessor 2 it is a scripting language, 3 it is originally designed for producing dynamic web pages, 4 it has evolved to include a command line interface capability, 5 it also can be used in standalone graphical applications. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 2 / 30

PHP availability 1 It is Open Source and free to use, 2 many OS support it, 3 several modules and extensions are available for example the extension to connect with MySQL dbms. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 3 / 30

PHP for web development 1 It runs on a web server, 2 can be embedded into HTML, 3 usually web framework are used to help development. An example: hello.php <html> <head> <title>php Hello world!</title> </head> <body> <p> echo "Hello World!"; </p> </body> </html> Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 4 / 30

PHP syntax: Delimiters 1 PHP only parses code within its delimiters, 2 the purpose of these delimiters is to separate PHP code from non-php code, 3 everything outside the delimiters is ignored by the parser and is passed through as output, 4 the most common delimiters are and. The Hello World code example is: echo "Hello World!"; The output is: Hello World! Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 5 / 30

PHP syntax: Comments PHP supports C, C++ and Unix shell-style (Perl style) comments. For example: echo This is a test ; // A one-line c++ style comment /* * This is a multi line comment */ echo One Final Test ; # A one-line shell-style comment Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 6 / 30

PHP syntax: Constants 1 a constant is an identifier (name) for a constant value, 2 a valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores, 3 a constant is case-sensitive by default, 4 by convention, constant identifiers are always uppercase, 5 you can access constants anywhere in your script without regard to scope. An example: define( MYCOSTANT, "Hello world!"); echo MYCONSTANT; The output is: Hello World! Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 7 / 30

PHP syntax: Data types and variables PHP supports eight primitive types: 1 four scalar types (boolean, integer, float, string), 2 two compound types (array, object), 3 two special types (resource, NULL). Type does not need to be specified in advance. 1 Variables are represented by a $ followed by its name, 2 a valid variable name follows the same rules of constants, 3 the variable name is case-sensitive. An example: $mystring = "Hello world!"; echo $mystring; The output is: Hello World! Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 8 / 30

PHP syntax: Data types and variables (2) Examples: $myboolean = True; $myboolean = False; $myinteger = 123; $myinteger = -123; $myinteger = 0123; $myinteger = 0x1A; $myfloat = 1.23; $myfloat = 1.0e3; $myfloat = 1E-1; $mystring = This string will not be escaped ; $mystring = "This string will be escaped"; $mystring = "I ll be back!"; Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 9 / 30

PHP syntax: Data types and variables (3) Examples: $myarray = array("mykey" => "myvalue", 1 => 300); echo $myarray["mykey"]; // Output: myvalue echo $myarray[1]; // Output: 300 $myarray[] = 200; echo $myarray[2]; // Output: 200 $myarray["newkey"] = "newvalue"; echo $myarray["newkey"]; // Output: newvalue unset($myarray["newkey"]); // remove the element unset($myarray); // delete the whole array $myarray[] = array(); // create an empty array Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 10 / 30

PHP syntax: Classes and Objects A simple example: class MyClass { private $myvar = 0; public myfunction() { echo "\$myvar value is $myvar"; $myobj = new myclass(); $myobj->myfunction(); // Output: $myvar value is 0 Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 11 / 30

PHP syntax: Arithmetic and Assignment Operators Examples: $a = 0; $a += 5; // $a = 5 $echo -$a; // print: -5 $b = $a + 5; // $b = 10 $c = $b - $a; // $c = 5 $d = $a * $c; // $d = 25 $e = $b / $a; // $e = 2 $f = $b % $a; // $e = 0 $b = ++$a; // $b = 6, $a = 6 $c = $b++; // $c = 6, $b = 7 $b = --$a; // $b = 5, $a = 5 $c = $b--; // $c = 5, $b = 4 $s = "Hello ". "World"; $s.= "!"; echo $s; //Output: Hello World! Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 12 / 30

PHP syntax: Comparison Operators (1) Examples 1 $a == $b return TRUE if $a is equal to $b; 2 $a === $b return TRUE if $a is equal to $b and they are of the same type; 3 $a!= $b return TRUE if $a is not equal to $b 4 $a!== $b return TRUE if $a is not equal to $b or they are not of the same type; 5 $a < $b return TRUE if $a is strictly less than $b; 6 $a > $b return TRUE if $a is strictly greater than $b; 7 $a <= $b return TRUE if $a is less than or equal to $b; 8 $a <= $b return TRUE if $a is greater than or equal to $b; Warning! 1 it is possible the comparison between two different data types, 2 the parser will perform an implicit cast (see the manual). Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 13 / 30

PHP syntax: Comparison Operators (2) An examples of implicit casts during the comparisons: $a = "This is a string"; if (0 == $a) { echo "\$a is equal to 0."; else { echo "\$a is not equal to 0."; $b = "The 4th edition of IMIT." if (4 == $b) { echo "\$b is equal to 4."; else { echo "\$b is not equal to 4."; The output is: $a is equal to 0. $b is equal to 4. The Comparison with Various Types table: http://www.php.net/manual/en/language.operators.comparison.php Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 14 / 30

PHP syntax: Logical Operators Examples 1!$a return TRUE if $a is not TRUE; 2 $a && $b return TRUE if both $a and $b are TRUE; 3 $a and $b return TRUE if both $a and $b are TRUE; 4 $a $b return TRUE if either $a or $b are TRUE; 5 $a or $b return TRUE if either $a or $b are TRUE; 6 $a xor $b return TRUE if either $a or $b are TRUE, but not both. The precedence of the operators is not the same! See Operator Precedence of the manual. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 15 / 30

PHP syntax: Array Operators Examples 1 $a + $b union of $a and $b; 2 $a == $b return TRUE if $a and $b have the same key/value pairs; 3 $a === $b return TRUE if $a and $b have the same key/value pairs in the same order and of the same types; 4 $a!= $b return TRUE if $a is not equal to $b; 5 $a!== $b return TRUE if $a is not identical to $b; Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 16 / 30

PHP syntax: if/elseif/else if ($a > $b) { echo "\$a is bigger than \$b."; elseif ($a == $b) { echo "\$a is equal to \$b."; else { echo "\$a is smaller than \$b."; An HTML example: if ($a > $b) { <p>a is bigger than b.</p> else { <p>a is not bigger than b.</p> Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 17 / 30

PHP syntax: switch switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; default: echo "i is not equal to 0, 1 or 2"; Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 18 / 30

PHP syntax: while/do-while The differences between while and do-while: $i = 0; while ($i < 10) { // print numbers from 0 to 9 echo $i++; // now $i = 10 do { echo $i++; // print 10 while ($i < 10); // now $i = 11 Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 19 / 30

PHP syntax: for/foreach for ($i = 0; $i < 10; $i++) { // print numbers from 0 to 9 echo $i++; $myarray = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); foreach ($myarray as $value) { if (!($value % 3)) continue; if ($value == 10) break; echo $value; // print 0, 1, 2, 4, 5, 7, 8 Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 20 / 30

PHP syntax: include/require (1) 1 the include() and require() statements include and evaluate the specified file; 2 the two constructs are identical in every way except how they handle failure; 3 use require() if you want a missing file to halt processing of the page. File:./includes/functions.php function helloworld() { echo "Hello World!"; File:./hello.php include("./functions.php"); // print a warning helloworld(); // print nothing include("./includes/functions.php"); helloworld(); // print: Hello World! Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 21 / 30

PHP syntax: include/require (2) File:./includes/functions.php function helloworld() { echo "Hello World!"; File:./hello.php require("./functions.php"); // FATAL ERROR echo "String will never be printed."; PHP has also include once() and require once() statements: 1 they are similar to include() and require() statements; 2 but if the code from a file has already been included, it will not be included again. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 22 / 30

PHP syntax: User-defined functions (1) A function may be defined using syntax such as the following: function myfunction($arg1, $arg2,..., $argn) { echo "An example of user-defined function."; return $val; A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. It is possible to call recursive functions in PHP. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 23 / 30

PHP syntax: User-defined functions (2) You can pass arguments by reference, and set default arguments values: function myfunction(&$value) { $value *= 100; function myfunction2($arg1, $arg2 = "default") { echo $arg1. " - ". $arg2; $value = 5; myfunction($value); echo $value; // print: 500 myfunction2($value); // print: 500 - default Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 24 / 30

PHP syntax: User-defined functions (3) Values (any type) are returned by using the optional return statement: function myfunction($value) { return $value *= 100; function &myfunction2() { $ref = new MyObject(); return $ref; $myref &= myfunction2(); Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 25 / 30

PHP reference: predefined variables PHP provides a large number of predefined variables to all scripts: 1 $ SERVER an associative array with server and execution environment information; 2 $ GET an associative array with HTTP GET variables; 3 $ POST an associative array with HTTP POST variables; 4 $ FILES an associative array with HTTP File Upload variables; 5 $ SESSION an associative array with session variables; 6 $ COOKIE an associative array with HTTP cookies; 7 $argc the number of arguments passed to script; 8 $argv array of arguments passed to script. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 26 / 30

PHP reference: handling file uploads (1) PHP can receive file uploads from any RFC-1867 compliant browser: <!-- The data encoding type, enctype, MUST be specified as below --> <form enctype="multipart/form-data" action="myscripts.php" method="post"> <!-- MAX FILE SIZE must precede the file input field --> <input type="hidden" name="max FILE SIZE" value="30000" /> <!-- Name of input element determines name in $ FILES array --> Send file 1: <input name="userfile[]" type="file" /> Send file 2: <input name="userfile[]" type="file" /> <input type="submit" value="send File" /> </form> Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 27 / 30

PHP reference: handling file uploads (2) foreach ($ FILES["userfile"]["error"] as $key => $error) { if ($error == UPLOAD ERR OK) { $tmp name = $ FILES["userfile"]["tmp name"][$key]; $name = $ FILES["userfile"]["name"][$key]; if (move uploaded file($tmp name, "data/$name")){ echo "File is valid, and was successfully uploaded."; else { echo "File was not successfully uploaded."; Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 28 / 30

Exercise 1 install WAMP server on your PC (http://www.wampserver.com); 2 develop a simple web application. The application is composed of two scripts: 1 the first prints HTML form with 3 input text field (name, surname, email), 1 input file field and a submit button which send the request to second script; 2 the second script receives variables then it prints the text variables and saves on disk the file. Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 29 / 30

References PHP Manual (English HTML version): http://www.php.net Stefano Fontanelli stefano.fontanelli@sssup.it PHP Hypertext Preprocessor January 16, 2009 30 / 30