CHAPTER 9 INTRODUCTION TO PHP

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Chapter 12 Introduction to PHP. © 2006 Pearson Addison-Wesley. All rights reserved Origins and Uses of PHP - Origins - Rasmus Lerdorf
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
WHAT IS PHP PHP is an HTML-embedded scripting language primarily used for dynamic Web applications.
PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
PHP: Hypertext Processor Fred Durao
PHP1-1 PHP Xingquan (Hill) Zhu
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.
Chapter 9 © 2010 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
Mandakini Ayushi Infotech Mandakini Kumari 22 nd July PHP Basic.
Lecture 12 PHP Basics Boriana Koleva Room: C54
Languages in WEB Presented by: Jenisha Kshatriya BCM SS09.
IDK0040 Võrgurakendused I harjutus 06: PHP: Introduction Deniss Kumlander.
INTERNET APPLICATION DEVELOPMENT For More visit:
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Nael Alian Introduction to PHP
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
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.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
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 12 © 2003 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
Introduction to PHP Advanced Database System Lab no.1.
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.
Introduction to Web Programming. Introduction to PHP What is PHP? What is a PHP File? What is MySQL? Why PHP? Where to Start?
Introduction to PHP.
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.
Chapter 9 © 2012 by Addison Wesley Longman, Inc Origins and Uses of PHP - Origins - Rasmus Lerdorf Developed to allow him to track visitors.
PHP Introduction PHP is a server-side scripting language.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
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,
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 1 Introduction to PHP Hypertext Preprocessor - PHP.
PHP-Basic By- Tanvi Raval. Pre-requisites Before you continue you should have a basic understanding of the following: 1. HTML 2. JavaScript.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 13 Scott Marino.
By bscshelp.com 1.  It is a group assignment.  Complete Website design Using Html and Css.  Due date: 10 th December, 2014 (Hard Deadline) 2.
PHP stands for …….. “PHP Hypertext Pre-processor” and is a server-side scripting language like ASP. PHP scripts are executed on the server PHP supports.
CHAPTER 9 INTRODUCTION TO PHP 1. ORIGINS AND USES OF PHP Origins –Rasmus Lerdorf – 1994 –Developed to allow him to track visitors to his Web site PHP.
1 Server Side scripting PHP. 2 What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are.
Introduction to PHP. PHP Origins Rasmus LerdorfRasmus Lerdorf (born Greenland, ed Canada) PHP originally abbreviation for ‘Personal Home Pages’, now ‘PHP.
A pache M ySQL P hp Robert Mudge Reference:
PHP Basics and Syntax Lesson 3 ITBS2203 E-Commerce for IT.
PHP using MySQL Database for Web Development (part II)
12.1 Origins and Uses of PHP 12.2 Overview of PHP - Origins
Session 2 Basics of PHP.
Lecture 2: Intro to PHP What is PHP?
CS 371 Web Application Programming
CHAPTER 5 SERVER SIDE SCRIPTING
9.1 Origins and Uses of PHP 9.2 Overview of PHP - Origins
เอกสารประกอบการบรรยายรายวิชา Web Technology
Introduction to Web programming
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
JavaScript Syntax and Semantics
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
IntroductionToPHP Static vs. Dynamic websites
Web Programming Language
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
Chapter 12 Introduction to PHP.
9.1 Origins and Uses of PHP 9.2 Overview of PHP - Origins
Introduction to PHP.
PHP: Hypertext Preprocessor
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

CHAPTER 9 INTRODUCTION TO PHP 1 CHAPTER 9 INTRODUCTION TO PHP

ORIGINS AND USES OF PHP Origins PHP is an open-source product 2 ORIGINS AND USES OF PHP Origins Rasmus Lerdorf – 1994 Developed to allow him to track visitors to his Web site PHP is an open-source product PHP is an acronym for Personal Home Page, or PHP: Hypertext Preprocessor PHP is used for form handling, file processing, and database access

OVERVIEW OF PHP 3 PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software (OSS) PHP is free to download and use What is a PHP File? PHP files may contain text, HTML tags and scripts PHP files are returned to the browser as plain HTML PHP files have a file extension of ".php", ".php3", or ".phtml" PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: www.php.net

What Can PHP Do? Query a database Allow users to upload files Create/read files on the server (for example, the files that your users upload) Have a "member's area" (i.e. via a login page) Have a shopping cart

How PHP works ? PHP is a server-side language that means the code written in PHP resides on a host computer called a server. The server sends Web pages to the requesting visitors (you, the client, with your web browser) PHP Client-Server Client Server URL HTML 1 2 3 Script request Visitor goes to a web site written in PHP HTML 1 PHP 2 Server reads the PHP and then processes it according to its scripted directions. 3 PHP code tells the server to send the appropriate data – HTML code. Treats it as a standard HTML.

What you will need? A web server application (Apache, Xitami, or IIS) PHP My SQL Web Browser Text editor, PHP-capable WYSIWYG application (Adobe Dreamweaver/Kompozer/Amaya) or IDE (integrated development environment) FTP application if using remote server

PHP Installation You need to install web server before you install PHP. There are many different web servers available to choose from, so you need to choose which one you prefer. Two of the more popular web servers are: Apache Internet Information Services (IIS)

PHP Syntax The PHP syntax is based on C, Java, and Perl, Creating a PHP file is similar to creating an HTML file. In fact, most PHP files are a mixture of PHP code and HTML. To create a PHP file, simply do the following: 1. Create a new file in your favorite editor 2. Type some PHP code 3. Save the file with a .php extension

Basic Code Syntax 3 different forms declaring php <html> <head> <title>PHP Syntax Example</title> </head> <body> <?php echo "PHP is easy!"; ?> </body> </html> <? PHP Code In Here ?> <?php php?> <script language="php"> </script>

Comments in PHP PHP support three kinds of comment tags : 1. // This is a one line comment 2. # This is a Unix shell-style comment. It's also a one line comment 3. /* ..... */ Use this multi line comment if you need to.

Comments in PHP <html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>

Variables Variables are used for storing values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a $ sign symbol. The correct way of declaring a variable in PHP: $var_name = value;

String Variables in PHP String variables are used for values that contains characters. <?php $txt="Hello World"; echo $txt; ?>

PHP Variables In PHP, variable names must start with a dollar sign ($). For example: <?php $myVariable = "PHP is easy!"; echo $myVariable; ?>

Variables name Variable names can only contain alpha-numeric characters and underscores ( i.e. a-z, A-Z, 0-9, or _ ) Variable names must not contain spaces. For multi-word variable names, either separate the words with an underscore ( _ ) or use capitalization.

PHP Variables <?php $variable1 = 2; $variable2 = 9; $variable3 = $variable1 + $variable2; echo $variable3; ?>

PHP Variables-Concatenation <?php $variable1 = 2; $variable2 = 9; $variable3 = $variable1 + $variable2; echo $variable1 . " + " . $variable2 . " = " . $variable3; ?>

PRIMITIVES, OPERATIONS, EXPRESSIONS 18 PRIMITIVES, OPERATIONS, EXPRESSIONS Variables There are no type declarations An unassigned (unbound) variable has the value, NULL The unset function sets a variable to NULL The IsSet function is used to determine whether a variable is NULL - error_reporting(15); - prevents PHP from using unbound variables PHP has many predefined variables, including the environment variables of the host operating system You can get a list of the predefined variables by calling phpinfo() in a script There are eight primitive types: Four scalar types: Boolean, integer, double, and string Two compound types: array and object Two special types: resource and NULL

PRIMITIVES, OPERATIONS, EXPRESSIONS 19 PRIMITIVES, OPERATIONS, EXPRESSIONS Integer & double are typical Strings Characters are single bytes String literals use single or double quotes Single-quoted string literals Embedded variables are NOT interpolated Embedded escape sequences are NOT recognized Double-quoted string literals Embedded variables ARE interpolated If there is a variable name in a double-quoted string but you don’t want it interpolated, it must be back slashed Embedded escape sequences ARE recognized For both single- and double-quoted literal strings, embedded delimiters must be back slashed

PRIMITIVES, OPERATIONS, EXPRESSIONS 20 PRIMITIVES, OPERATIONS, EXPRESSIONS Boolean - values are true and false (case insensitive) 0 and "" and "0" are false; others are true Arithmetic Operators and Expressions Usual operators If the result of integer division is not an integer, a double is returned Any integer operation that results in overflow produces a double The modulus operator coerces its operands to integer, if necessary When a double is rounded to an integer, the rounding is always towards zero Arithmetic functions floor, ceil, round, abs, min, max, rand, etc. String Operations and Functions The only operator is period, for catenation Indexing - $str{3} is the fourth character

PRIMITIVES, OPERATIONS, EXPRESSIONS 21 PRIMITIVES, OPERATIONS, EXPRESSIONS String Operations and Functions (continued) Functions: strlen, strcmp, strpos, substr, as in C chop – remove whitespace from the right end trim – remove whitespace from both ends ltrim – remove whitespace from the left end strtolower, strtoupper Scalar Type Conversions String to numeric If the string contains an e or an E, it is converted to double; otherwise to int If the string does not begin with a sign or a digit, zero is used Explicit conversions – casts e.g., (int)$total or intval($total) or settype($total, "integer")

PRIMITIVES, OPERATIONS, EXPRESSIONS 22 PRIMITIVES, OPERATIONS, EXPRESSIONS The type of a variable can be determined with gettype or is_type gettype($total) - it may return "unknown“ is_integer($total) – a predicate function

OUTPUTS Output from a PHP script is HTML that is sent to the browser 23 OUTPUTS Output from a PHP script is HTML that is sent to the browser HTML is sent to the browser through standard output There are three ways to produce output: echo,print, and printf echo and print take a string, but will coerce other values to strings echo “Test“,”None”; # More than one parameter acceptable echo("first <br />", $sum) # More than one, so ILLEGAL! print "Welcome to my site!"; # Only one printf – same like C PHP code is placed in the body of an HTML document

OUTPUTS An Example: 24 <html> <head><title> Trivial php example </title> </head> <body> <?php print "Welcome to my Web site!"; ?> </body> </html>

CONTROL STATEMENTS Control Expressions Selection statements 25 Relational operators - same as JavaScript, (including === and !==) Boolean operators - same as Perl (two sets, && and and, etc.) Selection statements if, if-else, elseif switch - as in C The switch expression type must be integer,double, or string while - just like C do-while - just like C for - just like C foreach - discussed later break - in any for, foreach, while, do-while, or switch continue - in any loop

CONTROL STATEMENTS 26 HTML can be intermingled with PHP script if ($a == $b) { $a = 3 * $a; ?> <br /> At this point, $a and $b are equal <br /> So, we change $a to three times $a }

ARRAYS Not like the arrays of any other programming language 27 ARRAYS Not like the arrays of any other programming language A PHP array is a generalization of the arrays of other languages A PHP array is really a mapping of keys to values,where the keys can be numbers (to get a traditional array) or strings (to get a hash) Array creation Use the array() construct, which takes one or more key => value pairs as parameters and returns an array of them The keys are non-negative integer literals or string literals The values can be anything $list = array(0 => "apples", 1 => "oranges", 2 => "grapes") This is a “regular” array of strings

28 ARRAYS If a key is omitted and there have been integer keys, the default key will be the largest current key + 1 If a key is omitted and there have been no integer keys, 0 is the default key If a key appears that has already appeared, the new value will overwrite the old one Arrays can have mixed kinds of elements $list = array("make" => "Cessna","model" => "C210","year" => 1960, 3 => "sold"); $list = array(1, 3, 5, 7, 9); $list = array(5, 3 => 7, 5 => 10, "month" => "May"); $colors = array('red', 'blue', 'green', 'yellow');

ARRAYS 29 Accessing array elements – use brackets $list[4] = 7; $list["day"] = "Tuesday"; $list[] = 17; If an element with the specified key does not exist, it is created If the array does not exist, the array is created The keys or values can be extracted from an array $highs = array("Mon" => 74, "Tue" => 70,"Wed" => 67, "Thu" => 62, "Fri" => 65); $days = array_keys($highs); $temps = array_values($highs); Dealing with Arrays An array can be deleted with unset unset($list); unset($list[4]); # No index 4 element now

ARRAYS Dealing with Arrays (continued) 30 ARRAYS Dealing with Arrays (continued) is_array($list) returns true if $list is a function in_array(17, $list) returns true if 17 is an element of $list explode(" ", $str) creates an array with the values of the words from $str, split on a space implode(" ", $list) creates a string of the elements from $list, separated by a space Sequential access to array elements current and next $colors = array("Blue", "red", "green","yellow"); $color = current($colors); print("$color <br />"); while ($color = next($colors)) print ("$color <br />");

31 ARRAYS This does not always work – for example, if the value in the array happens to be FALSE Alternative: each, instead of next while ($element = next($colors)) { print ("$element['value'] <br />"); The prev function moves current backwards array_push($list, $element) and array_pop($list) Used to implement stacks in arrays foreach (array_name as scalar_name) { ... } foreach ($colors as $color) {print "Is $color your favorite?<br /> ";} Is red your favorite color Is blue your favorite color Is green your favorite color? Is yellow your favorite color?

ARRAYS foreach can iterate through both keys and values: 32 foreach (array as key => value) { … } Inside the compound statement, both $key and $color are defined $ages = array("Bob" => 42, "Mary" => 43); foreach ($ages as $name => $age) print("$name is $age years old <br />");

33 PHP EXAMPLES To concatenate two or more variables together, use the dot (.) operator: <html> <body><?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2 ; ?></body> </html> The output of the script above will be: "Hello World 1234"

34 PHP EXAMPLES The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!": <html> <body><?php $d=date("D"); if ($d =="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?></body> </html>

35 PHP EXAMPLES The following example prints the text "Hello World!" five times: <html> <body><?php for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?></body> </html>

36 PHP EXAMPLES The following example demonstrates a loop that will print the values of the given array: <html> <body><?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?></body> </html>