PHP (Hypertext Preprocessor) 1. PHP References From W3 Schools: PHP.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

PHP I.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
The Web Warrior Guide to Web Design Technologies
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
1 Chapter 12 Working With Access 2000 on the Internet.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Server side basics.
Web Programming Introduction to PHP COM Objectives To understand what PHP is and how a PHP script works with a Web Browser and a Web Server To learn.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Introduction to scripting
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
PHP: Hypertext Processor Fred Durao
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
CSC 318 WEB APPLICATION DEVELOPMENT.  Introduction to Server Scripting language  Client VS Server  Introduction to PHP  PHP Files and Syntax  Function.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
PHP TUTORIAL. HISTORY OF PHP  PHP as it's known today is actually the successor to a product named PHP/FI.  Created in 1994 by Rasmus Lerdorf, the very.
Week 9 PHP Cookies and Session Introduction to JavaScript.
10/5/2015CS346 PHP1 Module 1 Introduction to PHP.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
 2008 Pearson Education, Inc. All rights reserved Introduction to XHTML.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Tutorial 10 Programming with JavaScript
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
1 PHP Introduction Chapter 1. Syntax and language constructs.
JavaScript - A Web Script Language Fred Durao
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
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.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
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,
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
CGS 3066: Web Programming and Design Spring 2016 PHP.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Chapter 6 JavaScript: Introduction to Scripting
DBW - PHP DBW2017.
PHP Introduction.
Intro to PHP & Variables
Web Systems Development (CSC-215)
WEB PROGRAMMING JavaScript.
PHP.
Intro to PHP.
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP (Hypertext Preprocessor) 1

PHP References From W3 Schools: PHP A pocket reference: _pkt.html _pkt.html Another tutorial: 2

Introduction PHP is a scripting language, created in 1994 by Rasmus Lerdorf, that is designed for producing dynamic Web content. PHP originally stood for “Personal Home Page,” but as its functionality expanded, it became “PHP: Hypertext Processor” because it takes PHP code as input and produces HTML as output. Although PHP is typically used for Web-based applications, it can also be used for command-line scripting – a task commonly handled by Perl – as well as client-side applications (possibly with a graphical user interface) – a task commonly handled by Python or Java. NOTE: This course only addresses PHP in the context of dynamic Web content creation. PHP is … interpreted rather than compiled like Java or C. an embedded scripting language, meaning that it can exist within HTML code. a server-side technology; everything happens on the server as opposed to the Web browser’s computer, the client. cross-platform, meaning it can be used on Linux, Windows, Macintosh, etc., making PHP very portable. PHP does not… handle client-side tasks such as creating a new browser window, adding mouseovers, determining the screen size of the user’s machine, etc. Such tasks are handled by JavaScript or Ajax (Asynchronous JavaScript and XML). do anything within the Web browser until a request from the client has been made (e.g., submitting a form, clicking a link, etc.). 3

Basic PHP syntax PHP code typically resides in a plaintext file with a.php extension. The code itself is defined within PHP delimiters:. The PHP interpreter considers anything outside of these delimiters as plaintext to be sent to the client. 4

Example <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " transitional.dtd"> Some title here <?php phpinfo(); // phpinfo() is a built-in PHP function ?> 5

Script execution There are two methods for executing PHP scripts: – via the Web server, and the command-line interface (CLI). We will use the first method. Upload your.php file to your Web account Make sure the permissions are set correctly; for example, chmod 644 somefile.php. Navigate to the file with a Web browser. The URL will be The Web server is configured to associate any filename ending in.php with the PHP interpreter. The PHP interpreter opens the file and begins processing it. The output of the PHP interpreter is sent to the client. 6

To display a string <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " Some title here The timeless phrase is: <?php print("Hello, world!"); ?> 7

Printing using echo and print The echo() function is slightly different, in that you can provide comma-separated strings. The print() function requires that you provide one string argument; therefore, to work with multiple strings, you must use the string concatenation operator (.). echo "Hello, world! ", "Goodbye, world!"; print "Hello, world! ". "Goodbye, world!"; In these cases, the interpreter does not require parentheses; however, using the parentheses is also acceptable. Writing HTML to the browser is essentially no different than writing simple text. To print the double-quotation mark, escape it within the string using \". print " Useful Links "; print " Google "; Any text that would exist in the Web page (e.g., HTML elements, cascading style sheets, JavaScript) can be sent to the browser using printing functions. 8

Comments Comments are denoted using the same syntax as C, C++, or Perl: /* */, and //, and # respectively. 9

PHP Variables The syntax for PHP variables is similar to Perl. Variable names must be preceded by a dollar sign ($). Variables do not need to be declared before being used. Variables do not need to be explicitly typed (e.g., int, float, etc.). 10

PHP Types The fundamental types are: Numeric integer. Integers (±231); values outside this range are converted to floating-point. Floating-point numbers. boolean. true or false; PHP internally resolves these to 1 (one) and 0 (zero) respectively. Also as in C, 0 (zero) is false and anything else is true. string. String of characters. array. An array of values, possibly other arrays. Arrays can be indexed or associative (i.e., a hash map). object. Similar to a class in C++ or Java. resource. A handle to something that is not PHP data (e.g., image data, database query result). PHP has a useful function named var_dump() that prints the current type and value for one or more variables. Arrays and objects are printed recursively with their values indented to show structure. 11

PHP Variables-Examples <?php $greeting = 'Hello'; $message1 = "$greeting, world!\n"; // Double quotation marks $message2 = '$greeting, world!\n'; // Single quotation marks print "1: $message1\n"; print "2: $message2\n"; ?> <?php $a = "Hello, world". 35. "!\n"; print $a; ?> <?php $a = 100; $b = ; $sum = $a + (int) $b; print "$a + $b = $sum"; ?> 12

Arrays- indexed and associative Indexed Arrays <?php $a = 35; $b = "Programming is fun!"; $c = array(1, 1, 2, 3, 5, 8); var_dump($a,$b,$c); ?> The var_dump() function in PHP is used to print the current type and values of one or more variables. Arrays and objects are printed recursively with values indented to show structure. Output int(35) string(19) "Programming is fun!" array(6) { [0]=> int(1) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(5) [5]=> int(8) } 13

Arrays- associative $file_ext = array(".c" => "C", ".cpp" => "C++", ".pl" => "Perl", ".java" => "Java"); 14

Arrays $proglangs = array("C", "C++", "Perl", "Java"); echo "I know ", count($proglangs), " language(s)."; // Displays "I know 4 language(s).“ $proglangs = array("C", "C++", "Perl", "Java"); print_r($proglangs); Output: Array ( [0] => C [1] => C++ [2] => Perl [3] => Java ) 15

Printing an array The print_r() function performs a recursive output of the given array. If you want to use the print_r() function for temporary debugging output within the Web browser, surround the print_r() call with the HTML and tags to make the output easier to read. The print_r() function takes an optional second argument, that if set to true, will return the output as a string instead of printing anything to the console or Web browser. For example, $res = print_r($proglangs,true);. The var_dump() function provides more detail with regard to the individual keys and values stored in the array. 16

Associative Arrays Associative arrays work much like their indexed counterparts, except that associative arrays can have non-numeric keys(e.g., strings). 17

Functions To define a function, use the function keyword. For example, function foo() below takes no arguments simply returns the integer 1 (one). function foo() { return 1; } As with Perl, functions in PHP do not have a specified return type; therefore, the return statement is not required. If you were to use return with no argument, the value NULL is returned. As with C you can return the results of conditions, in which case 1 (one) is true and 0 (zero) is false. Here is an example: function is_more_than_ten ($i) { return $i > 10; } 18

Functions Arguments are passed by specifying the names within the parentheses of the function definition. Because PHP uses dynamic typing, no data type is necessary. NOTE: You can pass arguments by reference (i.e., pointers) as in C; function display_name_and_age ($name, $age) { print "It appears that $name is $age year(s) old.\n"; } You can also specify default values for function arguments. function greet ($name = "user") { print "Hello, $name!\n"; } 19

Functions Arguments are passed by specifying the names within the parentheses of the function definition. Because PHP uses dynamic typing, no data type is necessary. NOTE: You can pass arguments by reference (i.e., pointers) as in C; function display_name_and_age ($name, $age) { print "It appears that $name is $age year(s) old.\n"; } You can also specify default values for function arguments. Arguments that have default values should be placed at the end of the argument list. function greet ($name = "user") { print "Hello, $name!\n"; } 20

Functions Because the PHP interpreter does not complain if you specify too many arguments, you can support multiple arguments using the func_num_args() and func_get_arg() functions. Here is an example function that prints out each of its arguments: function display_arguments () { $n = func_num_args(); for ($i=0; $i < $n; $i++) echo "arg ", $i+1, " = ", func_get_arg($i), "\n"; } 21

Returning Arrays from Functions A common task for functions is to initialize various data structures. Consider the following function (and its call): function init_params () { $params["username"] = “sjames"; $params["realname"] = “Susan James"; return $params; } $params = init_params(); The init_params() function creates an associative array with two keys, then returns the newly-created array. Suppose the “user name” and “real name” values had to be retrieved from a database, and one or more of the values could not be retrieved. The only way to ensure that the array was correctly populated is to see if the desired keys are defined (i.e., not NULL). 22

Returning Arrays from Functions function init_params (&$params) { // Perform a database query here, then populate $params... if (query was successful) return true; return false; } if (!init_params($params)) // Display some useful error message here... The syntax becomes slightly more complex because the array is passed by reference (denoted by the & operator in the function argument list). However, the function can now return a success/failure indicator while populating the array (assuming the query was successful). 23

Global Variables Global variables are stored in a predefined associative array named $GLOBALS. To assign a value to a global variable, use the following syntax: $GLOBALS['variablename'] = somevalue; To access a global variable, simply provide the variable name as the key to the $GLOBALS array. For example, $GLOBALS['theuser'] = "dknuth"; // Assign the value $curr_user = $GLOBALS['theuser']; // Retrieve the value Alternatively, you can specify that a given variable is global within the current scope using the GLOBAL keyword. Suppose you have a global variable $bar that you want to be updated by function foo(). function foo () { GLOBAL $bar; $bar++; } In the above example, any use of the variable $bar after the GLOBAL statement is equivalent to $GLOBALS['bar']. 24

Variable Variables Variable variables allow you to access a variable without using that variable directly. Essentially the first variable contains a string whose value is the name of the second variable (without the dollar sign). The second variable is accessed indirectly by prefixing the first variable with an extra dollar sign. This PHP construct is best described with an example: $val1 = 30; $val2 = 60; $foo = "val2"; $bar = $$foo; // $bar's value is 60 after this statement 25

Table 11.1 The reserved words of PHP 26

Table 11.2 Some useful predefined functions 27

Table 11.3 Some commonly used string functions 28

Table 11.4 File use indicators 29

SuperGlobals Superglobals are global variables that a predefined by PHP because they have special uses. The variables themselves are associative arrays. The following table lists the superglobals and their corresponding descriptions. $_GET Variables sent via an HTTP GET request. $_POST Variables sent via an HTTP POST request. $_FILES Data for HTTP POST file uploads. $_COOKIE Values corresponding to cookies. $_REQUEST The combination of $_GET, $_POST, and $_COOKIE. $_SESSION Variables stored in a user’s session (server-side data store). $_SERVER Variables set by the Web server. $_ENV Environment variables for PHP’s host system. $GLOBALS Global variables (including $GLOBALS). 30

Processing XHTML Forms The first step in handling user input via Web forms is to create the user interface. The form is typically defined in an XHTML document unless the form itself has dynamic content. The action attribute of the form element should be the URL of the PHP script that will be processing the form data. 31

Receiving form data Recall that form data is submitted in name- value pairs, which are derived from the form widgets’ name and value attributes. The standard method for accessing this data is by accessing one of the predefined associative arrays named $_POST and $_GET, depending on the form submission method used. The syntax is $_POST['name'], where name corresponds to the name attribute of a given form widget. 32

Form Processing - Example HTML file (Survey.html) with a form Your Name: Do you like this website? Yes No Not sure Your comments: A PHP script - processSurvey.php <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.1 //EN" " D/xhtml11.dtd"> Your name is: Your Do you like this website? Comments: 33