PHP for Server-Side Programming

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Java Script Session1 INTRODUCTION.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
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 CS URLs and web servers  Usually when you type a URL in your browser:  Your computer looks up the server's IP address using.
Server side basics.
Server side basics CS URLs and web servers  Usually when you type a URL in your browser:  Your computer looks up the server's IP address using.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
CMPT241 Web Programming Intro to PHP.
8/17/2015CS346 PHP1 Module 1 Introduction to PHP.
PHP: Introduction By Trevor Adams.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
INTERNET APPLICATION DEVELOPMENT For More visit:
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
10/5/2015CS346 PHP1 Module 1 Introduction to PHP.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
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.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Introduction to PHP Advanced Database System Lab no.1.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
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.
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.
Introduction to PHP.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
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.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
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.
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,
CSE 154 LECTURE 14: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
PHP. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
CSE 154 LECTURE 16: FILE I/O; FUNCTIONS. Query strings and parameters URL?name=value&name=value...
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
Lecture 16: File I/O; Functions
Chapter 5 Scripting Language
CS 371 Web Application Programming
Web Database Programming Using PHP
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
PHP (PHP: Hypertext Preprocessor)
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Chapter 5 Scripting Language
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
Module 1 Introduction to PHP 11/30/2018 CS346 PHP.
PHP.
Web DB Programming: PHP
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Lecture 5: Functions and Parameters
Intro to PHP.
Tutorial 6 PHP & MySQL Li Xu
JavaScript CS 4640 Programming Languages for Web Applications
CMPT241 Web Programming Intro to PHP.
Lecture 14: JSON and Web SERVICES
PHP: Hypertext Preprocessor
PHP an introduction.
23 PHP.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Web Application Development Using PHP
Presentation transcript:

PHP for Server-Side Programming By Jinzhu Gao

Objectives Create pages that takes advantage of data and resources on a web server and present it to the user in dynamic ways www.webstepbook.com

Motivation HTML cannot express similarity in a compact way Large amounts of redundant content in a page Redundancy between pages Needs to create dynamic web pages www.webstepbook.com

Web Pages Static content/fixed content Dynamic content A client’s browser requests a simple .html file The web server software (Apache/IIS) reads the file from the disk and sends its contents back to the client Dynamic content Web pages generated on-the-fly by a web server at the moment a user requests them. www.webstepbook.com

URLs and Web Servers When you type a URL in your browser: http://training.pacificescience.com Your computer looks up the server's IP address using DNS Your browser connects to that IP address and requests the given file The web server software (e.g. Apache) grabs that file from the server's local file system, and sends back its contents to you www.webstepbook.com

URLs and Web Servers Some URLs actually specify programs that the web server should run, and then send their output back to you as the result: http://www.pacificescience.com/jgao/contact.php the above URL tells the server www.pacificescience.com to run the program contact.php and send back its output www.webstepbook.com

Server-Side Web Programming Server-side scripting Writing programs to generate dynamic content Server-side pages are programs written using one of web programming languages/frameworks such as PHP, Java/JSP, Ruby on Rails, ASP.NET, Python, Perl The web server contains software to run those programs and send the output back to the client www.webstepbook.com

PHP “PHP Hypertext Preprocessor” A server-side scripting language Used to make web pages dynamic: provide different content depending on context interface with other services: database, e-mail, etc authenticate users process form information PHP code can be embedded in HTML code www.webstepbook.com

Lifecycle of a PHP Web Request www.webstepbook.com

Lifecycle of a PHP Web Request Browser requests a .html file (static content): server just sends that file Browser requests a .php file (dynamic content): server reads it, runs any script code inside it, then sends result across the network script produces output that becomes the response sent back www.webstepbook.com

Advantages of PHP free and open source compatible: simple: supported by most popular web servers simple: lots of built-in functionality; familiar syntax widely available well-documented: type php.net/functionName in browser Address bar to get docs for any function www.webstepbook.com

PHP vs. other language PHP source code is translated and executed dynamically/interpreted by the web server PHP has more relaxed syntax than Java and C The key construct in PHP is the function rather than the class Most PHP code is contained within a web page and integrated with that page’s HTML content www.webstepbook.com

First PHP Program: hello.php www.webstepbook.com

PHP Output You can't view your .php page on your local hard drive; you'll either see nothing or see the PHP source code If you upload the file to a PHP-enabled web server, requesting the .php file will run the program and send you back its output www.webstepbook.com

PHP Syntax <?php Statements; ?> HTML content <?php www.webstepbook.com

The print Statement print “text”; www.webstepbook.com The output of PHP code is directly inserted into the HTML text and the browser largely ignores whitespace some PHP programmers use the equivalent echo instead of print www.webstepbook.com

Types int, float, boolean, string, array, object, NULL is_string (…) : tests what type a variable is gettype(…) : returns a variable’s type as a string PHP converts between types automatically in many cases: string → int auto-conversion on + ("1" + 1 == 2) int → float auto-conversion on / (3 / 2 == 1.5) type-cast with (type): $age = (int) "21"; www.webstepbook.com

Boolean/bool type The following values are considered to be FALSE (all others are TRUE): 0 and 0.0 "", "0", and NULL (includes unset variables) arrays with 0 elements Can cast to boolean using (bool) FALSE prints as an empty string (no output); TRUE prints as a 1 www.webstepbook.com

NULL a variable is NULL if it has not been set to any value (undefined variables) it has been assigned the constant NULL it has been deleted using the unset function can test if a variable is NULL using the isset function NULL prints as an empty string (no output) www.webstepbook.com

Arithmetic Arithmetic operators: +, -, *, /, % www.webstepbook.com

Variables $name = expression; www.webstepbook.com

Arrays to append, use bracket notation without specifying an index element type is not specified; can mix types www.webstepbook.com

Array Functions www.webstepbook.com

Array Functions www.webstepbook.com

Comments www.webstepbook.com

Strings zero-based indexing using bracket notation string concatenation operator is . (period), not + 5 + "2 turtle doves" === 7 5 . "2 turtle doves" === "52 turtle doves" can be specified with "" or '' www.webstepbook.com

Interpreted Strings strings inside " " are interpreted variables that appear inside them will have their values inserted into the string strings inside ' ' are not interpreted: www.webstepbook.com

string functions www.webstepbook.com

string functions www.webstepbook.com

string functions www.webstepbook.com

htmlspecialchars function www.webstepbook.com

Control Statements for loop www.webstepbook.com

Control Statements foreach loop www.webstepbook.com

Control Statements if/else loop www.webstepbook.com

Control Statements while loop break and continue keywords also behave as in Java/C www.webstepbook.com

Self-Check Page 169 www.webstepbook.com

Advanced PHP Syntax PHP file I/O functions www.webstepbook.com

Advanced PHP Syntax Reading/writing files file returns lines of a file as an array (\n at end of each) file_get_contents returns entire contents of a file as a single string file_put_contents writes a string into a file Strrev: reverses the order of the characters in the given string www.webstepbook.com

Advanced PHP Syntax Reading/writing files file returns lines of a file as an array (\n at end of each) file_get_contents returns entire contents of a file as a single string file_put_contents writes a string into a file Strrev: reverses the order of the characters in the given string www.webstepbook.com

Advanced PHP Syntax Reading/writing files file returns lines of a file as an array (\n at end of each) file_get_contents returns entire contents of a file as a single string file_put_contents writes a string into a file Strrev: reverses the order of the characters in the given string www.webstepbook.com

Advanced PHP Syntax file function: Returns the lines of a file as an array of strings each ends with \n ; to strip it, use an optional second parameter: $lines = file("todolist.txt", FILE_IGNORE_NEW_LINES); common idiom: foreach or for loop over lines of file www.webstepbook.com

Advanced PHP Syntax list function www.webstepbook.com

Advanced PHP Syntax Reading directories www.webstepbook.com glob can match a "wildcard" path with the * character glob("foo/bar/*.doc") returns all .doc files in the foo/bar subdirectory glob("food*") returns all files whose names begin with "food" the basename function strips any leading directory from a file path basename("foo/bar/baz.txt") returns "baz.txt" www.webstepbook.com

Advanced PHP Syntax Reading directories www.webstepbook.com glob can match a "wildcard" path with the * character glob("foo/bar/*.doc") returns all .doc files in the foo/bar subdirectory glob("food*") returns all files whose names begin with "food" the basename function strips any leading directory from a file path basename("foo/bar/baz.txt") returns "baz.txt" www.webstepbook.com

Advanced PHP Syntax Functions parameter types and return types are not written a function with no return statements is implicitly "void" can be declared in any PHP block, at start/end/middle of code www.webstepbook.com

Advanced PHP Syntax Functions: variables declared in a function are local to that function; others are global www.webstepbook.com

Advanced PHP Syntax Functions: if no value is passed, the default will be used (defaults must come last) www.webstepbook.com

Advanced PHP Syntax Constructing and using objects Test whether a class is installed with class_exists The following code unzips a file www.webstepbook.com

Advanced PHP Syntax Classes: www.webstepbook.com

Advanced PHP Syntax Classes: www.webstepbook.com

Advanced PHP Syntax Classes: www.webstepbook.com

Advanced PHP Syntax Classes: Basic inheritance www.webstepbook.com

Advanced PHP Syntax Static methods, fields, and constants www.webstepbook.com

Advanced PHP Syntax Abstract classes and interfaces interfaces are supertypes that specify method headers without implementations abstract classes are like interfaces, but you can specify fields, constructors, methods www.webstepbook.com

Advanced PHP Syntax PHP's HttpRequest object can fetch a document from the web www.webstepbook.com

Embedded PHP Insert PHP code into HTML seamlessly to produce a dynamic results Design goal Maximize the amount of the code that is spent in plain HTML “mode” Minimize the amount spent in PHP “mode” Printing HTML tags with print statements is bad style and error-prone: www.webstepbook.com

PHP Expression Blocks evaluates and embeds an expression's value into HTML <?= expr ?> is equivalent to <?php print expr; ?> <? expr ?> is equivalent to <?php expr ?> www.webstepbook.com

PHP Expression Blocks evaluates and embeds an expression's value into HTML <?= expr ?> is equivalent to <?php print expr; ?> <? expr ?> is equivalent to <?php expr ?> www.webstepbook.com

PHP Expression Blocks evaluates and embeds an expression's value into HTML <?= expr ?> is equivalent to <?php print expr; ?> <? expr ?> is equivalent to <?php expr ?> www.webstepbook.com

PHP Expression Blocks: Common Erros Missing = page 161 www.webstepbook.com

Self-Check Page 193 Reading Assignment: Ch 5.5 Case Study: Word of the Day http://www.pacificescience.com/jgao/COMP127/code/ch05- php/index.php www.webstepbook.com

Summary PHP Syntax Embedded PHP Print, Types, Arithmetic, Variables, Strings, Comments, Boolean, Control statements Functions, Including files, Arrays, foreach, File I/O Embedded PHP Expression blocks www.webstepbook.com