Introduction to PHP - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to PHP

Description:

Introduction to PHP Chapter 8 Working with PHP JavaScript vs. PHP PHP scripts are similar to JavaScript scripts, but be careful with syntax! – PowerPoint PPT presentation

Number of Views:169
Avg rating:3.0/5.0
Slides: 20
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 Chapter 8
  • Working with PHP

2
JavaScript vs. PHP
  • PHP scripts are similar to JavaScript scripts,
    but be careful with syntax!
  • PHP variables always begin with a symbol.
    There is no equivalent to JavaScripts var data
    declaration.
  • PHP scripts are embedded within a
  • lt?php ?gt tag.
  • PHP functions are similar to JavaScript
    functions, but argument passing is easier.
  • PHP functions can return a single value, with
    multiple values returned as elements in an array.
  • PHP scripts can read data from and write data to
    a file on a remote server.
  • PHP outputs can be used within the PHP
    application, but not passed back to JavaScript.

3
Solving the Water Vapor Problem
  • Pass instrument serial number, time and place,
    and instrument output voltages.
  • Find calibration constants for the instrument.
  • Calculate suns position based on time and place
    (long!).
  • Calculate total column water vapor (short).
  • Where should the solar position calculations be
    done (they depend on time and place, but are
    independent of the instrument outputs)?

4
Decisions, decisions
  • Solar position calculations could be done in
    JavaScript, but we will do them in PHP to learn
    how to use the language.
  • Document 8.1 gives a complete JavaScript
    solution, assuming that the instrument
    calibration constants area known. Eventually,
    these calculations need to be translated into PHP.

5
A self-contained JavaScript solution
6
JavaScript
function getSunpos(m,d,y,hour,minute,second,Lat,Lo
n) with (Math) // Explicit type conversions
to make sure inputs are treated like numbers, not
strings. mparseInt(m,10) dparseInt(d,10)
yparseInt(y,10) hourparseFloat(hour)
minuteparseFloat(minute) secondparseFloat(se
cond) LatparseFloat(Lat) LonparseFloat(Lon)

Note the use of parseFloat() and parseInt() to
convert form field values to numbers.
function get_PW(IR1,IR2,A,B,C,beta,tau,airm,p)
var x Cairmtau - (Math.log(IR2/IR1)-A)/B
var PW Math.pow(x,1./beta)/airm return
Math.round(PW1000.)/1000.
7
PHP equivalent
mgetSunpos(_POST"mon",_POST"day",_POST"y
r",_POST"hr", _POST"min",_POST"sec", _P
OST"lat",_POST"lon")
x Cmtau - (log(IR2/IR1)-A)/BPW
pow(x,1./beta)/m
8
An HTML interface to PHP
9
Output from PHP
10
Returning multiple values
Document 8.4. (circleStuff.php) lt?php/ Note
that this function CIRCLESTUFF(r) will
also work because PHP function names are
case-insensitive!/ function CircleStuff(r)
areaM_PIrr circumference2M_PIr
/ However, this won't work return
array(AREA,circumference) because variable
names are case-sensitive./ return
array(area,circumference) list(area,circ
umference) CircleStuff(3) echo area . ", " .
circumference?gt
11
More about file I/O
A text file contains wind speed data 1 1991 31
3.2, 0.4, 3.8, 4.5, 3.3, 1.9, 1.6, 3.7, 0.8, 2.3,
2.8, 2.4, 2.5, 3.2, 4.1, 3.9, 5.0, 4.4, 4.4, 5.5,
3.0, 3.7, 2.2, 2.0 2.6, 2.8, 2.3, 2.3, 1.2, 2.4,
3.1, 4.0, 3.6, 2.9, 6.0, 4.4, 0.8, 3.8, 3.5, 4.5,
2.7, 3.4, 6.6, 5.2, 1.6, 1.2, 2.3, 2.4 2 1991
28 4.6, 5.9, 3.1, 3.2, 4.5, 4.4, 3.9, 4.4, 7.5,
8.4,10.2, 9.2, 8.1, 6.3, 3.1, 3.5, 2.2, 1.4, 0.4,
4.2, 5.4, 4.0, 2.9, 1.7 2.5, 2.3, 2.1, 1.5, 2.3,
4.1, 5.3, 6.0, 6.0, 9.7,11.3,12.7,13.0,13.0,11.6,
9.9, 9.6, 8.7, 5.4, 5.1, 5.3, 5.6, 4.4, 4.2
For each day, there are 24 hourly wind speed
values. Missing hours are represented by a value
of -1. Read this file and count and display the
month (1-12) and the number of missing values for
each month. Write the results into a file and
save it.
12
Document 8.4. (windspd.php) lt?phpinFile"windsp
d.dat"outFile"windspd.out"in
fopen(inFile, "r") or die("Can't open
file.")outfopen("c/Documents and
Settings/ All Users/Documents/phpout/".outFile,"w
")while (!feof(in)) // Read one month, year,
of days. fscanf(in,"u u u",m,y,nDays)
if (feof(in)) exit echo m . ', ' . y . ',
' . nDays . 'ltbr /gt' nMissing0 for
(i1 iltnDays i) hrly
fscanf(in, "f, f, f, f, f, f, f, f, f,
f, f, f, f, f, f, f, f, f, f, f, f,
f, f, f") for (hr0 hrlt23 hr)
echo hrlyhr . ', ' if (hrlyhr -1)
nMissing echo hrly23 . 'ltbr
/gt' echo 'Number of missing hours this
month is ' . nMissing . '.ltbr
/gt' fprintf(out,"u, u, u\r\n",m,y,nMissin
g)fclose(in)fclose(out)?gt
13
Another application
Write an HTML document that allows a user to
select a solid object shape and enter its
dimensions and the material from which it is
made. The choices could be a cube, a rectangular
block, a sphere, or a cube. You could choose
a number of possible materialsair, gold, water,
etc. Then call a PHP application that will find
the mass of the object by calculating its volume
based on the specified shape and looking up the
density of the material in a data file.
14
Document 8.5a (getMass.htm) lthtmlgtltheadgtlttitlegtCa
lculate masslt/titlegtlt/headgtltbodygtltform
method"post" action"getMass.php"gtEnter length
ltinput type"text" name"L" value"3" /gtltbr
/gtEnter width ltinput type"text" name"W"
value"2" /gtltbr /gtEnter height ltinput
type"text" name"H" value"10" /gtltbr /gtEnter
radius ltinput type"text" name"R" value"3"
/gtltbr /gtltselect name"shapes" size"10"gt
ltoption value"cube"gtcubelt/optiongt ltoption
value"cylinder"gtcylinderlt/optiongt ltoption
value"block"gtrectangular blocklt/optiongt
ltoption value"sphere"gtspherelt/optiongtlt/selectgtlt
select name"material" size"10"gt ltoption
value"air"gtairlt/optiongt ltoption
value"aluminum"gtaluminumlt/optiongt ltoption
value"gold"gtgoldlt/optiongt ltoption
value"oxygen"gtoxygenlt/optiongt ltoption
value"silver"gtsilverlt/optiongt ltoption
value"water"gtwaterlt/optiongt lt/selectgtltinput
type"submit" value"Click to get
volume." lt!-- ltinput type"button"
value"click" onclick"alert(document.form1.shape
s.selectedIndex) alert(shapes.optionsshapes.sel
ectedIndex.value) " --gt /gtlt/formgtlt/bodygtlt/htm
lgt
15
PHP, the first step
Display the input values
lt?php print_r(_POST)?gt This code will
display something like this Array ( L gt 1
W gt 1 H gt 1 R gt 3 shapes gt cube
material gt oxygen )
16
Create data files
(density.dat) material density (kg/m3) water
1000 aluminum 2700 gold 19300 silver 10500 oxygen
1.429 air 1.2
(volume.dat) shape volume cube LLL sphere
4/3M_PIRRR cylinder M_PIRRL block
LWH
17
Find the material
Document 8.5b (getMass.php) lt?phpprint_r(_POST)
material_POSTmaterialshape_POSTshapes
L_POSTLW_POSTWH_POSTHR_
POSTRecho "ltbr /gt" . material . ", " .
shape . "ltbr /gt"materialFilefopen("density.da
t","r")shapeFilefopen("volume.dat","r")//
Read materials file.foundfalselinefgets(ma
terialFile)while ((!feof(materialFile))
(!found)) valuesfscanf(materialFile,"s
f",m,d) if (strcasecmp(material,m) 0)
echo material . ", " . m . ", " . d .
"ltbr /gt" foundtrue
18
Calculate the volume
// Read volume file.foundfalselinefgets(sh
apeFile)while ((!feof(shapeFile))
(!found)) valuesfscanf(shapeFile,"s
s",s,v) if (strcasecmp(shape,s) 0)
echo shape . ", " . v . "ltbr
/gt" foundtrue // Close both data
files.fclose(materialFile)fclose(shapeFile)
// Calculate mass.vvv . "d"echo "Result
".eval("return round(vv,3)")." kgltbr /gt" ?gt
This is the clever code!
19
Self-contained HTML/PHP applications
Document 8.11 (CompoundInterest.php)   lthtml
gtltheadgtlttitlegtCalculate Compound
Interestlt/titlegtlt/headgtltbodygtlth3gtCalculate
Compound Interestlt/h3gtltform action"lt?php
_SERVER'PHP_SELF'?gt" method"post"gtInitial
amount (no commas), ltinput type"text"
name"initial" value"10000" /gtltbr /gtAnnual
interest rate, ltinput type"text" name"rate"
value"4" /gtltbr /gtHow many years? ltinput
type"text" name"years" value"20" /gtltbr
/gtltinput type"submit" value"Generate
compound interest table." /gtlt/formgt
lt?php initial_POST"initial" rate_POST"
rate" years_POST"years" echo initial."
".rate." ".years."ltbr /gt" for (i1
iltyears i) amountinitialpow(1rat
e/100,i) echo i." ".number_format(amount,2)
."ltbr /gt" ?gtlt/bodygtlt/htmlgt
Write a Comment
User Comments (0)
About PowerShow.com