copyright © 2008-2014 curt hill php history and introduction

32
Copyright © 2008-2014 Curt Hill PhP History and Introduction

Upload: conrad-andrews

Post on 26-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Copyright © 2008-2014 Curt Hill

PhP

History and Introduction

Page 2: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Beginnings

• Developed by Rasmus Lerdorf• Member of the Apache Group• Implemented in approximately 1995• Rasmus wanted to track visitors to

his home page• The name is the acronym for

Personal Home Page– This is a dumb name and so became:

PhP: Hypertext Preprocessor

Copyright © 2008-2014 Curt Hill

Page 3: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Growth

• Within about two years of release it was very widespread

• Main focus is on Server Side processing

• Normally used for forms processing and database access

• Most common server side language• Is and was an open source product

– http://www.php.net/

Copyright © 2008-2014 Curt Hill

Page 4: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Versions

• Like most languages PhP has gone through a number of versions

• The most recent is version 5.6– Version 5.6.3 is current

• Versions 5.5 and 5.4 are still available with recent bugs fixed

• Version 4 is still available, but fading fast

Copyright © 2008-2014 Curt Hill

Page 5: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Server Side Options• There are several ways to process

data at the server• CGI - Common Gateway Interface• ASP - Active Server Pages• JSP - Java Server Pages• Java Servlets• Cold Fusion• Server side JavaScript• Clearly there is a market needing to

be servicedCopyright © 2008-2014 Curt Hill

Page 6: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Server Side Processing• Most of these require spawning an

additional thread from the server• They are routines that are outside

of the server• PhP is more like JavaScript

– JavaScript is interpreted by the browser– PhP is interpreted by the server

• Server recognizes PhP by an extension such as .php .php3, .phtml

Copyright © 2008-2014 Curt Hill

Page 7: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Server or Client Side• We typically have one server and a

myriad of clients• Thus we prefer to let the client

machine do as much processing as possible to ease the server workload

• There are some things that just cannot be done client-side, so we use PhP for this– Such as database access

Copyright © 2008-2014 Curt Hill

Page 8: Copyright © 2008-2014 Curt Hill PhP History and Introduction

PhP File Contents

• A PhP file must have PhP code• It may also contain HTML or any

type of client side script– HTML in the broadest sense: HTML or

XHTML

• The server will either copy or interpret the file– Or both

Copyright © 2008-2014 Curt Hill

Page 9: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Server Modes• When the server sees HTML it merely

copies this to the client– Copy mode

• When the server sees PhP it interprets this– Only the program output reaches the

client– Interpret mode

• Many files have both copy and interpretation

• PhP is transparent– Client never sees any of it

Copyright © 2008-2014 Curt Hill

Page 10: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Compilation or Interpretation?

• Historically PhP has always been interpreted by the server

• There is a performance penalty for this

• Types of pre-compilation have been done to speed up the script– Helpful for large and complicated

scripts– Eases server workload

Copyright © 2008-2014 Curt Hill

Page 11: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Language Overview

• PhP has much in common with both Perl and JavaScript

• Dynamic typing– No type declaration– Variables may change type during a

program

• Forgiving syntax• Large libraries

Copyright © 2008-2014 Curt Hill

Page 12: Copyright © 2008-2014 Curt Hill PhP History and Introduction

PhP Script Tag• The script tag has following form:<?php … ?>

• You may also use<script language=“php”>

• There may be multiple scripts in a file

• This tag is replaced by the program output before the client sees it– Even View Page Source will not show

the PhP code

Copyright © 2008-2014 Curt Hill

Page 13: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Other Tags

• PhP may also be enclosed in short tags or ASP tags

• A short tag is just the:<? … ?>

• An ASP tag is:<% … %>

• The previous two are always enabled but these two may need mention in the initialization file of the server

Copyright © 2008-2014 Curt Hill

Page 14: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Some Reserved Words

Copyright © 2008-2014 Curt Hill

and break case class const

continue

declare

default do echo

else elseif extends

false for

foreach function

global if include

list new not or print

require return static switch this

true var virtual xor while

Page 15: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Comments

• Three types• Two single line comments:

– # - the same as Perl– // - the same as C++/Java

• Multiple line comments:– Starts with /*– Ends with */– Similar to C/C++/Java among others

Copyright © 2008-2014 Curt Hill

Page 16: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Variables• Variables must start with a dollar

sign• This must be followed by letters,

underscores or digits• Variables are case sensitive• Reserved words are not case

sensitive• There is no way to declare a variable

– Just use it

Copyright © 2008-2014 Curt Hill

Page 17: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Stropping– Marking names with special characters

• PhP is unusual in that it forces variables to start with a special character, the $

• This has several nice results• It makes parsing easy• No confusion between a variable and

anything else• A variable may be recognized as a

variable even within a string• Makes for harder typing

Copyright © 2008-2014 Curt Hill

Page 18: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Types• PhP has four scalar or primitive

types:• Boolean• integer• double• string

• There are two structured types:• array• object

• Two special types:• resource• NULL

Copyright © 2008-2014 Curt Hill

Page 19: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Statements• Statements in PhP are terminated by

semicolons• The last one in a PhP script may be omitted

• The braces are used to wrap multiple statements into one like in the C family of languages

• There are two levels of scope– Global scope– Scope of a function body

• Most of the operators of Java still exist

Copyright © 2008-2014 Curt Hill

Page 20: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Output

• There are several output statements of note print, printf and echo

• Today we will only consider print• Form:print “Any string”;

• Either quote may be usedprint ‘Hi there.’;

Copyright © 2008-2014 Curt Hill

Page 21: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Multiline Print

• Print may output multiple lines:print “This stuff and some more and the last.”;

• It will actually put out three lines with newlines (\n) in between

• However, if the recipient is a web browser the redundant white space (including the newline) is compacted

Copyright © 2008-2014 Curt Hill

Page 22: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Printing variables

• Very easy• Print variables directly:print $count;

• Variables may be recognized inside double quotes:print “The answer is: $ans”;

• This makes formatting output easy

Copyright © 2008-2014 Curt Hill

Page 23: Copyright © 2008-2014 Curt Hill PhP History and Introduction

include• The include function inserts another

file into the PhP program• The form is:include(“filename”);– This file does not need the .php

extension

• The file may be HTML, client script or PhP

• The server enters copy mode at the beginning of the inclusion

Copyright © 2008-2014 Curt Hill

Page 24: Copyright © 2008-2014 Curt Hill PhP History and Introduction

As Seen by the Server

Copyright © 2008-2014 Curt Hill

<HTML><HEAD><TITLE>PHP Demo 1</TITLE></HEAD><BODY><H1><CENTER>PHP Demo Page #1</CENTER></H1><P>Here is the slot.<P><HR><?php print "<B>Hello World!</B><BR/>"; ?><HR><P><A HREF="cis420home.htm" target="_top"> Return to the CIS 420 Home Page</A>.</BODY></HTML>

Page 25: Copyright © 2008-2014 Curt Hill PhP History and Introduction

As Seen by the Browser

Copyright © 2008-2014 Curt Hill

<HTML><HEAD><TITLE>PHP Demo 1</TITLE></HEAD><BODY><H1><CENTER>PHP Demo Page #1</CENTER></H1><P>Here is the slot.<P><HR><B>Hello World!</B><BR/><HR><P><A HREF="cis420home.htm" target="_top"> Return to the CIS 420 Home Page</A>.</BODY></HTML>

Page 26: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Client View

Copyright © 2008-2014 Curt Hill

Page 27: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Source

Copyright © 2008-2014 Curt Hill

Page 28: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Example commentary

• PhP is typically inside HTML so when it does prints, it uses HTML tags

• All the PhP disappeared, but the output of the PhP program is retained– All the raw HTML was merely copied

Copyright © 2008-2014 Curt Hill

Page 29: Copyright © 2008-2014 Curt Hill PhP History and Introduction

phpinfo()

• This function will show you the characteristics of the PhP interpreter on this system

• It will show things like:– Operating System– PhP version– Configurations information

Copyright © 2008-2014 Curt Hill

Page 30: Copyright © 2008-2014 Curt Hill PhP History and Introduction

XML• Did you notice the similarities

between PhP tags and XML?<?php … ?><?xml … ?>

• If short tags are enabled this causes some problems

• One of which is that an XML file will be served as empty

• Solutions: • Only use HTML - no XML header• Have PhP write out the XML header• Disable short tags

Copyright © 2008-2014 Curt Hill

Page 31: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Example

Copyright © 2008-2014 Curt Hill

<?php print "<?xml ...?><!DOCTYPE ...<HTML>...</HTML>

Page 32: Copyright © 2008-2014 Curt Hill PhP History and Introduction

Finally

• PhP seems to be the most popular of the server side scripting languages– However, there is money to be made

here so competition is fierce

• It has very good libraries for accessing databases– The SQL example pages all use PhP

scripts

Copyright © 2008-2014 Curt Hill