ADMTC-UCSC-University of Colombo

Basics of PHP - 1

Basics of PHP - 1
3. PHP basic syntax
IMPORTANT: PHP code is case-sensitive (like JavaScript) but HTML is not case-sensitive. 3.1 How to write PHP in HTML Begin with !"#" and end with !$. !"php ........PHP code........... "# 3.% &#ere to insert PHP scri"t in HTML! $o% can insert PHP script &asicall' an'where in HTML page. There is no li(itation o) the location. !HTML# !H*+,# !.H*+,# !B/,$# !.B/,$# !.HTML# Sometimes it is needed to place PHP code before <HTML> tag.

Basically you can insert PHP script anywhere

IMPORTANT: The )ile (%st &e saved with the e0tension o) 1."#"2 not 1.#t'(2

3.3 Basic structures  A state'ent statement)  *ariab(e +ec(aration There is no need to declare varia&les in PHP. $o% can 3%st start %sing varia&les an'where in the code. IMPORTANT: 4aria&le na(e in PHP (%st &egin with 1,2. *0a(ple) 5age 6 789  -i'"(e branc# if .condition/ 0 ... 1 semi"colon# same as a!aScript

Same as a!aScript

1

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

 Mu(ti"(e branc# if .condition1/ 0 ... 1 e(seif .condition2/ 0 ... 1 e(se 0 ... 1

IMPORTANT: $ifferent from a!aScript %&else if'(

Note: Condition can &e as )ollows. A 22 B3 A 42 B3 A  Loo" w#i(e .condition/ 0 ... 1 for .initial !roced"re) condition) !roced"re for every loo!/ 0 ... 1 B3 A $ B3 A 2 B3 A $2 B

Same as a!aScript

Same as a!aScript

 5unction function .variable 13 variable 23 -3 variable n/ 0 ... 1  6o''ents 77 one-line comment 78 - M"lti-line comment 87  O"erators ,a 2 9 : %) ,a 2 9 - %) ,a 2 9 8 %) ,a 2 9 7 %) ,a 2 :Hello : . :PHP:) .. +dd .. S%&tract .. M%ltipl' .. ,ivide .. String concatenation Same as a!aScript

$a is a !ariable

IMPORTANT: ,o not %se 1:2 )or connecting string. $o% (%st %se 1.2. 2

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

;. Basic state'ent an+ functions
 ec#o state'ent /%tp%ts te0t or varia&le onto the page. The (ost i(portant )%nction 'o% will %se in PHP; IMPORTANT: <) 'o% don=t o%tp%t an'thing then nothing will &e shown on >e& &rowser; (*0a(ple ?) !HTML# !B/,$# !"#" ec#o :!H?#Hello PHP;!.H?#:) !$ !.B/,$# !.HTML# (*0a(ple 7) !HTML#!B/,$# !"#" !ariable ,a<e 2 39) ec#o :!H?#< a( ,a<e 'ears old!.H?#\n:) !$ !.B/,$#!.HTML#  +ate./ function @et c%rrent date and ti(e as a string according to the speci)ied )or(at. 5now 6 +ate.:d.(.$ h:(:s:/9 echo :!P#A%rrent date and ti(e is ,now!.P#\n:9 )ou should output HTML tags

“ \ n' means new line

So(e o) the advanced )or(ats )or date() )inction echo date(:B 3 $ (l) C h:i:s:)9 .. See PHP help )ile )or the detail

9. Arrays
+rra' is a special varia&le that can store (%ltiple val%es. +rra' is ver' convenient in (an' wa's and 'o% (%st know how to %se arra's when 'o% access to data&ase.  Tra+itiona( array .=ust (i>e in ot#er "ro<ra''in< (an<ua<es/ 5aDEF 6 :Hello :9 5aD?F 6 :M' na(e is :9 5aD7F 6 :GASA:9 echo :!P#: 5aDEF 5aD?F 5aDHF :!.P#:9 *se s+uare brac,ets [ ] to indicate the inde- of data stored in an array

3

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

Associati?e array .t#e >ey is not a nu'ber3 but a strin</ 5aD:na(e:F 6 :GASA:9 5aD:age:F 6 I?9 5aD:o))ice:F 6 :+,MTA:9 echo :na(e: : 5aD:na(e:F :!BJ#:9 echo :age: : 5aD:age:F :!BJ#:9 echo :o))ice: : 5aD:o))ice:F :!BJ#:9  foreac# (oo"

)ou can mi- string and number for each item in an array

Special version o) 1)or2 loop that can &e %sed with arra's. $o% don=t have to speci)' inde0 in the loop. 5aD:na(e:F 6 :GASA:9 5aD:age:F 6 I?9 5aD:o))ice:F 6 :+,MTA:9 foreac# (5a as 5ke' 6# 5val%e) K echo :5ke': 5val%e!BJ# :9 L

@. Basic "ractica( exa'"(es
@.1 -i'"(e Access 6ounter This e0a(ple %ses PHP )%nctions regarding )ile access (open read write or close the )ile). $o% need to create separate te0t )ile (co%nt.t0t) that contains onl' ? character 1E2 then %pload the )ile onto the sa(e director' as the )ollowing PHP )ile. !HTML# !B/,$# !"php i) (5) 6 fo"en(:co%nt.t0t: :rM:)) K .. /pen :co%nt.t0t: )ile 5co%nt 6 f<ets(5) ?E)9 .. Jead the n%(&er )ro( the )ile 5co%nt 6 5co%nt M ?9 .. <ncre(ent the n%(&er fsee>(5) E)9 .. @o to the &eginning o) )ile f"uts(5) 5co%nt)9 .. >rite the new n%(&er fc(ose(5))9 .. Alose the )ile L "# !p#$o% are the !"#" ec#o ,count) !$-th g%est to this page!.p# !.B/,$# !.HTML#

4

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

@.% Aettin< "ara'eter ?a(ues by BRL Standard GJL can contain para(eters to a d'na(ic page &' %sing 1"2 character as )ollows.  http:..server name.!a#e name.php!variable name2val"e

$o% can %se this para(eter in PHP code to change the &ehavior. seto"tion.#t'( (Nor(al HTML page to send para(eter to PHP page) !HTML# !B/,$# !p#!a hre)6:getoption.php"option6?:#Alick here )or option ?!.a# !p#!a hre)6:getoption.php"option67:#Alick here )or option 7!.a# !.B/,$# /dd name of the !ariable and its !alue to *0L !.HTML# <eto"tion."#" (PHP page to receive the option )ro( a&ove page) !HTML# .ame of the !ariable !B/,$# !p#$o% click the option !"#" ec#o ,CADTEFo"tionFG) !$!.p# !.B/,$# !.HTML# <) 'o% want to %se (an' para(eters 'o% can separate each para(eter &' 1O2 character as )ollows.  http:..server name.!a#e name.php! variable name2val"eHvariable name2val"e $ @.3 Aettin< t#e res"onse fro' user usin< BRL "ara'eters Ia?aPHP."#" !HTML# !B/,$# !P#*nter the 'ear 'o% &orn!.P# !P#$ear:!<NPGT t'pe6:te0t: id6:'ear:#!.P# !<NPGT t'pe6:&%tton: val%e6:/P: onAlick6:get+ge()9:# !HJ# !"php i) (isset(5Q@*TD:':F)) K echo :$o%r age is : date(:$:) - 5Q@*TD:':F9 L "# !.B/,$# !SAJ<PT lang%age6:JavaScript:# )%nction get+ge() K location.hre) 6 :JavaPHP.php"'6: M 'ear.val%e9 L !.SAJ<PT# !.HTML# 5

ADMTC-UCSC-University of Colombo

Basics of PHP - 1

@.; Auestboo> usin< HTML for' $o% need to create &lank te0t )ile ((essages.t0t) then %pload the )ile onto the sa(e director' as the )ollowing PHP )ile. !HTML# !B/,$# !H?#@%est Book!.H?# !P#>rite a (essage and click &%tton!.P# !B/JM (ethod6:P/ST:# !<NPGT t'pe6Rte0tR na(e6R(essageR siSe68E# !<NPGT t'pe6Rs%&(itR val%e6RsendR# !.B/JM# !HJ# !"php .. >rite new (essage to a )ile i) (isset(5QP/STD:(essage:F)) K i) (5) 6 )open(:(essages.t0t: :a:)) K )p%ts(5) ht(lspecialchars(5QP/STD:(essage:F) . :\n:)9 )close(5))9 L htmlspecialchars() L function con!erts special characters to .. Jead e0isting (essages )ro( a )ile a safe notation for HTML display. )ou i) (5) 6 )open(:(essages.t0t: :r:)) K should use this function whene!er you display user"input strings on HTML while (5( 6 )gets(5))) K echo :!P#5(!.P#:9 L )close(5))9 L "# !.B/,$# !.HTML#

6