IS Infrastructure Managing Infrastructure and Services Copyright 2016 Curt Hill.
Copyright © 2008-2014 Curt Hill PhP History and Introduction.
-
Upload
conrad-andrews -
Category
Documents
-
view
216 -
download
1
Transcript of Copyright © 2008-2014 Curt Hill PhP History and Introduction.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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>
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>
Client View
Copyright © 2008-2014 Curt Hill
Source
Copyright © 2008-2014 Curt Hill
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
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
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
Example
Copyright © 2008-2014 Curt Hill
<?php print "<?xml ...?><!DOCTYPE ...<HTML>...</HTML>
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