Top Banner
Introduction to PHP CSC 318 WEB APPLICATION DEVELOPMENT
39

Introduction to PHP

Feb 23, 2016

Download

Documents

TAM

CSC 318 WEB APPLICATION DEVELOPMENT. Introduction to PHP. Overview. Introduction to Server Scripting language Client VS Server Introduction to PHP PHP Files and Syntax Function of PHP Why PHP? Implementing PHP How PHPWorks ? Requirements for PHP How to write PHP codes. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to PHP

Introduction to PHPCSC 318WEB APPLICATION DEVELOPMENT

Page 2: Introduction to PHP

Overview

Introduction to Server Scripting language Client VS Server Introduction to PHP

PHP Files and Syntax Function of PHP Why PHP? Implementing PHP How PHPWorks? Requirements for PHP

How to write PHP codes

Page 3: Introduction to PHP

Learning Outcome

Students will design and develop webpage using Server-Side scripting for efficient user interaction.

Students will establish, configure and maintain an intranet and internet website.

Students will understand about PHP, requirement and function of PHP.

Page 4: Introduction to PHP

Introduction Client Server Communication

What is server? A (software program | computer) that provides a specific kind of service

to client software running on the same computer or other (clients | computers) on a network

Example:▪ Web server, file server, DHCP server

What is client?▪ A (computer | program | process) that makes requests for

information from another (computer | program | process) in a client-server relationship

`

Client-side Server-side

Send request to the server

Return file to the client

Send another request to the server

Return required file to the client

Page 5: Introduction to PHP

Introduction to Scripting

In a client-server application there are two kinds of scripts or program:

client-side scriptclient side script like Client-JavaScript is run on the client computer using web browser.

server-side scriptserver-side script like ASP,PHP, and ColdFusion is run by Web server (such as IIS Apache, etc.)

Page 6: Introduction to PHP

Client Vs Server

•Reduce requests needed to be passed to server•Access browser•Enhance Web pages with DHTML, ActiveX controls, and applets

•Validate user input•Executed on server•Generate custom response for client•Wide range of programmatic capabilities•Access to server-side software that extends server functionality

Server-side scripts

Client-side scripts

Page 7: Introduction to PHP

Client Side Processing

Advantages

Faster to display since workload is distributed to user’s PC

Customizable output – personalized experience

Disadvantages

Cannot retain global data

Cannot store data from user into database

Page 8: Introduction to PHP

Server Side Processing

Advantages

Data from user can be stored into database

Web server control user’s browsing selection

Disadvantages

The processing is centralized at the server. This will put the burden of processing on the server instead of the client

Page 9: Introduction to PHP

Introduction to PHP

PHP To have basic understanding of HTML, CSS and JavaScript PHP stands for “Hypertext Preprocessor“ PHP is a widely-used, open source scripting language PHP scripts are executed on the server PHP is open source software and it is FREE Current version of PHP is Version 6.

Page 10: Introduction to PHP

PHP File & Syntax

PHP files can contain text, HTML, CSS, JavaScript, and PHP code

PHP code are executed on the server, and the result is returned to the browser as plain HTML

PHP files have extension “*.php“ In terms of keywords and language syntax, PHP is

similar to most high level languages that follow the C style syntax. 

if conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, C#, Java and Perl.

Page 11: Introduction to PHP

Function of PHP

PHP can restrict users to access some pages

on your websitePHP can add, delete, modify data in your

database

PHP can create, open, read, write, and close

files on the server

PHP can generate dynamic page content PHP collect form data PHP can encrypt data

PHP can send and receive cookies

With PHP you are not limited to output

HTML. You can output images, PDF files, and even Flash movies. You

can also output any text, such as XHTML

and XML.

Page 12: Introduction to PHP

Why PHP

PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)

PHP is compatible with almost all servers used today (Apache, IIS, etc.)

PHP supports a wide range of databases Download material of PHP from the official PHP

resource: www.php.net

Page 13: Introduction to PHP

Implementing PHP

To implement PHP, you need a SERVER inside your computer – It is FREE!

Server is used to test PHP script in your computer. PHP code is interpreted by a web server with a PHP

processor module, which generates the resulting web page.

PHP commands can be embedded directly into an HTML source document rather than calling an external file to process data

Page 14: Introduction to PHP

How PHP Works

PHP code is interpreted by a web server with a PHP processor module, which generates the resulting web page: PHP commands can be embedded directly into an HTML source document rather than calling an external file to process data.

PHP Scripts

Web browser

PHPModule

Web Server (XAMPP)

Send script

Return output

Page 15: Introduction to PHP

Requirement for PHP

Install a web server – Eg. XAMPP Install PHP – Eg. phpMyAdmin Install a database, such as MySQL

Page 16: Introduction to PHP

Requirement for PHP

Install PHP MyAdmin – for database & coding platform http://

www.phpmyadmin.net/home_page/index.php Look & try at the “Demo” first before download the

software. Install XAMPP – for server

http://www.wikihow.com/Install-XAMPP-for-Windows

Page 17: Introduction to PHP

PHP

client serverURL request

HTML

PHP

Script requestHTML

Page 18: Introduction to PHP

How to write your PHP

PHP code is surrounded with by delimeter <? … ?> <? is open section for PHP ?> is closing section of PHP

Page 19: Introduction to PHP

PHP code skeleton:

PHP Block

<?php …. ?>

<? … ?>

<script language = “PHP”> … </script>

First style

Second style

Third style

Page 20: Introduction to PHP

PHP Programming Style

Page 21: Introduction to PHP

Sending data to web browser

Use PHP built in functions Example

▪ echo ‘Hello Student’;▪ print “ How are you”;

Case-insensitive for function names▪ ECHO, echo, Echo

Other print functions print_r, var_dump - value of variable print_f - formatting what you print

Page 22: Introduction to PHP

White spaces - blank lines, tabs and extra spaces To alter spacing of finished web page, use

▪ <br /> - line break ▪ <p></p> - paragraph

To alter spacing of HTML source from PHP, use▪ echo() or print() over the course of several lines ▪ \n (newline character) within double quotation marks

White space, HTML and PHP

Page 23: Introduction to PHP

Writing comments

Important aspect to dynamic web site development

Viewable in the source but not in the browser window

PHP supports 3 type of comments # this is a comment // this is also a comment /* this is a larger comment that spans two line */

Page 24: Introduction to PHP

Variables

Rules of thumb Variable name must start with dollar sign ($) Combination of strings, numbers and the underscore First character after dollar sign cannot be a number Case sensitive Assigned value using equals sign (=)

Page 25: Introduction to PHP

String

A quoted chunk of letters, numbers, spaces, punctuation .. Example strings

‘hello’ ‘software’ ‘1000’ ’12 January, 2006’

String variable – assign a string value to valid variable name $today =’16 July, 2007’;

To print out echo $today; echo “Today is $today”;

Concatenation string Addition of strings using period (.).

▪ $day=‘12’;▪ $month=‘January’;▪ $year =‘2006’;▪ $today = $day . ’ ‘ . $month . ’ ‘ . $year;

Use it extensively when building database queries in later chapters

Page 26: Introduction to PHP

Numbers

Valid number-type variables can be 8 3.14 1098727272798 -4.2828282

Arithmetic operators + addition - subtraction * multiplication / division % modular ++ increment -- decrement

Page 27: Introduction to PHP

Numbers

Functions round()

▪ $j = 3.14;▪ $k = round( $j);

number_format()▪ $p =20980;▪ $g=number_format($p);▪ $g=number_format($p,2);

Page 28: Introduction to PHP

Constant

Specific data type Retain initial value throughout script Cannot change once it has been set

Use define()▪ define (‘AGE’, ‘value’);

Print constant▪ echo ‘Hello, ‘ . AGE; OR▪ echo ‘Hello,’, AGE;

Page 29: Introduction to PHP

Single quote -> values treated literally Double quote -> interpolated Example:

$var =‘Hello’;

echo “var equal to $var”;▪ var equal to hello

echo ‘var equal to $var’;▪ var equal to $var

echo “\$var is equal to $var”;▪ $var is equal to hello

Single vs Double Quotation Marks

Page 30: Introduction to PHP

“” replace variables name with its value and a special

character’s code (\$) with its represented value‘’

display exactly what you type, except for the escaped single quote (\’) and the escape backslash(\\).

Single vs Double quotation marks

Page 31: Introduction to PHP

PROGRAMMING WITH PHP

PROGRAMMING WITH PHP

Page 32: Introduction to PHP

Discuss on..

Setup Creating an HTML form Handling an HTML form

Page 33: Introduction to PHP

Setup

ClientWeb-browser

ServerApache Server

URL request

HTML

PHP

Script requestHTMLTo open the electronic

pagelocalhost/foldername/pagename.extension

Server and Databasexampp [ Apache:web server , MySQL: database]

Programming LanguagesHTML:design , JavaScript:Validation

PHP:Server Side Scripting , SQL: Data manipulation

Folder and Electronic Page

Save the electronic page in a folderSave the folder : xampp/htdocs

Page 34: Introduction to PHP

Managing HTML form with PHP involves 2 steps :

1.Step 1:Create HTML form with any text editor• HTML form (.htm/.html) is created using the HTML

form tags and various input types.

2. Step 2:Create PHP scripts that receives form data• PHP script (.php) is created to receives the submitted

form data and handle it.

HTML form

Page 35: Introduction to PHP

Handling HTML form Step 1: create html form

Step 2: create php scripts that receive form data

Page 36: Introduction to PHP

Step 1: create html form form.html

how data is sent(get or post)

which page the form data will be send

Page 37: Introduction to PHP

Step 2: create php script procesform.php

Page 38: Introduction to PHP

If you have a text box in html form with name attribute age, PHP will store the text entered there in a variable called $age (registered global variable)

$age is similar to $_POST[‘age’] (superglobal variables)

eg : ( in HTML form)

eg : ( in PHP scripts) echo “<p>Thank you, {$_POST[‘age’]} for the following comments”;

Superglobal variable

<p><b>Age: </b><input type ="text" name=“age" size="20" maxlength="40"/></p>

attributes