TP2543 WEB PROGRAMMING - ftsm.ukm.my fileClient side vsserver side processing Database SQL...

Post on 15-Aug-2019

242 views 0 download

Transcript of TP2543 WEB PROGRAMMING - ftsm.ukm.my fileClient side vsserver side processing Database SQL...

1

TP2543 WEB PROGRAMMINGTP2543 WEB PROGRAMMING

Introduction to PHP / September 2010

Questions…

�What is PHP?

�Why would you need PHP?

�What are the things that you can do with PHP?

� ... And can’t do with PHP?

GoalGoal

� Provide the basic knowledge of PHP

programming…

� Not to teach everything about PHP

� Explain how you can code and run PHP scripts

� Explain code of examples

� Provide useful references

� …and related concepts

2

So far…

HTML/XHTML

CSS

JavaScript

Structure/Contents

Presentation/Layout

Action/Interactivity

and now…

HTML/XHTML

CSS

JavaScript

DatabasePHP

For PHP Programming

Client-Server Architecture

3

Related concepts

�Web servers

�FTP

�HTTP Requests

�Client side vs server side processing

�Database

�SQL

Client-Server

FTP SERVER-SIDE SCRIPTING

WEB SERVERS

IIS

DATABASE

Apache

MySQL

SQLPHP

Deploying PHP packages

4

Web Servers

� Responds to client (browser) requests to provide resources (HTML documents)

� Communicates using HTTP

� Introducing 2 types of web server:

� Internet Information Services (windows)

�Apache Web Server (open-source for both linuxand windows machine)

http://httpd.apache.org/

Add in Windows

Components

File Transfer Protocol

� Using FTP to upload/download files from server:

� Use built-in Windows function

� Use FTP application/plug-in for browser

Filezilla, CuteFTP,

SmartFTP…

5

HTTP Requests

� GET

�Gets/retrieves information from server (e.g retrieve an image, fetch search results)

� Sends information as part of URL (visible, 2048 chars limit)

� Cached, bookmark-able

� POST

� Posts/sends data (e.g login information, post form data)

� Submitted data is hidden (invisible)

� Non-cached, non-bookmark-able

Client Side vs Server Side

� Client-side

� Validates input, enhance pages

� Source code is visible

� Limited to browser support

� Server-side

� Access server directory, write to database

� Source code is invisible

�Wider-range of programmatic capabilities

Database and SQL

� Database – an integrated collection of data

� DBMS – mechanism to store and organize data according to database format

� Relational databases – popular

�Uses SQL (Structured Query Language) to perform queries, manipulate data

�MS Access, MySQL, MS SQL, Oracle, Sybase, DB2

6

An introduction

PHP Programming

What is What is PHP?PHP?

� PHP = ‘Hypertext PreProcessor’

� Originally created by Rasmus

Lerdorf in 1994

� The main implementation of PHP

is now produced by The PHP Group

(de facto standard for PHP) - http://www.php.net/

� Open-source (access to source code and free distribution right), server-side scripting language

7

B. What is PHP?

� Used to generate (and write to file) dynamic web-pages and web contents

� Text files – HTML, XHTML, XML…

� Images – JPG, PNG…

� PDF files

� Flash movies

� File extension - *.php

How PHP works

PHP Scripts

� PHP scripts reside between reserved PHP tags within HTML pages

<?php print (“$nama_saya”); ?>

� Interpreted language, scripts are parsed at runtime rather than compiled beforehand

� Source-code not visible by client

� Various built-in functions allow for fast development

� Compatible with many popular databases

8

How to run PHP scripts

� A web server with PHP support, you can:

� Set up your own server… (M1/M2/M3)

�…or find a webhosting plan with PHP support (M4)

How to run PHP scripts

� LAMP – Linux + Apache + MySQL + PHP

operating system web server database scripting language

� …as opposed to WISA – Windows + IIS + MS SQL

Server + ASP

� WAMP for Windows platform

PHP on Windows

M1

IIS

FastCGI+ PHP

mySQL

M2

Apache

PHP

mySQL

M3

WAMP Packages

M4

Web Hosting

How to run PHP scripts

9

Manually Setting Up WAMP

� Steps:

�Disable IIS (if active)

�Install Apache Web server + configure

�Install PHP + configure

�Install MySQL + configure

�Install phpMyAdmin

WAMP Packages

http://en.wikipedia.org/wiki/Comparison_of_WAMPs

�AppServ

�WampServer

� XAMPP

� EasyPHP

XAMPP

� http://www.apachefriends.org/en/index.html

� Version for Windows includes: Apache, MySQL, PHP, Perl, phpMyAdmin, JpGraph, FileZilla FTP Server,

SQLite etc.

10

Webhosting C-panel

Coding in PHP

PHPVARIABLESBASIC

SYNTAXDATA TYPE

FUNCTIONCONTROL STATEMENTS

DATABASE

CONNECT/ READ

INSERT UPDATE DELETE

SESSION PHP PACKAGES

11

PHP code

� Structurally similar to C/C++

� All PHP statements end with a semi-colon

� Each PHP script must be enclosed in the reserved PHP tag

<?php……?>

PHP code - comments

� Standard C, C++, and shell comment symbols

// C++ and Java-style comment

# Shell-style comments

/* C-style commentsThese can span multiple lines */

PHP code - output

<?php$nilai = 25; // Numerical variable$ayat = “Hello”; // String variable

echo $ayat; // Outputs Helloecho $nilai, $ayat; // Outputs 25Helloecho “5x5=”,$nilai; // Outputs 5x5=25echo “5x5=$nilai”; // Outputs 5x5=25echo ‘5x5=$nilai’; // Outputs 5x5=$nilai?>

� Use ‘echo’ or ‘print’

� Strings in single quotes (‘ ’) are not interpreted or evaluated by PHP

12

� If the string has a set of double quotation marks

that must remain visible, use the \ [backslash] before the quotation marks to ignore and display

them.

<?php

$jab=“\”PHP\””;

print $jab; //”PHP”

?>

PHP – escape character

PHP code - variables

� PHP variables must begin with a “$” sign

� Case-sensitive ($var != $VAR != $vAr)

� Global and locally-scoped variables

�Global variables can be used anywhere

� Local variables restricted to a function or class

� Certain variable names reserved by PHP� Form variables ($_POST, $_GET)

� Server variables ($_SERVER)

PHP code – variables

<?php

$nilai = 25; //Numerical variable$ayat = “Hello”; //String variable

$nilai = ($nilai * 7); //Multiplies variable nilai by 7

?>

13

PHP code PHP code -- operationsoperations

<?php$a=30;$b=5;$total=$a+$b;print $total; //35print “<p>Jumlah ialah $total</p>”;// Jumlah ialah 35

print $a-$b; //25print $a*$b; //150print $a/$b; //6print $a+=$b; //35print $a%$b; //0

?>

� Use a period to join strings into one.

<?php

$string1=“Hello”;$string2=“PHP”;

$string3=$string1 . “ ” . $string2;

print $string3; //Hello PHP

?>

PHP code – strings function

PHP – Control Statements

� Control structures similar with JavaScript/C++

� if, elseif, else

� switch, case

�while

� for

� foreach

14

PHP PHP -- if, if, elseifelseif, else, else

<?php

$markah = 90;

if ($markah >= 80)echo “Lulus dengan cemerlang";

elseif ($markah >= 40)echo “Lulus";

elseecho “Gagal";

?>

PHP control PHP control –– switchswitch

…switch ($jantina){

case “L”:echo “Lelaki";break;

case “P”:echo “Perempuan";break;

default:echo “Tiada input jantina";

}…

PHP control PHP control –– while loopswhile loops

<?php

$nombor = 1;

while ($nombor!=10){print “Bilangan $nombor”;$nombor++;}

?>

15

PHP control PHP control –– for loopsfor loops

<?php

for ($n = 1; $n<=10; $n++){print “Bilangan $n”;}

?>

PHP control PHP control –– foreachforeach loopsloops

<?php

$numbers = array("one","two","three");

foreach ($numbers as $value) {echo $value . "<br />";}

?>

PHP arrays

� Three kind of arrays:

�Numeric array - An array with a numeric index

�Associative array - An array where each ID key is associated with a value

�Multidimensional array - An array containing one or more arrays

16

PHP – numeric arrays

<?php

//numeric array$cars = array("Saab","Volvo","BMW",“Ford");

echo $cars[2]; //BMW

?>

PHP – associative arrays

<?php

//associative array

$umur = array ("Raju"=>32, "Gopal"=>34, "Samy" => 36); //same as $umur[‘Raju’]=32…

echo $umur[‘Gopal’]; //34

?>

PHP - multi dimensional arrays

<?php

//multidimensional array

$kump = array ("Merah"=> array ("Ali", "Raju", "Joan"), "Biru"=> array ("Abu", "Jason", "Lin"), "Hijau" => array ("David", "Jim", "Mary");

echo $kump [‘Merah’][2]; //Joanecho $kump [‘Hijau’][0]; //David

?>

17

PHP array functions

� array_push() – adds element/s to an array

<?php

$a=array("Dog","Cat");array_push($a,"Horse","Bird");print_r($a);

/*Array ([0]=>Dog [1]=>Cat [2]=>Horse [3]=>Bird) */

?>

PHP array functions

� array_pop() – deletes last element in an array

<?php

$a=array("Dog","Cat","Horse");array_pop($a);print_r($a);

// Array ([0]=>Dog [1]=>Cat)

?>

PHP array functions

� unset() – destroy a variable

$array = array(0, 1, 2, 3);unset($array[2]);

/* array(3) { [0]=>int(0)[1]=>int(1)[3]=>int(3)

} */

18

� Functions MUST be defined before they can be called

� Function headers are of the format

function function_name ($var1, $var2…){

}

� Function names are not case sensitive

PHP - functions

<?php

// This is a function

function darab($arg1, $arg2){

$arg3 = $arg1 * $arg2;

return $arg3;

}

echo darab(12,3); // 36

?>

PHP - functions

� Using “include” to include external files

<?phpinclude “header.php”Include “tarikh.php”include “menubar.php”…

?><?php

print “Tarikh hari ini ialah$date2”;

?>

PHP - include

19

PHP References

• http://www.php.net <- php home page

• http://www.php.net/downloads <- phpdownload page

• http://www.php.net/manual/en/install.windows.php <- php installation manual

• http://www.w3schools.com/php/default.asp <-php online tutorial

THANK YOU