Introduction to PHP - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to PHP

Description:

Introduction to PHP To boldly go where JavaScript has never been JavaScript vs. PHP JavaScript is a client-side language that resides in your browser. – PowerPoint PPT presentation

Number of Views:208
Avg rating:3.0/5.0
Slides: 18
Provided by: David11090
Learn more at: https://instesre.org
Category:
Tags: php | introduction

less

Transcript and Presenter's Notes

Title: Introduction to PHP


1
Introduction to PHP
  • To boldly go where JavaScript has never been

2
JavaScript vs. PHP
  • JavaScript is a client-side language that resides
    in your browser. It is available for use with no
    effort on your part.
  • PHP must be installed on a server (local or
    remote), and you must have specific permissions
    to access PHP applications.
  • JavaScript cannot access any information outside
    an HTML document.
  • PHP applications can access and create files
    stored elsewhere in a server.

3
A typical server-side problem
A user enters information about measurements
taken with a sun-viewing instrument (a sun
photometer) that is used to measure total column
water vapor in the atmosphere. The information
provided by the user consists of the
instruments serial number, the location and
time of the measurements, and voltage outputs
from the instrument. The application must use
this information to find the location of the sun
at the time and place of the measurement and
then calculate total column water vapor based on
calibration constants stored for the instrument
used to collect the data.
4
The data file might look like this
SN A B C beta tau WV2-113 0.762 0.468 0.20 0.65
0.10 WV2-114 0.814 0.468 0.20 0.650.10 WV2-157
0.911 0.468 0.20 0.65 0.10
5
The HTML document
Document 1.1. (getCalib.htm) lthtmlgtltheadgtlttitlegt
Get calibration constantlt/titlegtltscript
language"javascript"gt document.write("This
document last modified on "document.lastModified
".")lt/scriptgtlt/headgtltbodygtlth2gtGet calibration
constants for water vapor instrumentlt/h2gtltpgtltfor
m method"post" action"getCalib.php"gtEnter
serial number here ltinput type"text" name"SN"
value"WV2-157" /gtltbr /gtltinput type"submit"
value"Click here to get calibration constants"
/gtlt/bodygtlt/htmlgt
6
Steps to using PHP
1. Setting up a PHP environment.
  • Install a server on a computer (local or remote).
  • ownload and install a PHP interpreter.
  • Configure the server to recognize PHP documents
    and
  • to locate the PHP interpreter.

7
Steps to using PHP
2. Creating, editing, and executing PHP
documents.
  • PHP documents are text files that can be created
    with
  • any text editor. (A real code editor such as
    AceHTML is
  • better.)
  • 2. Figure out where to store PHP documents. With
    the WAMP
  • server, they are stored in /wamp/www.
  • 3. Create and edit PHP documents in an editor.
  • 4. Execute the documents from your browser. With
    Windows
  • IIS the URL is localhost/PHP file name.
  • 5. Make required changes in your editor and
    refresh the
  • application in your browser.

8
Steps to using PHP
3. Passing information from an HTML Document to a
PHP application.
By design, this is very easy to do in PHP! The
statement ltform method"post" action"getCalib.ph
p"gt Automatically passes all form field values
to the PHP document to a system-defined array
called _POST. In the PHP document, SN_POST
SN" retrieves the instrument serial number
passed from Document 1.1.
9
Looking for a match
Document 1.2. (getCalib.php) lt?php// Extract
instrument ID from POST data
SN_POST"SN" lenstrlen(SN)// Open WV
instrument calibration constant file inFile
"WVdata.dat" in fopen(inFile, "r") or
exit("Can't open file")// Read one header
line linefgets(in)// Search rest of file
for SN match found0 while ((!feof(in))
(found 0)) valuesfscanf(in,"s f
f f f f") list(SN_dat,A,B,C,beta,ta
u)values if (strncasecmp(SN_dat,SN,len)
0) found1 fclose(in)
10
Several ways to read data from file
Read values from the file into an array,
according to a specified format. Then assign the
elements to named variables
valuesfscanf(in,"s f f f f f")
list(SN_dat,A,B,C,beta,tau)values
Read an entire line as a string and then read
values from the string according to a specified
format
linefgets(in)list(SN_dat,A,B,C,beta,tau
)sscanf(line,"s f f f f f")
Read an entire line and assign named variables
directly, according to a specified format
fscanf(in,"s f f f f f",SN_dat,A,B,C,b
eta,tau)
11
Processing the data
if (found 0) echo "Couldn't find this
instrument." else // Build table of
outputs echo "ltpgtlttable border'2'gtlttrgtltthgtQu
antitylt/thgt ltthgtValuelt/thgtlt/trgt"."lt/tdgtlt/trgt
" echo "lttrgtlttdgtInstrument IDlt/tdgt
lttdgtSNlt/tdgtlt/trgt" echo "lttr
bgcolor'silver'gt
lttd colspan'2'gtCalibration Constantslt/tdgt
lt/trgt" echo "lttrgtlttdgtAlt/tdgtlttdgtAlt/tdgtlt/
trgt" echo "lttrgtlttdgtBlt/tdgtlttdgtBlt/tdgtlt/trgt"
echo "lttrgtlttdgtClt/tdgtlttdgtClt/tdgtlt/trgt"
echo "lttrgtlttdgttault/tdgtlttdgttault/tdgtlt/trgt"
echo "lttrgtlttdgtbetalt/tdgtlttdgtbetalt/tdgtlt/trgt"
echo "lt/tablegt" ?gt
12
Saving your results on a server
inFile "WVdata.dat"outFile"WVreport.csv"
in fopen(inFile, "r") or exit("Can't open
file.") outfopen( "c/Documents and
Settings/All Users/Documents/PHPout/" .outFile,"a
")
fprintf(out, "Data have been reported for
s,f,f,f,f,f\n", SN,A,B,C,tau,beta)fc
lose(out)
13
Solving a quadratic equation
For the quadratic equation ax2 bx c 0, find
the real roots r1 -b (b2 4ac)1/2 /2a
r2 -b - (b2 4ac)1/2 /2a The a coefficient
must not be 0. If the discriminant, b2 4ac
0, there is only one root. If the discriminant
is less than 0, there are no real roots.
14
Document 1.6a (quadrat.htm) lttitlegtSolving the
Quadratic Equationlt/titlegtlt/headgtltbodygtltform
method"post" action"quadrat.php"gtEnter
coefficients for axltsupgt2lt/supgt bx c 0ltbr
/gta ltinput type"text" value"1" name"a" /gt
(must not be 0)ltbr /gtb ltinput type"text"
value"2" name"b" /gtltbr /gtc ltinput
type"text" value"-8" name"c" /gtltbr /gtltbr
/gt ltinput type"submit" value"click to get
roots..." /gtlt/formgtlt/bodygtlt/htmlgt
15
Document 1.6b (quadrat.php) lt?phpa
_POST"a"b _POST"b"c
_POST"c"d bb - 4.acif (d 0)
r1 b/(2.a) r2 "undefined"else
if (d lt 0) r1 "undefined" r2
"undefined"else r1 (-b sqrt(bb -
4.ac))/2./a r2 (-b - sqrt(bb -
4.ac))/2./aecho "r1 " . r1 . ", r2
" . r2 ?gt
16
Preventing multiple form submissions
Document 1.7a (WeatherReport.htm) lthtmlgtltheadgtltt
itlegtWeather Reportlt/titlegtltscript
language"javascript" type"text/javascript" gt
var alreadySubmitted false function
submitForm ( ) if (alreadySubmitted)
alert("Data already submitted. Click on
'Reset Button' and start over." ) return
false else
alreadySubmitted true return true
lt/scriptgtlt/headgt
17
Multiple submissions, part 2
This code allows only one submit, after which
you have to click the Reset button. Note that
this is a JavaScript solution, but the problem
often occurs when you are submitting data to a
PHP application that appends to an existing file.
ltbodygtlth2gtReport weather observationslt/h2gtltform
method"post" action"WeatherReport.php"
onSubmit"return submitForm(this.form)" gt Date
(mm/dd/yyyy) ltinput type"text" name"date"
value"09/23/2007" /gtltbr /gt Time (UT hhmmss)
ltinput type"text" name"time" value"170000"
/gtltbr /gt Air temperature (deg C)ltinput
type"text" name"T" value"23" /gtltbr /gt
Barometric pressure (millibar) ltinput
type"text" name"BP" value"1010" /gtltbr /gt
Cloud cover (octas 0-8) ltinput type"text"
name"octas" value"7" /gtltbr /gt Precipitation
today (total mm) ltinput type"text"
name"precip" value"2.3" /gtltbr /gt ltinput
type"submit" name"PushButton" value"Click to
submit..." /gtltbr /gt ltinput type"reset"
value"Reset Button" onClick"alreadySubmitted
false"/gtlt/bodygtlt/htmlgt
Write a Comment
User Comments (0)
About PowerShow.com