Introduction to php

30
Introduction to PHP Ahmad Karawash 2015

Transcript of Introduction to php

Page 1: Introduction to php

Introduction to PHPAhmad Karawash

2015

Page 2: Introduction to php

Karawash - PHP Introduction 2

ObjectivesTo understand what PHP is and how a PHP script works with a Web

Browser and a Web ServerTo learn what software and components

you need to get started with PHPTo create and run a simple PHP script

1/19/2015

Page 3: Introduction to php

Karawash - PHP Introduction 3

Client–Server model

1/19/2015

The client–server model of computing is a distributed application structure that partitions tasks between the providers of a service, called servers, and service requesters, called clients.

Page 4: Introduction to php

Karawash - PHP Introduction 4

Client-Side ScriptingClient scripts are interpreted by the web

browserFor example: JavaScriptClient scripts can:

Alter the appearance of the document Validate form fields Perform general computational tasks

Client scripts can be embedded or attached

1/19/2015

Page 5: Introduction to php

Karawash - PHP Introduction 5

Server-Side ScriptingServer scripts are interpreted by the web

serverCommon languages are

PHPASP JSP Perl

1/19/2015

Page 6: Introduction to php

Karawash - PHP Introduction 6

What is PHP?PHP is a computer scripting languageDeveloped by Rasmus Lerdorf from the Apache GroupAppeared in 1995Originally designed for producing dynamic web pagesRuns on most operating systems and platformsURL: http://www.php.net

1/19/2015

Page 7: Introduction to php

Karawash - PHP Introduction 7

What is PHP?Advantages of Using PHP to enhance

Web pages: Easy to use. Open source. Works on Multiple platforms.

1/19/2015

Page 8: Introduction to php

Karawash - PHP Introduction 8

How PHP Pages are Accessed andInterpreted

Web browser

3. Receive request find file and read it

4. Execute PHP statement

5. Send results back

Web Server1. Enter name

Submit cancel

7. Result

Web browser

This is your phone nb: xxx

2. Send request

6. Receive

request

1/19/2015

Page 9: Introduction to php

Karawash - PHP Introduction 9

How PHP Pages are Accessedand InterpretedHow do Web Servers work?

Client specifies document at a specific web address that is desired (specified by a URL) Ex: http://www.MUM.com

If the document is html or text, the server simply forwards it back to the clientIt is then rendered in the client's browser

However, if it has embedded PHP, the server first executes the PHP, replacing the PHP code with its output in the document

1/19/2015

Page 10: Introduction to php

Karawash - PHP Introduction 10

How PHP Pages are Accessedand InterpretedThe modified document is then sent back to

the clientNote that the client never sees the PHP code

This is important – typically client should not see logic / code that server executes to process requests

The server may be accessing files whose names should not be seen by the client

The only reason the client even knows PHP is involved is due to the file extension : .php

1/19/2015

Page 11: Introduction to php

Karawash - PHP Introduction 11

Getting Started with PHPTo develop and publish PHP scripts all

what you need is:A Web server with PHP built into itA client machine with a basic text editor

and Internet connectionFTP or Telnet software

1/19/2015

Page 12: Introduction to php

Karawash - PHP Introduction 12

Exploring the Basic PHPDevelopment ProcessThe basic steps you can use to develop and

publish PHP pages are:1. Create a PHP script file and save it to a

local disk.2. Use FTP to copy the PHP file to the server.3. Access your file using a browser.

1/19/2015

Page 13: Introduction to php

Karawash - PHP Introduction 13

Creating a PHP Script File andSaving It to a Local DiskYou can use a number of different

editors to create your PHP script files.The PHP script starts with a <?php tag

and ends with ?>.Between these tags there is a single PHP

print statement.

1/19/2015

Page 14: Introduction to php

Karawash - PHP Introduction 14

Accessing Your File Using aBrowser

1/19/2015

Page 15: Introduction to php

Karawash - PHP Introduction 15

SyntaxIf you have a syntax error then you have

written one or more PHP statements that are grammatically incorrect in the PHP language.

1/19/2015

Page 16: Introduction to php

Karawash - PHP Introduction 16

If Use Improper SyntaxSuppose you use the wrong syntax:For example error with quotation mark1. <?php2.3. print ( “Hello World !);4. ?>

1/19/2015

Page 17: Introduction to php

Karawash - PHP Introduction 17

A Little About PHP's SyntaxSome PHP Syntax Issues:

Be careful to use quotation marks, parentheses, and brackets in pairs.

Most PHP commands end with a semicolon (;).

Be careful of letter case.PHP ignores blank spaces.

1/19/2015

Page 18: Introduction to php

Karawash - PHP Introduction 18

Embedding PHP StatementsWithin HTML DocumentsOne way to use PHP is to embed PHP scripts

within HTML tags in an HTML document.1. <html>2. <head>3. <title>HTML With PHP Embedded</title> </head>4. <body>5. <font size=5 color=”green”>Welcome To My

Page</font>6. <?php7. print ("<br> Using PHP is not hard<br>");8. ?>9. and you can learn to use it quickly!10. </body></html>

1/19/2015

Page 19: Introduction to php

Karawash - PHP Introduction 19

Would Output The Following ...

1/19/2015

Page 20: Introduction to php

Karawash - PHP Introduction 201/19/2015

VariablesVariables in PHP are represented by a dollar

signPHP supports eight types:

boolean, integer, float, double, array, object, resource and NULL

Page 21: Introduction to php

Karawash - PHP Introduction 21

Variables example<?php

$x=“Hello”;Print($x);

?>

1/19/2015

Page 22: Introduction to php

Karawash - PHP Introduction 22

Using Comments withPHP ScriptsComments enable you to include

descriptive text along with the PHP script.

Comment Syntax - Use //<?php// This is a comment?>

Can place on Same line as a statement:<?phpprint ("A simple initial script"); //Output a line?>

1/19/2015

Page 23: Introduction to php

Karawash - PHP Introduction 23

Example Script with Comments1. <html> <head>2. <title> page title</title> </head>3. <body> <h1> Comment example PHP</h1>4. <?php5. //6. // Example script to output HTML tags7. //8. print (“In PHP you can use: <i>Comments</i>");9. print ("</body></html>");10. ?>

1/19/2015

Page 24: Introduction to php

Karawash - PHP Introduction 24

Alternative Comment SyntaxMultiple line comments.

<?php/*A script that gets information about thePHP version being used.*/?>

1/19/2015

Page 25: Introduction to php

Karawash - PHP Introduction 25

SummaryYou can embed a PHP script within an HTML

document or run it as a stand-alone script.

To begin working with PHP you need a Web server with built-in PHP, a client machine with a basic text editor, and FTP or Telnet software.

PHP script process: write the PHP script, copy its file to the Web server, and access the file with a Web browser.

Comments can be proceeded with two forward slashes (//) or (/* … */).

1/19/2015

Page 26: Introduction to php

Karawash - PHP Introduction 26

Simple HTTP Request

1/19/2015

Page 27: Introduction to php

Karawash - PHP Introduction 27

HTTP With JavaScript

The client browser parses the HTML file, finds the script and executes it locally

1/19/2015

Page 28: Introduction to php

Karawash - PHP Introduction 28

HTTP on Demand1. Web server loadsthe script from itsfile system

2. Web serverpasses the scriptto the interpreter

3. The interpreterruns the script andpasses its output tothe web server

4. The web serverblindly passes theoutput to the client

1/19/2015

Page 29: Introduction to php

Karawash - PHP Introduction 29

Data-driven HTTP Request

1/19/2015

Page 30: Introduction to php

Karawash - PHP Introduction 30

Three-tiered Web Site

1/19/2015

ClientUser-agent: Firefox

ServerApache HTTP Server

example requestGET / HTTP/1.1Host: www.tamk.fiUser-Agent: Mozilla/5.0 (Mac..)...

response

DatabaseMySQL

PHPPHP