Intro to PHP What is PHP? Language developed by - - PowerPoint PPT Presentation

intro to php
SMART_READER_LITE
LIVE PREVIEW

Intro to PHP What is PHP? Language developed by - - PowerPoint PPT Presentation

Intro to PHP What is PHP? Language developed by Rasmus Lerdorf from the Apache Group Its primary use is for server-side scripAng Ex:


slide-1
SLIDE 1

1 ¡

Intro ¡to ¡PHP ¡

  • What ¡is ¡PHP? ¡

– Language ¡developed ¡by ¡Rasmus ¡Lerdorf ¡from ¡the ¡ Apache ¡Group ¡ – Its ¡primary ¡use ¡is ¡for ¡server-­‑side ¡scripAng ¡

  • Ex: ¡To ¡process ¡HTML ¡forms ¡
  • Ex: ¡To ¡perform ¡a ¡DB ¡query ¡and ¡pass ¡on ¡results ¡
  • Ex: ¡To ¡dynamically ¡generate ¡HTML ¡

– PHP ¡scripts ¡are ¡oJen ¡embedded ¡within ¡HTML ¡ documents ¡

  • The ¡server ¡processes ¡the ¡HTML ¡document, ¡execuAng ¡the ¡

PHP ¡segments ¡and ¡subsAtuAng ¡the ¡output ¡within ¡the ¡HTML ¡ document ¡

Lecture ¡2 ¡

slide-2
SLIDE 2

2 ¡

Intro ¡to ¡PHP ¡

– The ¡modified ¡document ¡is ¡then ¡sent ¡to ¡the ¡client ¡ ¡ – As ¡menAoned ¡previously, ¡the ¡client ¡never ¡sees ¡the ¡ PHP ¡code ¡

  • The ¡only ¡reason ¡the ¡client ¡even ¡knows ¡PHP ¡is ¡involved ¡is ¡

due ¡to ¡the ¡file ¡extension ¡à ¡.php ¡

  • But ¡even ¡this ¡is ¡not ¡required ¡if ¡the ¡server ¡is ¡configured ¡

correctly ¡

– The ¡server ¡can ¡be ¡configured ¡to ¡run ¡PHP ¡scripts ¡even ¡if ¡the ¡file ¡ does ¡not ¡have ¡a ¡.php ¡extension ¡ – By ¡default ¡XAMPP ¡will ¡only ¡execute ¡PHP ¡if ¡the ¡.php ¡extension ¡is ¡ provided ¡ – See ¡what ¡happens ¡if ¡you ¡have ¡PHP ¡code ¡without ¡the ¡.php ¡ extension ¡

Lecture ¡2 ¡

slide-3
SLIDE 3

3 ¡

Intro ¡to ¡PHP ¡

  • PHP ¡is ¡a ¡HUGE ¡language ¡

– It ¡is ¡a ¡fully ¡funcAonal ¡language ¡ – It ¡has ¡an ¡incredible ¡amount ¡of ¡built-­‑in ¡features ¡

  • Form ¡processing ¡
  • Output ¡/ ¡generate ¡various ¡types ¡of ¡data ¡(not ¡just ¡text) ¡
  • Database ¡access ¡

– Allows ¡for ¡various ¡DBs ¡and ¡DB ¡formats ¡

  • Object-­‑oriented ¡features ¡

– Somewhat ¡of ¡a ¡loose ¡hybrid ¡of ¡C++ ¡and ¡Java ¡

  • Huge ¡funcAon ¡/ ¡class ¡library ¡

Lecture ¡2 ¡

slide-4
SLIDE 4

4 ¡

Intro ¡to ¡PHP ¡

  • We ¡will ¡look ¡at ¡only ¡a ¡small ¡part ¡of ¡PHP ¡

– There ¡are ¡also ¡many ¡tools ¡that ¡are ¡already ¡pre-­‑wri^en ¡ in ¡/ ¡for ¡PHP ¡

  • If ¡you ¡are ¡building ¡a ¡substanAal ¡project ¡you ¡may ¡want ¡to ¡use ¡

some ¡of ¡these ¡

– Ex: ¡h^p://github.com/pear ¡ – Also ¡see ¡h^p://www.php.net/sites.php ¡

  • There ¡are ¡also ¡content ¡management ¡systems ¡wri^en ¡in ¡PHP ¡

– Ex: ¡h^p://drupal.org/ ¡ ¡ – Ex: ¡h^p://wordpress.org/ ¡ ¡

  • However, ¡we ¡may ¡not ¡be ¡covering ¡them ¡here ¡

– We ¡will ¡focus ¡on ¡the ¡straight ¡PHP ¡language ¡

Lecture ¡2 ¡

slide-5
SLIDE 5

Intro to PHP

  • PHP ¡Program ¡Structure ¡

– Or ¡really ¡lack ¡thereof ¡ – PHP, ¡as ¡with ¡many ¡scripAng ¡languages, ¡does ¡not ¡ have ¡nearly ¡the ¡same ¡structural ¡requirements ¡as ¡a ¡ language ¡like ¡Java ¡ – A ¡script ¡can ¡be ¡just ¡a ¡few ¡lines ¡of ¡code ¡or ¡a ¡very ¡ large, ¡structured ¡program ¡with ¡classes ¡and ¡objects ¡

  • The ¡complexity ¡depends ¡on ¡the ¡task ¡at ¡hand ¡

– However, ¡there ¡are ¡some ¡guidelines ¡for ¡ incorporaAng ¡PHP ¡scripts ¡into ¡HTML ¡files ¡

5

Lecture ¡2 ¡

slide-6
SLIDE 6

Intro to PHP

– When ¡a ¡PHP ¡file ¡is ¡requested, ¡the ¡PHP ¡interpreter ¡ parses ¡the ¡enAre ¡file ¡

  • Any ¡content ¡within ¡PHP ¡delimiter ¡tags ¡is ¡interpreted, ¡

and ¡the ¡output ¡subsAtuted ¡

  • Any ¡other ¡content ¡(i.e. ¡not ¡within ¡PHP ¡delimiter ¡tags) ¡is ¡

simply ¡passed ¡on ¡unchanged ¡

  • This ¡allows ¡us ¡to ¡easily ¡mix ¡PHP ¡and ¡other ¡content ¡(ex: ¡

HTML) ¡

  • See: ¡

– h^p://us3.php.net/manual/en/language.basic-­‑syntax.phptags.php ¡ ¡ – h^p://us3.php.net/manual/en/language.basic-­‑syntax.phpmode.php ¡ ¡

6

Lecture ¡2 ¡

slide-7
SLIDE 7

7 ¡

Intro ¡to ¡PHP ¡

  • Consider ¡the ¡following ¡PHP ¡file

<!DOCTYPE html> <html> <head> <title>Simple PHP Example</title> </head> <body> <?php echo "<p><h1>Output</h1>"; echo "<h2>Output</h2>"; echo "<h3>Output</h3></p>"; ?> <script language="PHP"> echo "\n<b>More PHP Output</b>\n"; echo "New line in source but not rendered"; echo "<br/>"; echo "New line rendered but not in source"; </script> </body> </html>

HTML ¡5 ¡Document ¡ Root ¡HTML ¡Tag ¡

Document Head D O C B O D Y PHP Code

Lecture ¡2 ¡

slide-8
SLIDE 8
  • Intro. to PHP
  • Now ¡consider ¡the ¡resulAng ¡HTML ¡

<!DOCTYPE html> <html> <head> <title>Simple PHP Example</title> </head> <body> <p><h1>Output</h1><h2>Output</h2><h3>Output</h3></p> <b>More PHP Output</b> New line in source but not rendered<br/> New line rendered but not in source </body> </html>

  • How ¡will ¡it ¡look ¡in ¡the ¡browser? ¡

– Look ¡at ¡it ¡in ¡the ¡browser! ¡ – See ¡ex2.php ¡

8

Lecture ¡2 ¡

slide-9
SLIDE 9

9 ¡

Intro ¡to ¡PHP ¡

– If ¡we ¡prefer ¡to ¡separate ¡a ¡PHP ¡code ¡segment ¡from ¡ the ¡rest ¡of ¡our ¡script, ¡we ¡can ¡write ¡it ¡in ¡another ¡file ¡ and ¡include ¡it ¡

  • SomeAmes ¡it ¡is ¡easier ¡if ¡we ¡"separate" ¡the ¡PHP ¡code ¡from ¡

the ¡straight ¡html ¡

– We ¡also ¡may ¡be ¡using ¡several ¡different ¡files, ¡esp. ¡if ¡we ¡are ¡using ¡ classes ¡

  • But ¡we ¡must ¡tag ¡it ¡even ¡if ¡it ¡is ¡already ¡within ¡a ¡PHP ¡tagged ¡

segment ¡

– Included ¡files ¡are ¡not ¡interpreted ¡by ¡default ¡ » Don’t ¡necessarily ¡have ¡to ¡be ¡PHP ¡ » If ¡we ¡want ¡PHP, ¡include ¡PHP ¡tags ¡within ¡the ¡included ¡file ¡ – See ¡ex3.php ¡ – See ¡h^p://us3.php.net/manual/en/funcAon.include.php ¡ ¡

Lecture ¡2 ¡

slide-10
SLIDE 10

10 ¡

Lecture ¡2: ¡Intro ¡to ¡PHP ¡

  • Simple ¡types ¡
  • See: ¡h^p://us3.php.net/manual/en/language.types.php ¡ ¡

– boolean ¡

  • TRUE ¡or ¡FALSE ¡

– integer ¡

  • Plaiorm ¡dependent ¡– ¡size ¡of ¡one ¡machine ¡word ¡

– typically ¡32 ¡or ¡64 ¡bits ¡

– float ¡

  • Double ¡precision ¡
  • We ¡could ¡call ¡it ¡a ¡double, ¡but ¡since ¡we ¡don't ¡declare ¡

variables ¡(we ¡will ¡discuss ¡shortly) ¡float ¡works ¡ ¡

Lecture ¡2 ¡

slide-11
SLIDE 11

11 ¡

Intro ¡to ¡PHP ¡

– string ¡

  • We ¡have ¡single-­‑quoted ¡and ¡double-­‑quoted ¡string ¡

literals ¡

– Double ¡quoted ¡allows ¡for ¡more ¡escape ¡sequences ¡and ¡allows ¡ variables ¡to ¡be ¡interpolated ¡into ¡the ¡string ¡ – What ¡does ¡that ¡mean? ¡ » Rather ¡than ¡outpuong ¡the ¡name ¡of ¡the ¡variable, ¡we ¡

  • utput ¡its ¡contents, ¡even ¡within ¡a ¡quote ¡

» We'll ¡see ¡an ¡example ¡once ¡we ¡define ¡variables ¡ » Note ¡that ¡this ¡is ¡NOT ¡done ¡in ¡Java ¡ » See ¡example ¡

  • Length ¡can ¡be ¡arbitrary ¡

– Grows ¡as ¡necessary ¡

Lecture ¡2 ¡

slide-12
SLIDE 12

12 ¡

Intro ¡to ¡PHP ¡

  • Easy ¡conversion ¡back ¡and ¡forth ¡between ¡strings ¡and ¡

numbers ¡

– In ¡Web ¡applicaAons ¡these ¡are ¡mixed ¡a ¡lot, ¡so ¡PHP ¡will ¡ implicitly ¡cast ¡between ¡the ¡types ¡when ¡appropriate ¡ – This ¡is ¡another ¡clear ¡difference ¡between ¡PHP ¡and ¡Java ¡ » Java ¡requires ¡explicit ¡casAng ¡ » PHP ¡allows ¡explicit ¡casAng ¡if ¡desired ¡ – See: ¡h^p://us3.php.net/manual/en/language.types.type-­‑juggling.php ¡ ¡

  • Can ¡be ¡indexed ¡– ¡the ¡preferred ¡way ¡is ¡using ¡square ¡

brackets ¡

$mystring ¡= ¡"hello"; ¡ ¡ echo ¡$mystring[1]; ¡

– Output ¡here ¡is ¡'e' ¡

Lecture ¡2 ¡

slide-13
SLIDE 13

13 ¡

Intro ¡to ¡PHP ¡

  • PHP ¡variables ¡

– All ¡PHP ¡variables ¡begin ¡with ¡the ¡$ ¡

  • Variable ¡names ¡can ¡begin ¡with ¡an ¡underscore ¡
  • Otherwise ¡rules ¡are ¡similar ¡to ¡most ¡other ¡languages ¡

– Variables ¡are ¡dynamically ¡typed ¡

  • No ¡type ¡declaraAons ¡

– Variables ¡are ¡BOUND ¡or ¡UNBOUND ¡ » Unbound ¡variables ¡have ¡the ¡value ¡NULL ¡ – Type ¡informaAon ¡for ¡a ¡variable ¡is ¡obtained ¡from ¡the ¡current ¡ bound ¡value ¡ – Compare ¡this ¡to ¡Java ¡

Lecture ¡2 ¡

slide-14
SLIDE 14

Intro ¡to ¡PHP ¡

14

  • Java ¡typing ¡discipline: ¡

Static, ¡strong, ¡safe, ¡nominaAve ¡(name), ¡manifest ¡ ¡ ¡ ¡

  • PHP ¡typing ¡discipline: ¡

Dynamic, ¡weak ¡

  • InvesAgate ¡what ¡these ¡classificaAons ¡mean! ¡

Lecture ¡2 ¡

slide-15
SLIDE 15

Intro to PHP

– ImplicaAons ¡of ¡dynamic ¡typing: ¡

  • No ¡“type ¡clash” ¡errors ¡like ¡in ¡Java ¡

int ¡x ¡= ¡3.5; ¡ ¡// ¡oh ¡no! ¡ String ¡s ¡= ¡100; ¡// ¡argh! ¡ – Instead ¡we ¡have ¡in ¡PHP ¡ $x ¡= ¡3.5; ¡ ¡// ¡no ¡problem! ¡ $s ¡= ¡100; ¡ ¡// ¡a-­‑ok! ¡

  • A ¡variable’s ¡type ¡may ¡change ¡throughout ¡program ¡execuAon ¡

$x ¡= ¡5; ¡ ¡// ¡integer ¡ $x ¡= ¡5 ¡+ ¡4 ¡ ¡ ¡ ¡ ¡// ¡integer ¡ $x ¡= ¡$x ¡+ ¡1.5; ¡// ¡float ¡ $x ¡= ¡$x ¡. ¡“ ¡dollars”; ¡// ¡string ¡

15

Lecture ¡2 ¡

slide-16
SLIDE 16

Intro to PHP

– Perhaps ¡intenAonally ¡but ¡perhaps ¡by ¡mistake ¡ – We ¡have ¡to ¡be ¡careful ¡and ¡to ¡test ¡types ¡during ¡ execuAon ¡

– ge^ype() ¡funcAon ¡returns ¡a ¡string ¡representaAon ¡of ¡variable’s ¡ type ¡ $x ¡= ¡5; ¡ ¡echo(ge^ype($x)); ¡// ¡integer ¡ $x ¡= ¡$x ¡+ ¡1.5; ¡echo ¡(ge^ype($x)); ¡// ¡float ¡ $x ¡= ¡$x ¡. ¡“ ¡dollars”; ¡echo(ge^ype($x)); ¡// ¡string ¡ – is_<type>() ¡funcAon ¡returns ¡a ¡boolean ¡to ¡test ¡for ¡a ¡given ¡<type> ¡ $x ¡= ¡5; ¡ ¡ ¡$check ¡= ¡is_int($x); ¡ ¡// ¡true ¡ ¡ ¡$check ¡= ¡is_float($x); ¡// ¡false ¡ – Can ¡use ¡these ¡tests ¡to ¡make ¡decisions ¡within ¡a ¡script ¡ – See ¡ex4.php ¡

16

Lecture ¡2 ¡

slide-17
SLIDE 17

17 ¡

Intro ¡to ¡PHP ¡

– PHP ¡programs ¡have ¡access ¡to ¡a ¡large ¡number ¡of ¡ predefined ¡variables ¡

  • These ¡variables ¡allow ¡the ¡script ¡access ¡to ¡server ¡informaAon, ¡

form ¡parameters, ¡environment ¡informaAon, ¡etc. ¡

  • Very ¡helpful ¡(as ¡we ¡will ¡see) ¡for ¡determining ¡and ¡

maintaining ¡state ¡informaAon ¡

  • Ex: ¡ ¡

– $_SERVER ¡is ¡an ¡array ¡containing ¡much ¡informaAon ¡about ¡the ¡ server ¡ – $_POST ¡is ¡an ¡array ¡containing ¡variables ¡passed ¡to ¡a ¡script ¡via ¡ HTTP ¡POST ¡ – $_COOKIE ¡is ¡an ¡array ¡containing ¡cookies ¡ – See ¡ex5.php ¡

Lecture ¡2 ¡

slide-18
SLIDE 18

18 ¡

Intro ¡to ¡PHP ¡

  • PHP ¡Expressions ¡and ¡Operators ¡

– Similar ¡to ¡those ¡in ¡C++ ¡/ ¡Java ¡/ ¡Perl ¡ – Be ¡careful ¡with ¡a ¡few ¡operators ¡

  • / ¡in ¡PHP ¡is ¡always ¡floaAng ¡point ¡division ¡

– To ¡get ¡integer ¡division, ¡we ¡must ¡cast ¡to ¡int ¡ $x ¡= ¡15; ¡ $y ¡= ¡6; ¡ echo ¡($x/$y), ¡(int) ¡($x/$y), ¡"<BR ¡/>"; ¡ » Output ¡is ¡ ¡2.5 2

  • Mixed ¡operands ¡can ¡produce ¡odd ¡results ¡

– Values ¡may ¡be ¡cast ¡before ¡comparing ¡

Lecture ¡2 ¡

slide-19
SLIDE 19

Intro ¡to ¡PHP ¡

Consider ¡comparing ¡ ¡a ¡string ¡and ¡an ¡int ¡

  • Any ¡non-­‑numeric ¡PHP ¡string ¡value ¡will ¡“equal” ¡0 ¡
  • Any ¡numeric ¡PHP ¡string ¡will ¡equal ¡the ¡number ¡it ¡

represents ¡– ¡first ¡few ¡digits ¡ ¡Consider ¡comparing ¡a ¡string ¡and ¡a ¡boolean ¡

  • Regular ¡PHP ¡string ¡value ¡will ¡“equal” ¡true ¡
  • “0” ¡string ¡will ¡equal ¡false ¡
  • Consider ¡comparing ¡an ¡int ¡and ¡boolean ¡
  • 0, ¡0.0 ¡are ¡false; ¡anything ¡else ¡is ¡true ¡
  • Consider ¡int ¡and ¡float ¡
  • Float ¡is ¡changed ¡to ¡an ¡integer ¡

Lecture ¡2 ¡ 19 ¡

slide-20
SLIDE 20

20 ¡

Intro ¡to ¡PHP ¡

  • To ¡compare ¡strings, ¡it ¡is ¡be^er ¡to ¡use ¡the ¡C-­‑

like ¡string ¡comparison ¡funcAon, ¡strcmp() ¡

  • Other ¡string ¡funcAons ¡are ¡listed ¡in ¡Sebesta ¡Table ¡9.3 ¡
  • Even ¡the ¡== ¡operator ¡has ¡odd ¡behavior ¡in ¡PHP ¡when ¡
  • perands ¡are ¡mixed ¡

Lecture ¡2 ¡

slide-21
SLIDE 21

21 ¡

Intro ¡to ¡PHP ¡

  • An ¡addiAonal ¡equality ¡operator ¡and ¡inequality ¡operator ¡

are ¡defined ¡

=== ¡returns ¡true ¡only ¡if ¡the ¡variables ¡have ¡the ¡same ¡value ¡and ¡ are ¡of ¡the ¡same ¡type ¡ » If ¡casAng ¡occurred ¡to ¡compare, ¡the ¡result ¡is ¡false ¡ !== ¡returns ¡true ¡if ¡the ¡operands ¡differ ¡in ¡value ¡or ¡in ¡type ¡

– Precedence ¡and ¡associaAvity ¡are ¡similar ¡to ¡C++/ Java ¡

  • See ¡h^p://us2.php.net/manual/en/language.operators.precedence.php ¡ ¡

Lecture ¡2 ¡

slide-22
SLIDE 22

22 ¡

Intro ¡to ¡PHP ¡

  • PHP ¡Control ¡Structures ¡

– Again, ¡these ¡are ¡similar ¡to ¡those ¡in ¡C++ ¡/ ¡Java ¡

  • if, ¡while, ¡do, ¡for, ¡switch ¡are ¡virtually ¡idenAcal ¡to ¡those ¡in ¡C+

+ ¡and ¡Java ¡

  • PHP ¡allows ¡for ¡an ¡alternaAve ¡syntax ¡to ¡designate ¡a ¡block ¡in ¡

the ¡if, ¡while, ¡for ¡and ¡switch ¡statements ¡

– Open ¡the ¡block ¡with ¡: ¡rather ¡than ¡{ ¡ – Close ¡the ¡block ¡with ¡endif, ¡endwhile, ¡endfor, ¡endswitch ¡ » for ¡(expr1; ¡expr2; ¡expr3): ¡statement ¡... ¡endfor; ¡ – Advantage ¡to ¡this ¡syntax ¡is ¡readability ¡ » Now ¡instead ¡of ¡seeing ¡a ¡number ¡of ¡close ¡braces, ¡we ¡see ¡ different ¡keywords ¡to ¡close ¡different ¡types ¡of ¡control ¡ structures ¡ ¡

Lecture ¡2 ¡

slide-23
SLIDE 23

23 ¡

Intro ¡to ¡PHP ¡

  • A ¡nice ¡feature ¡of ¡PHP ¡is ¡that ¡the ¡"control" ¡resulAng ¡

from ¡a ¡control ¡structure ¡is ¡maintained ¡even ¡when ¡ you ¡exit ¡back ¡to ¡html ¡mode ¡ – You ¡can ¡then ¡exit ¡php ¡?> ¡and ¡format ¡in ¡straight ¡ html ¡ – Thus, ¡in ¡<?php ¡you ¡can ¡branch ¡/ ¡loop ¡etc. ¡ – PHP ¡also ¡has ¡the ¡foreach ¡loop ¡

  • We ¡will ¡look ¡at ¡this ¡when ¡we ¡discuss ¡arrays ¡
  • See ¡ex6.php ¡

Lecture ¡2 ¡

slide-24
SLIDE 24

In-­‑Class ¡exercise ¡

  • Start ¡your ¡XAMPP ¡web ¡browser ¡on ¡your ¡

laptop ¡

  • Open ¡a ¡copy ¡of ¡PHP ¡handout ¡ex5.php ¡from ¡the ¡

PHP ¡handouts ¡in ¡Collab ¡under ¡resources ¡

  • Write ¡a ¡simple ¡php ¡script ¡and ¡execute ¡it ¡
  • Show ¡resulAng ¡pages ¡to ¡course ¡instructor ¡or ¡

Tas ¡

Lecture ¡2 ¡ 24 ¡