Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

~r: :r

. e) 5-2 S
log)' (Sc1enc erver-Side Scripting (PHP)
f echno
~tiOfl
rrorfll
5 Server~Side Scripting
~
~
cise
Fill in: the blanks · j
. 1
. - - - - scri~hng anguage.
. AMffllfWJH)
pf{J' lS---
5.1 Introduction j
. :setv
er side · th · df
language 1.e: ere 1s no nee or compilation. t@ttftl!iMJE
toPHP
~nS• pf{l' iS - -
. .
!, , erpreted
Ans-: 1111 .able starts with _ _ _ sign followed by variable name. ~Mtti/ilf,Mii)
Avari
[Chapter at a Glance 5.3 Features of
PHP J.
is a variable, which can hold more than one value at a time.
PHP f eatures : - Simple, Fas_ter, Open Source, Platform Independen t,

Ans,:$
/. Afl----
Ans, : array . can be passed to functions through _ _ __
, !Jlforination
Case Sensitive, Error reporting, Interpreted etc. __ __
_s_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Ans,,• argument
.
i,
5.4 First Sample
code of 11

_______,,
c=:= State True/False I
tJP ·s platform depende nt scripting language.
JI J. Prii 1 .
Jl !)_ ..Q AflS, : False ·
[jij@ftiilfMJ)j
PHP file extension. PHI' Case Sensitivity, J}_ $ POST is an array of variables passed via the URL parameters.
php, how to execute PHP Data Types: - 2. -
PHP variables, rules Control structures in PHp:.
PHP program, Sbing, Integer, Float, Ans,: False
for declaring If statement, if else statement.
J. AFunction is a block of statements that can be used repeatedly in a program.
how to save code Boolean, Array,
variables, different ·Loop structure in PHP, .
in specific folder NULL Comments in
variable scopes For, foreach With syntaxand
PHP, Single line and Ans.: True
in PHPwith examples
multi-line commen!-5,
examples 4, PHP cannot be embedded along with HTML tags.
Ans. : False
( "'""'\
5.5 PHP String s. GET should NEVER be used for sending sensitive information. rMt!ffiliN:M
Functions 5.6 PHP
Ans.: True
Arrays
strlen(), strrev(), substr(), strtolower(), [ · Multiple Choice Question (1 correct)
trim(), ucwords(), str_replace(), Create an array, types of array, indexed
str~word_count(), strops(), substr_count() array, multi-dimensional arrays, PHP associative I. The program file of PHP have _ _ __ extension. @MffilfMIJl
arrays with syntax and examples. (a) .asp (b) .php
(c) .js (d) .txt
Ans.: (b)
2,
Avariable declared _ _ _ a function has global scope.
(a) outside · (b) anywhere
Simple HTML form wi th php code, GET vs POST (c), inside
With examples, create database connection object,
(d) none
SQL statement and its execution, cookies and
Ans.: (a)
sessions in PHP.
• e) 5-5
. n Technology (S c1en c Server-Sid S
5.6
TPS Jnformatro . e Ctipl:i
t tem ents in PHP code, which are not visible . "g (~lfy
12. . .. .... are the s a In the ou.1:pU.t )
ecia l variable which can hold
program . Of th~ ... js a s P more than on
t k . . " fl . . • . • e value at a fun
C 0 llllllents ~ ~
Ans. : ent allows programmer o ma e decision bas ed on .
13. The. . . . .. .. . .statem one or i9·, .. artaY t res :multiple values in. single variable.
so
conditions. 1\\0 ~
1 ;,11,· Afl ·····. •· D
l'li1"-IMII--
W&IIW¼
Ans. : if ent allows programmer to make decision based 30· . aJ.'l'aY p=P {unction is used to create an arr .
14. The·· ·····:·.statem
on I ther this

..,,. · •. • • r-,. ay variable
that condition. or ,., flle .. •· .
Ans. : if else 3t• : artaYO . ..types of arrays in PHP.
15. . ... . .. . . are u sed. t~ execute the same block of code repeatedly as lon µi•· fllere are . . . . . . .
condition is satisfied. g as cert .
a1n 3Z, e
Ans.: loop ,. : -rttre .. .arrays are the arrays with numeric index.
16. . .. ... _. loop w orks only on arrays. t,JI r1:te · ·•·· · l%1iWD
Ans. : foreach ~ 33, Jlldex:ed .
l 7. . .. .. ... functions returns the length of a stnn g. t,JI•· : .arrays are the arrays with named keys.
em;~
_,_
.
'[he · · · · · · · · MUffilfMiJl
Ans. : strlenO 34· odative
. .. ... . . functions counts the number-of words in a string. h ..
18.
, ASS
J\11•· • .arrays are t e arrays contauung one or more arrays . ""
Ans. : str_word_countO '[he . . • •· · · · · 1Wffli!:!•?n1
19. . .. .. .. . function reverses a string. 35. . rvtulti-dimensiona l .
5
Ans. : strrev() J\!1 • •'[he . .... ... Junction is used to return the length of an array.
20. . ...... . function searches for a specific text within a string. 36. ·
Ans. : strposO
21. . .. .. .. . function replaces some characters with some oth er ch aracters in a string.
Ans. : str_replace()
cJ\11•• : countO
· 5.7 PHP User Defined Functions j
22. . .. ... . . function returns a part of a string. [M ffit1011,;1a A is a block of statements that can be used repeatedly in a progra"" . _.._,""'
m,,.
tl&!ffll~ 1►f)!
Ans. : substrO 37· · · •.. · • ·
23. . .. .. .. . function converts a string to lowercase. tf~fflfili1•►JD Ans, : function
Ans. : strtolower()
· 38. Information can be passed to functions through ........ .
24. . ... .... function counts the number of times a substring occurs in a string.
Ans. : substr_count() Ans. : Arguments
25. . ----
. ...... . .converts the first character of each word in a string to upperca,.s.e,. 39. . .... ... .are ·specified after the function name, inside the parentheses.
. AMM411MJD
Ans. : ucwords() i\fls. : Arguments
26. . .. ... .functions removes whitespace and other predefined ch aracters from both sides 4o. The -... . .. . statement is used to let a function return a value.
r1@t1f!llfNJB)
of a string. Ans. : return
J
.,.•
Ans. : trim()
J [ 5.8
PHP Fonn Handling
5.5 PHP Strin g Functi ons
41.
27. A .... . ... is series of ch aracters. ~MMUtWJII Ans. :The
HTTPformd . sent with the ········.rnethod.
PO ata is
Ans. : string
42. . ST
28. A .. .. .. . . i s a block of statements that can be used repeatedly in a program. 1.Jll -•.•.•. .is an anay of va<iables passed,;, the URL p•"'"-
AnS; , ..$_GET
A ns.: f unction
. . ;
~,f.'l'ToljJ{
Server-Side .
5-7 Techn ology (Science)
TPS Informa ti on Technology (Science) Scnpt'1 tiOJl
or!l1a .8
~ ng (P~P) ~/ .
the HTTP post method.
_ ....... . js an array of variables passed via 11" er-side scripting is more · J.
43 I serV Seel.Ire th
. . . ~~-,,.5
Ans. : $_pOST .... ... . meth od 1s visible to ev eryone. j. • rrue
anClient-s·
from
t'on sent . a form with Id eSc~ Ptin 'd i Sc,,Ph
~~5• . BP is easy to learn and runs f . .
44. Infonna 1 e fic1ent1y g. ng ll'HP1
f ~
Ans. : GET itive data . ~ . on thec1·
. meth od may be used for sending non sens ,. . false ~
45_ ....... . 1 A~5• •friP is an interpreted language.
'ent•side.
Ans. : GET
meth od is invis ible toe . ~1
Infor mation sent from a form with ... ..... veryone. 6, .rrue
46. A~5' · ;117 runs on only on WindowS. ~
f~
Ans. : POST
of informati on to send . 1· .false ~
47. ..... ...method has no limits on the amount 5 --
~ · •pBP runs only on windows

--
Ans. : POST server.
1

to conn ect PHP code with any database


48 _ ........ .object helps s. .false --- --·
~s-. - - - - - - - - -
Ans. : PDOO
used to check whe ther variable has value
or not. _______ S._2-=-Se.rver Sid S .
49 _ .·... .... method in PHP is .d . e cnpting
. .
Ans. : issetO uter.~ The serve r s1 e environme nt that runs ascnptin gIan
that the server sends on the user's comp
50_ A ..... :•·js a small text file ~
!,
A 5 , f rue
guage~fenneij Web se
Ans. : Cookie ,,n · · n•er.
activ ities created h BP hides the code from the user .
user or its machine and track JO, P
51. .... .. .. .is used to identify
On t e us
er .
computer. f.OS-: True
Python is a server side programmingIanguage.
Ans. : Cookie l1•
52. When brow ser requests server
page .. ...... .are sent along with the re.g__u est. Ans.: True
~ ,~---
· ~w~sw--,~~
Ans. : cookie
.
C- --- --~ :-- ~- -s_.3_F_ea_,:tu.::re::.s
:::of.:.,:PH::P_ _ _ _ _ ]J
serve r to track user activities. ~
53. ........ js used to store user infor mation on 12. PHP is very difficult to use. ~
Ans. : sessi on Ans. : False
. .. ... .. help s web application to main tain
user informa tion on all the pages .
13. There is no need of comp ilation for PHP code.
m!lB
54. ·,-~...it~-~
.,,,1►~u•.m
Ans.: True
PHP is faster than other scripting language.

>
Ans. : Sessi on 14.
Ans. : True
I True or False 15. You need to pay for use of PHP.
Ans.: False

5.1 Introduction to PHP


] 16. PHP is platform independent.
.
Ans.: True . 0f variable dedaranon.
. 1angu age. M@
. tmg iffl
···-- -·· 17, PHP is case sensitive only at the hme
1purp ose scnp
1. PHP is widely used open source genera ~:True
·
Ans. : True tm@llll lS. PHP is interpreted langu age.
2. PHP does not supp orts data bases. Ans. : True
insensitive.
Ans. : False MtMIWII 19
· In PHP, the echo keyword is case
3. PHP runs on clien t side. Ans. : False
Ans. : False
c-\.ns. : l' . _ q1-> code -Fhst Sa
2 1. f> l"tte can a ls o b e lllple code of h
.i.-1--{r, HP)
(P _
lip t· ernbectd 5- 10 Serve r-Sid
___ ___ __ _:_ting
e Sc rip __
,n11,11ivn lt:L - = ' - - - - - - - - - -___
, .. , -- t, no logy (Scie n ce)
-Ans. : Fa.I Iles are save ect With I--n.tviL ta 1 11 [~j
- c gs Ustng , ) rr:' ' ' ~ ion returns a string.
22. Se d With -htn-u ~l•l'ooO tu n
exten s ion Pl--!p a
e cho k nc1 ', racters in a
Ans . l' e Yworct is ct ·
- . f, tse l~CC () function rep laces some characters with some other cha
. . ru.e Usect to ::j;r_rcp
23 ispiay t jS, string.
. Var· b les ex t on Web Page
A
-'""'-f\s • l'
Ia a r e tise ct t . Mfijf§(f,ju,jaj
_ •frtle . ,ction returns a part of a sh"ing.
,vt'•'subst rO lt ll
. . rue o s tore Va lt1es
0~-~
2
A.v . .
f~ () fu nc hon ~~-
~ -~lO-~05.: strtower
4. [l~filvli!'lim)
a r 1ab}e starts . . to l ower case.
· converts a stnng
A
ns. : l'rue With $ sign, follow ed b ·
25 . A. Variab le na · v. ·
Y name of anabJe_ · ,1
AOS·: false_ unt() func tion counts the number of times a subs tring occurs
in a string.
~ ;1. subst1_CO
Ans. : False
26
m.e can starts With a numb
e~ ~
~ ~ns- : r rue d () func tion: converts the firs t characte
rs of each w ord in a to
tol:1@:!1'-Jw
;a
s. .
lJ. ucwor
· A. Variab le
name can co ntain only alpha-n ume .
~ uppercase
rs fr om b oth sides of
A ns. : l'rue
nc characters anct unct i\JlS-: fru~) fLU,ction adds white sp ace and other pre defined characte ~
l3. trun
27. A variab le d e clare d outside a .
~I
~ •
a string.
false
5
A ns. : True functio n has a GLOBA L SCOPE i\J1 .. HP supports NULL data type .
- 44. p
28 · f . Ans . frue
. A variab le declar ed w ithin _ · · PHP does not support null data type. [®®NiM¥u
Ans. : True - -- a unction has a LOCAL SCOPE. 45
Ans, : False
29. Variab les can store data of differe nt types. 16_ PHP supports
none da ta types. lWtttffiihNI
Ans. : True · · Ans.: False
3 47_ .ph is valid php file extension . [l@M M•U►itl
To check data type of variabl e var_ put () method is used.
o. Ans. : False
A ns. : False 48. PHP variable has three differen t scopes namely local, global and static.
in PHP code. p;c1·~,.. 1!' ,~ i
m-·1-1$~
31. Comm ents are the non execut able stateme nt
Ans. : False
A ns.: True
49. gedit is example of text editor.
or more conditions.
32. If statem ent allows to make decisio n based on one Ans.: True
A ns. : True 50. PHP supports NULL data typ e. ti® ®tf4 1fJtl
33. F or each l o op w orks only on a r rays . Ans.: True

Ans .: True [ 5.5 PHP String Functio ns


string.
34. StrLen gth() functi~ n returns the length of a
. . .51. A string is a series of characte rs. [l~MHf•U►f1i
A ns. : False Ans. :True
tr_ word_ c dun t () functio n co unts the numbe r of words ma stnng. 51 used repeated ly in a program .
35. S · A function is a block of statements that ca~ be
Ans.: True
A ns.: True
S trrev () f unctio n reverse s a s tring.
53
· In PHP, string() returns the BOOLE AN value.
M@fi.t:MJt
36. Ans.: False
A n s. : True
, 5-11
TPS Inform ation Technology (Science)

5.6 PHP Arra ys

54. Arra y can store multiple values at a time.


Ans. : True
.
55. In PHP, array () function is used to create an array
Ans. : True
es).
56. Syntax of creating array variable is a= arr (valu
Ans. : False
57. In PHP there are six type s of array s.
Ans. : False
s.
58. An array with a num eric index is called inde xed array
Ans. : True
.
59. Multi-dimensional arrays are the arrays with nam ed keys
Ans. : False
60. Associative array contains one or more array
s.

Ans. : False
61. Arra y inde x alwa ys start s with zero .
Ans. : True
array.
62. Thel en() function is used to retur n the leng th of an
Ans. : false
nsional arrays . .
63. PHP can hand le mult iple levels of mult i dime
Ans. : True
64. We can create our own functions in PHP .
Ans. : True

5.7 PHP User Def ined Functions

.65. A function is a block of state men ts that can


be used repeatedly in a program.
Ans. : True
66. A function nam e cann ot start with a num ber.
&UfflfWI
Ans. : True
67. Info rmat ion can be pass ed to functions thro
ugh meth ods.
Ans. : False
e, insid e the par ent hes e~
68. Argu men ts ar~ specified after the func tion nam

Ans. : True
a function.
69. Retu rn statemen t is used to retur n a valu e of
Ans. : True
TP S Inf orm a tio n Te
d m o log y (S cie nc e) 5-1 2
Se rver-S ide Sc rip tin g
[ 5. 8 PH P Fo rm H an dl in g
(PH P)

70. The fo rm data is sent wi


th the HTTP Po st m et ho d.
;\n s.: Tr ue
71 . The PH P superglobals $_
GET an d $_POST are us
ed to collect form data.
;\ns. : True
. (March 2022, 23)
72 _ $_GET an d $_POST syperglobals w hi
Ans. : False ch m ea ns th at they are ne
_ _
:ve r accessible .
73 . $_ GE T is an ar ra y of variables pa ss ed . .
Ans.: True vi a th e UR L pa ra m et er s.
74. $_POST is an ar ra y of va ria bl es pa ss ed
}\.ns.: True vi a th e HTTP POST m et
·. ho d.
(Mar~h 2022)
75_ Information se nt fro m a fo rm w ith GE
T m et ho d is no t visible
to everyone.
}\.ns.: False ·
(M
ar ch 2022, 23)
76. GET ha s lim its on th e am ou nt of in fo rm ·
f\.IlS. : True at io n to se nd .
. . (M ar ch 2022)
77. GET sh ou ld NEVER be us ed for -
_ se nd in g pa ss w or ds or ot
_ he r sensitiv~ in fo rm t·
a 10 n.
Ans.: True
(Mai;ch 2023)
78. Information se nt fr om a fo rm w
ith PO ST m et ho d is invi
sible to everyone .
Ans.: True
(M ar ch 2022)
79, POST ha s lim its on th e am ou nt of
in fo rm at io n to se nd .
Ans.: False
(M ar ch -2022)
80. isset() m et ho d in PH
P is us ed to ch ec k w he
th er va ria bl e ha s va lu e
or not.
Ans.: True (M ar ch 2022)
81. A cookie is a sm al l te
xt file th e cl ie nt se nd s
Ans.: False on th e us er's co m pu te r.
(M ar ch 2023)
82. When br ow se r re qu es
ts se rv er pa ge , co ok ie
Ans. :True s ar e se nt al on g w ith th
e request.
Sl Cookies sto re vi si te d
pa ge on br ow se r to op
Ans.: True tim iz e se ar ch . (M ar ch 2022)
M. Session is us ed to st or e
us er in fo rm at io n on se
.lins.:True rv er to tra ck us er activiti
es .
·
S~. Session he lp s w eb ap
pl ic at io n to m ai nt ai n
Ans.: True us er in fo rm at io n on all th
e pa ge s.
~. PHP Data O bj ec t he lp s us to co
nn ec t PH P co de in U
\ multiple da ta ba se s. ni fo rm M et ho d of acce
ss to
,ns,: True (March 2022)
- rrsJntorniat1
· d oJocr)'
. 1Te 1n o
01
ne Corred Answers)
(Sc
.
5-13
..-ie_~nc~e): . . - - - = = - - - - - - - - Server-sr·deS
CriPtin
8(~~~J
MCQ (O . . == ~= -,--- ,~=

~ ~; -Introduction to PHP~~
[__ •dely used open :Ource server s:e. ;roo-ra
o
nun·-
mg langua
·
.......... is a w1 ge ivh·
1. ... various platforms. Ich
runs on .
(a) Word (b) PHP (c) Excel (d) PowerPoint

Ans.: (b)
5.2 Server _Side Scripting

•s a Computer system that serves as a central reposito


2. A ......... I . . . . ry of data a
. · nd
programs.
(a) Server (b) Network (c) Client (d) Object

Ans. : (a)
3.
The server side environment that runs a scripting language is termed ...... .
(a) Browser (b) Computer (c) Web server (d) Client

Ans.: (c)
·5.3 Features of PHP J
L-----------
[
4. ..........is a script executed on server which generate dynamic HTML pages.
(a) PHP (b) MS Word (c) MSExcel (d) Client
Ans. : (a)

5. . The PHP code can also be embedded with HTML tags using ..........
(a) <!Php! > (b) <?php and?>
(c) <=php > . (d) <#php#>
Ans.: (b)
6. PHP files are sav:ed with .......... extension. -
(a) .html (b) .xml (c) .js (d) .php
Ans.: (d)
7. ········.keyword is used to display text on web page.
(a) echo (b) print · (c) write (d) cin
Ans. : (a)
8 .. ·· ... .is a symbol or name that stands for a value.
·
(a) Method (b) condition (c) Variable (d) Value
Ans. : (c)
~ .
TPS Tnformc1tio n Tech nolo gy .
(Scie n ce·) 5-14 Serv er-S ide Scri pti (PH P)
ng
9. A vari able in PH P star ts wit h th
e ··· ·· ···.sign foll owe d by nam e of the
vari able .
(a) # (b) @ (c) = (d) $ ~f i$ Wffl1
A~ =~ 1
.
1
o. PH P var iabl e nan ,e cam ,ot star ts wit h a · ·· ·· · ·· •
(a) Nu mb e r
_(b) Cha racte r
(c) Alp han um eric
(d) Dol lar
;\JlS , ; (a) . . . : .
Cor rect met hod for dec lari ng PH P
11. var iab le 1s . .. .•... .
(a) # age (b) $ age (c) @ age (d) age Mffl&JJJ
j\JlS ·: (b) . .
_ _ A var iabl e dec lara tion .. ._.~ ·-· .a fun -4
12
(a) insi de (b) mid dle
ctio n has a_ GL OB AL SCOPE. ttiM®iQWJJ
(c) out side (d) top side
j\JlS, ; (c) . . .
_ A var iabl e dec lare d ... ..... .a fun ctio
13 n has a _LO CA L SCO PE
(a) Ins ide (b) out sid e (c) bet wee n (d) wit hin
J\JlS,: (d)

14_ pHP stor es all .. .. ., ...var iab les in an arra y call ed $ GL OB AL [Ind ex]
(a) loca l (b) glo bal
tf ff f~
(c) Sta tic (d) wit hin
Ans. : (b)
1s. To check dat a typ e of var iab le in PH P ..... ... me tho d is use d.
(a) var _du mp () (b) var _de l()
(c) var _re mo ve( ) (d). var _de lete ()
Ans, : (a)
16. ....... .. are the sta tem ent s in PH P cod
e wh ich are not visi ble in the out put
pro gra m. of the
(a) Me tho ds (b) com me nts ·(c) Obj ects (d) val ues
Ans.: (b)
17. How to add a sin gle line com me nt in
PH P?
(a) II (b) * 1r (c) $ (d) @
Ans.: (a)
18, How to a mu ltil ine com me nt in.PH
P ?
(a) II (b) # .(c) $ (d) I* and * I
Ans.: (d)
19,
········· stat em ent allo ws pro gra mm er to ma ke decisio
n, bas ed on one or more
conditions.
(a) if ~
(b) loo p .. (c) variable (d) me tho d
Ans.: (a)
~ s ·ence)
. n Techn ology ( c1
-rnc1 nfor111vtion ~
5-15
----------
Server-s·d
- " esCriPti~
.
mmer to make decisions based on . either th·
&(1>11
1I fl"
1nforfl'la
.
ttOil
Techno logy (Science)

function counts the number of times


5.

sub tr·
16
Server-Side Scripting (PHP)

nt. "
-]J
a nouwr<s pro
n gg1ra s tng occurs in a strin
~ . ~ s Ot I~ ...... . . subs tr_count() g.
{b)
·······stateme ' . t i1 3o. (a) substr0
20. decision. (b) if else (d) strlen()
(a) loop (d) method
tri1n0 lfflbliWJll
(c) .
(c) variable character of each word .
All'' (I,) _function converts the first m a strmg to upper case ·
as
t the same block of code repea tedly 1ong as a ..... ... (b) strtolower ()
Ans.: (b)
.are used to execu e ~~ 31. (a) subs tr().
2l. ·····di tion is satisfied. ·
{d) loops ~ (d) ucwords() tmf&!MiU
(b) Method (c) Object (c) trirnO
con
(a) Variable ·
· hlt other predefin d h
-~
,>\/Is" function removes w espace and e c aracters from both sides
Ans.: (d)
········ loop works only on arrays.
3Z, ..... ...st . IMIIWII
.
(b) for (c) foreach (d) forone of a nng. (c) straps() (d) strrev()
22. (a) for next (a) trun 0 (b) strlen()

Ans. : (c)
23.
(a) ····strrev
Ans. : (b)
() (b) strlen
· h f t· g
function returns the lengt o a s rm
() (c)
.
strlength()

d .
s ma strm g.
.
{d) substr() ~ c
f./lS•: (a)

33. A ......• . is serie


5.5 PHP String Functions
s of charac.ters.
(d) Value
1r

l
1
\Jrnrr1•r,u
J
24. ..... function counts the number of wor (a) String (b) Number {c) Object
(b) str_ word_count()
(a) strlen() Ans.: (a)
a program.
(d) trim() ments that can be used repeatedly in
(c) substr() 34. A ···:···· is a block of state
(c) method (d) varia ble
Ans. : (b) (a) function (b) class
111 ! ~
1il !'~ffl1t~~+ll!'i~
25. ........ func tion reverses a string. lb&liMq ~A~,"Hff•.

(a) strrev() (b) trim() (c) UCwords() (d) strop s{) Ans.: (a)
Ans.: (a)
ific text with in a string.
98 [ 5.6 PHP Arrays
26. . ....... func tion searc hes for a spec
{d) str_replace() more than one value at a time. tbitffii1•h\tJI
(a) substr() (b) strrev() (c) strp os() 35. . ...... . is a variable whlch can hold
(c) Object (d) Constant
Ans.: (c) (a) Array (b) Method
in a string.
. .. ..... function replaces some char
acters with same othe r character Ans.: (a)
27.
(a) strrev() {b) str _rep lace() In PHP .. ...... function is used to
create array.
36.
(c) trim() (d) strpos() {b) array() (c) value() (d) variable()
(a) arr()
Ans.: (b) Ans.: (b)
........ ftmction returns a part of a strin
g.
!MtffllfMD ........• ti&tiffitf11hHl .
28. 37. Syntax of creating array variable is
(a) substr() {b) trim() (c) strrev() (d) str_len() (b) #a= arr()
(a) @a= array()
Ans.: (a) (d) $a= array (values)
(c) = array()
29.
·······•iunction conv erts a string to lowercase . !MrffilfWII Ans.: (d)
tlti !ffihfJ❖JJl
rd (b) strtolower() 38. PHP.
(a) UC wo s() There are ..... ... .types of arrays in (d) Four
(c) strlow er() (d) trim() (c) Three
(a) Two (b) One
Ans. : (b)
Ans.: (c)
chnology (Science) 5.18
·
-1_7 _ _ _ _ _ _ _
___s_ se_rver-Sictes . 3 ·on Te - - -- - -- --..:_ _ Server-Side Scriptin g (PHP)
criptin 0 .
Technology (Science) - 1~1 r)11 tl
.
ation sent from a form with ........ method is ..

~
'th a numeric index. -rr5 v1s1ble to everyone.
-rrs 1nforn1atwn 1r1forJll ,,., . (b) GET (
tl1e arrays w1 c) HDp (D)
.arrays are ' (b) Indexed s pOSi Ffp !II
,~lf,m Jll'"'~••to.=~
. IF~
39, 4' (a)
(a) Associa tive . (d) Normal . 'bl
: (bl rna twn . sent from a form with .. ...... meth od 1.s m ·
ulti-din1ens10n al i\ns• ( v1s1 e to everyc i',2.
(c) M [nfor (b) GET (d) ,.,.,,,.,
49, (a) pOST c) HDP ru-
Ans. : (b) the arrays with named keys. .
_ .. .arrays are (b) Associative
40
AIIS·: (a)···· · ,NEVER be used for sending sensitiv
e information.
(a) Indexed (d) Normal
(b) HTTP (c) GET (d) FTP 1Htitfi•lfl4l
(c) Dinlensional 50. ... pOST
(a)
Ans. : (bl taining one or more arrays . · HP. d 1
ays are arrays con . Af\S•
: (cl
.method m p IS use to check whether variable has va ue or not.
(b) Indexe d .. .. ..
41. .. .. .arr
(a) Multi-dinlensional 51. i~) outset() (b) inset() (c) noset() (d) isset() (fflfffilfWfAj
(d) Numb ered
(c) Associative 11 fil th h
"
•115•: (d)
A ....... . is a sma text e at t e server sends on the user's computer.
Ans. : (a) ·1
. dex a[ways starts wit 1 ...... ·· · 52 (b) session
Array m · (a) · Cookie
42 four
·
(a) one
(b) two (c) zero (d)
(c) Presentation (d) Me th od (1Mt/ilfWf1j
Ans.: (c)
:J J\f\S,: (a)
.. .are sent along when browser requests server pages. {l~fflUf1 1fJU

-
L[_ _ _ _ _ _.::.5::.: _P
.7__:P~H _ ef_i_n_ed_ F_u_n_ct_io_n_s__ __ _
_ U_s_er_D 53,
(c) Method (d) Server
(a) Cookie (b) Session
in a program.
43. A ....... .is a block of statements that can be repeatedly Ans.: (a)
s.
(a) function (b) condition (c) object (d) value
54, is used to store information on server to track user activitie
(b) session (c) Presentation (d) Object
Ans. : (a) (a) Cookie
44. Information can be passed to functions throug h ... .....
. ltff iq Ans.: (b)
ation on all the pages.
(a) Condition (b) arguments (c) array
(d) . variable
55. ........ helps w eb application to maintain user inform
(a) Session (b) presen tation
Ans.: (b)
. t
(d) obiec fl&ftf#lt1 1fftj
····-
(c) Cookie
5.8· PHP Form Handling J Ans.: (a)
45. The form data is sent with the ........ metho d.
(a) HTTP POST (b) FTP POST \ MCQ (Two Correct Answers)> -
(c) STP POST (d) SMPT POST
[ 5.1 Introduction to PHP
Ans. : (a)

46. . .. .. .. . .is an array of variables passed via the URL


param eters. tMtffltfMU 1. PHP runs on following platform. (d) Windows
(a) $_GET (b) #GET (c) ._GET (d) @_GET
(a) Linux (b) Excel (c) Wo rd
Ans. : (a)
Ans. : (a), (d)
47
.. .. .... .is an array of variab les passed via the HTTP
POST metho d . 2- PHP is compatible with following servers.
· (d) Apache
(b) $]OST (c) @]OS T (d) #_FOST (a) Unix (b) XAMMP (c) Linux
(a) $POST
Ans. : (b)
Ans. : (b), (d)
]1J1ology (Science) 5.20
~tion rec ___::.: .------- Se . . .
5-19 Server-Sid S l~forf11 . . rver-S1de Scnptmg (PHP)
) '...-- e Cr1ptj
. e
echnology (Scieinn,c,~ ----------
ng (P tl,s . ble starts with the ..... ... sign followed b
' 1nfor111,1t1on T ~ ~P) A1,aria (b) # () Y.........of the variable
Tl 5 d to execute PHP program. c name (d) ·
can be use o $ value
Which servers ) HTML (c) NGINX 1 l · (a)
3· MPP (b .
al, (cl 1 d f ·
(a) XA NET (e) Python oS·: (
riable dee are ......... a unction has GLOBAL
(d) pHP- A A va function has LOCAL SCOPE. SCOPE and a variable declared
.... .a (b) .d 11:
Ans. : (a), (c) 11· outs1 e (c) within (d) \lljJJjl:Jflrl}l
,.•· . side
5.2 Server Side Scripting (a) J1l between
(b) (cl
. languages for server side programming are ...... _.. 10S· :
fo u' owJ.11.
' g are the data types used in PHP ... ... ...
4
·
Progranurung
(b) Tally (c) Excel (d) Python
I'

1z, (a) Boolean (b) Text (c) Number


(iMffUiWJE
(d) Array
(a) PHP
. (a), (d) .
Ans. : (a), (d)
~o5· · Control structures m P~P are .. .... ...
5.3 Features of PHP Mtffili11f}J1
[ 13· (a) for ·
(c) for each
(b) 1f statement
(d) if else statement
s. Features of PHP are ........ ·
(b) Open source ~os : (b), (d)
(a) Interpreted
(d) Platform dependent · Loop structure in PHP are ......... (!MffilfWii!M
(c) Difficult 14 (b) for each . (c) if
· (a) for (d) if else
Ans.: (a), (b)
6. Advantages of PHP are _ _ and ___ . AJlS,: (a), (b)

(a) platform independent 15


_ comments in PHP is possible in following ways. llMfflliWIJ!I
(a)/ / (b) \\ (c) /*and*/ (d) / #and #/
(b) difficult to learn
(c) does not support to database Ans, : (a), (c)
(d) easy to learn 16. Comments are of ........ .and ..... ....type.
(e) paid software (a) Single line (b) No line (c) Two line (d) Multi-line
Ans. : (a), (d)
Ans. : (a), (d)
7, Which are invalid statement for PHP? Following are the pre defined functions in PHP to manipulate string.
(a) Simple
l®IIWI 17.
(b) Closed source language (a) trim() (b) substr() (c) length() (D) strreverse()
(c) Loosely typed (d) Platform independent ~rM"n--,l'!'~l!'l!•t.,!'lu~1
(e) slower
Ans. : (b), (e) Ans. : (a), (b)
18. Which are the superglobal variables in pHp ? /IMffUfWMI
5.4 First Sample code of PHP (a) $_POST (b) $_GLOBAL (c) $_SUPER
8. Popular PHP Frameworks are (d) $_TOP (e) $_GET
(a) Echo Ans.: (a), (e)
(b) Laravel
(c) ServerSide 19, Which ate correct variable names in php programs ?
(d) Symfony
Ans.: (b), (d)
(a) $empname (b) $emp-narne (c) $emp-num
9,
Variables are used for storing v 1 (d) $get (e) $else
(a) Methods a ues such as .. ...... .and .... .... . Ans.: (a), (e)
(b) Numeric values 20,
tltfflil•►I
(c) Character Strings Which are not correct variable names in PHP program?
Ans.: (b), (c) (d) Functions
(a) $Sname (b) $S-name (c) $S_name
An (d) $lname . (e) $Snamel
s.: (b), (d)
_o_g_y_(S_ci_en_c_e_)__ _ 5_22

-------==::
5-21
ology (Science)
d
e1n
I
5.2 Server Side Scriptinge
User DefinE!d Fun ctio ns
5_7 PHP p
~
--m ing languages for server side rograllUni.ng
rau ,,, - :e
(b) Windows (c) Linux
. me can start with a .···-·-· .or .tt·-· -·-·. progpBP
( ) Le er . ( )
Afuncl:lon Ina {d) Underscore I (f) Java and JSP

-
c

-
(b) Number .

--
(a)

~~--
21.

~~
l]I11X

-==
(a) symb o (e )

(d)~ -- --5.8---=:-:---::-=-:::~- -- - ) (d),


(f)
Ans.: (cl,

22
Form data can be su
· (a) Return
PHP Form Handling

bmitted by .. -·-·-· ·or .. -·· ·-·method.


(b) forward (c) GET {d) POST
k
i.
. (a, J
featuDr~fficult
of PHP a,e .........~:3. . Features of Pl!P
(b) Simple
. .
()
c Error Creating
11/mlfHfUJ
(a) 1
(e) Error Reporting
Ans · (c), (d) (d) Platform Inde pend ent
. are used to collect form data.
23
.. The PHP super globals ...... .. .and ....... (D Platform Dep ende nt
(b) $POST (c) $GET {d) $_POST
· (a) $_GET
:- -- ---- ..J
. (b) (d), (e)
Ans. : (a), (d)

24. .. ... .. ..
is an array of varia bles passedthvia URL
variables passed via HTTP POST me od.
(a) $GET (b) $_GET (c) $_POST
parameters and .. .. .... ·1·s an arra

{d) . $POST
~ c:
AoS, • ;-;::t:=
'-- -- -; 5.4 :-- :-~
Firs Sample code of

s. Variables are used for storing values


such
Pl!P
as ............., .. ............, and
(b) Numeric (c) Objects (d) Methods
(~) Character
Ans. : (b), (c)
s whereas ........ J)as no lirn·ts (f) Memory addresses
ti'!ffiblfMH
25 . .. ...... .has limits on the amount of information to send 1 (e) Functions
on the amount of information to send .
Ans.: (a), (b), (f)
(a) Value (b) Method {c) GET (d) POST .. ........... (iMffilfNfiiE
6. The scope of variable in PHP are ..
Ans. : (c), (d) (c) National (d) Global
(a) Local (b) Regional
_ are used to collect form data .
26. The PHP superglobals __ _ and __ (e) Static (f) International
(a) $--GET {b) $-PET (c) $-POST
Ans.: (a), (d), (e)
(d) $-CET (e) $-NET
Ans. : (a), (c) 7. PHP data types are .... ..... .... .. (iM/ffilfMil!U
(a) Text (b) String (c) Number (d) Integer
(f) Boolean
MCQ (Three Correct Answers) · (e) Empty
Ans.: (b), (d), (£)
[ 5.1 Introduction to PHP 7
8.
Following are the predefined functions
in PHP to manipulate string
(d) strlen()
- (b) strops() (c) substr()
.. (a) Lower() i~~~,u~@mi
..i
t~.~ftt@
1. PHP runs on following platforms ...... ... [W/ffl/tft❖Jilll () (f) strlength()
(b) Wor ds (c) Linu x e upper()
(a) Systems (d) Excel Ans. : (b), (c), (d)
(e) Unix (f) Windows 9. - - ,- - - - -·
@ttfUIW.fJI
Rules for declaring php variables are
!
Ans. : (c), (e), (f)
2.
PHP is compatibl .
servers . (a) A variable name start with $ sign
(a) XAMMp . wing
e with follo
(b) A variable name can start with
a number
(d) Uni {b) Lmux (c) Apache
(c) Variable names are case sens itive
x (e) Lighttpd (f) Word
Ans. : (a), (c), (e)
(d) A variable same start with* sign
5-23 tion Technology.(Science) _
5 24
(Science)
ation Techno1ogy ,sl~for[lla
Trs 1nforrn t sta rt with number 11 , s request is fulfilled by runn . Server-Side S . . .
. bl name canno A user . . mg a scri t d. cnptmg (PHP)
(e) A vana e ' ·e no case sensitivity . sed to provide interactive web . p irectly on thew b
Variable names a1 Jt 1s u sites. e server.
(f) .
pro grarruning language for server side progralIIInin
____________
Ans- : (a), (c), (e) . PHP can be given using ___, - - - and - · g are PBP p
10. Comments ll1 / / (d) $ (e) /* (f) " 53 F , ythonJSP
(a) # (b) • (c) -------·-.:..:::ea:':tuu.rres of P}lp .
Ans. : (a), (c), (e) . . ble names are as follows :
Explain features of PHP (any 5).
ll- In PHP valid vana (b) $roll number J. . p}!P is most popular and frequent!
(a) $rolln um .
ber (d) $roll-number ~n5• • following are features of PHP . Y used Worldwide .
guage- . server side . .
(c) $roll_number (e) $rollnumberl 1an 5·JI1.ple; It is very simple and easy to u scripting
(e) $roll*number n1 l se, as compared t0
Jnterpreted : It is an interpreted languag . other scripting Ian
Ans. : (a), (c), (e) (iil . e, Le, there is no guages.
F ster : It 1s faster than other scripting 1 need for compilatio

~
(iiil a anguage e.g. JSP and n.
•.,) open Source : Open source means you need ASP.
mFHP ~ re, types of ,rrays ~;: :Hp ~:,,ays · , ------.........__ (1' download and use.
·
not pay for use of PHP y
· ou can freely
12.
(a) inside (b) multiple t
Platform Independent : PHP code will be run
(v) OS X, Windows. on every platform, Linux, Unix, Mac
(d) associate
(c) indexed
(e) outside
(f) multi-dimensional c------:--:--~s:.4~;F1:.r:st~s:am=p~le:--c~o:de-o-:f:-:-P-H_P_ _ _ _ _ _ _ J
Ans. : (c), (d), (f)

[ Answer in Briefly >- 4_


Ans.:
Explain how to save and execute PHP program.

(i) Type the PHP code using any text editor (Notepad).

I 5.1 Introduction to PHP J (ii) Save with .php extension in appropriate folder.
.
L -- - - - - - - - - - -

(iii) Go to browser and type http:/ / localhost/ php/ first.php


1. Explain PHP.
s. What is a variable?
Ans.:
Ans.:
i) PHP (Hypertext .Pre-processor ) is a widely-used open source general-purpose
scripting language. (i) Variable is a symbol or name that contains value.
ii) PHP runs on various platforms (Linux, Unix, Mac OS X, Windows etc.). (ii) Variables are-used for storing values such as numbers or characters.
iii) PHP supports a wide range of databases. PHP is easy to learn and runs efficienlly (iii) The stored values can be used in any part of the program.
on the server side. 6. What are the rules for declaring PHP variables?
Ans. : Following are the rules for declaring variables in PHP:-
5.2 Server Side Scripting ] (i) A variable starts with the$ sign, followed by the name of the variable

2. Explain Server Side Scripting? (ii) A variable name must start with a Jetter or the underscore character
Ans.: (iii) A variable name cannot start with a number
(iv) A . · · h racters and underscores
(i) A server is a computer system that serves as a central control of data and programs variable name can only contain alpha-numenc c a
shared by clients. (A-z, 0-9, and_) .
(v) y • . .. d $AGE are two different variables)
(ii) Tl,e server side environment that runs a scripting language is termed as web server. anable names are case-sensitive ($age an
) 5-25
- T,ei~ch~n~o:_<:lo~gy'.-.(:S::'.
'. ci::en_:::c~
e - - - - - - - - - - - -~ . Technology (Science)
TPS 1nfonnat1on T. f11ati 0 n · 5-26
Jnfor
. . ble scopes in PHP. tf5 •n the operators used in PHp
7. Explain vana J:~J'1al ,
o ators used in PHP are .
Ans, : d"fferent variables scopes in PHP : l. . orer .
There are three 1 . ~~s- · . '-Jlletic Operators
t,nh•
local Operator
global +
static
. ble declared outside a function has a GLOBAL SCOPE
(i) A vana _ . and ·*
accessed ou ts1de a function. . . can only b
~
•able declared within a funchon has a LOCAL SCOPE
. · . . ~c
e I
(ii) A
accessed within that funct10n. an Only b %
e want a local variable not to be deleted then 'we have to use . f . e
(iii) If w o static k Relational Operators
eyboa d
8. Explain PHP data types. ~ Operator Definition
Ans.: ___>_·-...,--t----G er~t:'._'h"'an _ _ _ t---...'Ex
.) String: _ A string is a sequence of characters. A string can be an t . .:.:r:..:e:.:
at:: .:~am5p~le'.__
(1 · Y ext ms·a ___ _ _ __:_a::::>::b__ ·
You can use single or double quotes: · 1
e quoies. ___<_ _ _+ _____:L=e:::s::_ s t'.'. 'hLQ,ann 7
(ii) Integer:- An integer data type is a non-decimal number between _2 147 >= Greater than or eq~u:a:i""1:: to-:----t---.:.a~<:.b'.:'._ _
4
2,147,483,647. ' , s3,648 and <= less than or equal to
a>=b
float (floating point numbers) a<=b
(iii) . :- A float. (floating point number) 1s
· a numb . Equal to
decimal point or a number m exponenhal form. er W1Iha a=b
;,- Explain PHP If statement with syntax.
(iv) Boolean : A Boolean represents two possible states: TRUE or FALSE.
~ns. :
(v) Array:- An array stores multiple values in one single variable.
if statement - executes some code if one condition is true.
(vi) NULL:- Null is a special data type which can have only one val . NUl.
variable of data type NULL is a variable that has no value assigned to ~e. L. A Syntax of if statement is

9. Explain Single line and multi line comments in PHP. if (condition) (


Ans.: code to be executed if condition is true;
(i) A comment in PHP code is a line that is not executed as a part of the program.
(ii) Its only purpose is to be read by someone who is looking at the code. 12. Explain PHP If else statement with syntax.
l.ns.:
(iii) . PHP supports several ways of commenting: Single l.ine comment and multi line
comment. The if .. .else statement executes some c~de if a condition is true and another code if
that condition is false.
(iv) Single line comment can be written in two ways, they are :-
/ / This is a single-line comment Syntax of if else is

# This is also a single-line comment if (condition) (


(v) Multi line comment can be written as:- code to be executed if condition is true;
l else (
; • This is a multiple-lines comment block that spans over multiple lines•/
code to be executed if condition is false;
logy (Science)_ _ _ _5-27
_ _ _ _ _ _ _ Server•Sict ogy (Science)
.
I form at10
n hO
Teen e~tip,
,,~,
--------5-28
rrs n with syntax. •I~,~ 5.6
. pHP for Ioop .,
Exp Ia1n
13-
•ns • te the same block of code again and . . MtaY and how to create anays in Pl{p .
,.. . . used to execu agalJ\ a !3"1'JatJ\
.) Loops are t e ' s Ion With syntax.
(1 . ndition is ru · g¾
certalii co . d h I 6
. ed when you know m a vance ow many times th 1· : .s a special variable, which can hold
(ii) The for loop 1s us e script sh AO'' array 1 more than one 1
·) f>,J1 y stores multiple values in one single v . bl va ue at a time.
~ . ~ (1 3 rra ana e
(ii) A!lpBP the array() fun~tion is used to create an array.
Syntax
(•ii) 1fl to create array 1s :- $a=array(value)
. counter) l
. .
for (uut coun ter,. test counter; increment
. I 5yntaX - -::-;:--~ .~ :::;:--..:.___
code to be executed for each.iteration; (i~l ; : , ~es of Arrays m PHP.
1/f,xpla1n •yr·
17•
I . PHP for each with syntax. 10S· : are three types of arrays: ·
14. Exp am nl d . ~ f{P there .
foreach loop works o y on arrays, an is used to loop thr JJ1 P ' d arrays - Arrays with a numeric index Fo ·
A.ns. : The ough
key /value pair in an array. ea~ i) In?~A~" "Toyota'');
I "J3JV1'. ' . r examp1e:-$cars= array("Volvo",

Syntax dative arrays - Arrays with named k F


(iil . A.sso ("Peter"=>"35", "Ben"=>"37", "Joe"=>"43").. eys. or example :- $age
foreach ($array as $value) l array ,
code to be executed; •Aultidimensional arrays - Arrays containing one or more arrays F 1
(iiil I" .. ro examp e :-
$cars = array
(
5.5 PHP String Functions array("Volvo",22,18),
array("BMW",15,13),
15. Explain String functions used in PHP (any 5). array("Saab",5,2),
array("Land Rover",17,15)
Function Description
. );

strlen() Returns the length of a string (i.e. total no. of characters)


str_word_count() Counts the number of words in a string [ 5.7 PHP User Defined Functions ·
strrev() Reverses a string
18. Explain how to declare ~ser defined function in PHP.
strpos() Searches for a specific text within a string and returns the characler Ans.:
position of the first match and if no match is found, then it will (i) A function is a block of statements that can be used repeatedly in a program.
return false
(li) A function will not execute automatically when a page loads.
str_replace() Replaces some characters with some other characters in a string (ili) A function will be executed by a call to the function.
substr() Returns a part of a string (iv) A user-defined function declaration starts with the wo rd function :
strtolower() Converts a string to lowercase Syntax
substr_count() Counts the number of times a substring occurs in a string function functionName() (
ucwords() code to be executed;
Converts the first character of each word in a string to uppercase
trim()
Removes whitespace and other predefined characters from both
sides of a string
logy t::ic1encei 5-30
,nology (Sciencee)1 _ _ _ _5-29
__ _ _ _ _ _ _ __ ·oJ'\ 'feChflO
tion Ted ~ aD . .
r rs Informa ents ~fo' cookies and <;l Session in Pl{p
. pHP function argum . 11" 1 ote oil .
J9. Explain s1tort J\ ·

J\IIS, : ed to functions throu gh arguments. An argUrn . ~- .


(i) Information can be pass ~s,- ·es·-
ent is ills\ Ii~ ~ coo1'1 • is often used to identify a user
,, ;
variable. . . 'd h il A coo,-,.e . : .
a
ts are spec1"fie d after the function name, ms1 e t e parenth
h eses y
l (i) kie is a small file that the server emb d .
(ii) Argumen ts as you w ant, just separate t em with a comm · 011 ~-~ A coo e son the user's co
~um~~~ · ~ . (ii) time the same computer requests a . mputer.
t::ach page with a bro •
Example is :- (ill.•) Pcook 1'e too . . wser, it will send the
(iii) .
<?php ·th p:HP you can both create and retrieve ki
(iV) <>T1yv ' coo e values.
function farnilyName($fname) l
5essioJ1 . .
echo·"$fname Refsnes.<br>" ; n,J . n is used to store user information on serv~ t
I' (i) Sess10 . . . . r o track user activities
l lps in web application to maintain user informati ·
(ii) It h e on on all the pages
familyName("Jani''); or example, if you login to gmail account, the session helps to
F
(iii) accoUilt 1 • .
familyName(''Hege''); a so. access youtube

farnilyName("Stale''); ~ tec-:p-::s--;t:o:-:c:r::e-;:-at~e~D~at:ta~b:a~s-;;e;&;:-:T:;:a::ib:il:es~.- - -- - - - - - ' - - - -


,.,....----Explain s .
13, .
familyName(" Kai Jim");
5
i\11 · ; te database and Table ;
familyName(" Borge"); Steps to crea
?> f or database creation ➔ open postgres sql terminal-➔ create database college; and
I) .
press enter. . .
5.8 PHP
L.,___ ____:.....:...._ Form
__ _ Handling
---::::..._ __ J For.Table creation - ➔ create table student(name text, gender Boolean); press enter ,
l) table w ill be created.
20. Explain difference between GET and POST.
Ans.: ~rograms for Pract ice>
(i) Both GET and POST are treated as superglobals w hich means that they are always
accessible regardless of scope. [ 5.4 First sample code of PHP
,(ii) It can be accessed from any function, class or file without having to do anything 1. Program to display "Hell~ world" message on the web page.
special. Coding :
(iii) $_GET is an array of variables passed via the URL parameters. <!DOCTYPE html>
<html>
(iv) $_pQST is an array of variables passed via the HTTP POST method.
<body>
21. Explain isset() method in PHP.
Ans.: <hl>My First Web Page< /hl>
<?php
(i) isset() method is used in PHP to check wh ether variable h as value or not.
echo "Bello World"·
(ii) This helps us to know if the button is clicked or not. 1> '
. ) _ _ _ 5-31
: nc_e:,___.
chnolog~yy~(S:c.::ie:__ _ _ _ _ _ __ Serv •.
rs !J1/ormat1on Te . GJ loc1'hG!!/php/sum;<p
T ute PHP programs:-
to save and exec . " "
Steps ogram and save 1t as demo.php using a t 7 C (D localhoSVphoiYJm
" ;,•,, p
1 Type the abo~e pr ny text editor
. Notepad, ged1t). . (for e
ith your name (for example php). 8
Create a fo Ider w . Sum of two numbm is JO
2. f Ider in server's root directory. For Ubuntu the th
~~ root dire
Note: Create/ hotml. For Windows the path of root directory is t ·/aXA=v1Pp
is var/ www / h ctory
"d O php" file in your folder. tdocs.
Save the em ·
3· b ser and type in address bar http:/ / localhost/ php / · Cli ck on de
4· Open
row rno.ph
- - - - -- - - - - - ,0~ - p.
1@1 1 n I
output : Glo:i ,ostlohf;;,co fhp X +
f- C (D localho;~phpid,mo.pho

My First Web Page


pf{P program to check and print greatest among two numbers.
H,llo \\'orld

coding:
<?php
$a:::100;
$b:::200;
if($a>$b)
echo "$a is greater than $b";
else
echo "$bis greater than $a";
2. PHP Program to print sum of two numbers.
Coding:
Output:~
<?php 9 localhost/phpigreat.php X +
$a=10; f- -"' C (1) localhost/php/great.php * • e
$b=20;
200 is greater than 100
$c=$a+$b;
echo "<br><br> Sum of two numbers is $c";
?>
11111 logY(Science
. chnology (Science) 5-33 f echflO •·~·- - -) - - - 5-:¾
f 1113 uon Te
S f1l'atiOll
. -------
",::bo <:br> Extract
erver-Sid e Seri . ..ii\rfo - Setver-Side Sc. .
TPS ln or . t numbers from 1 to 10
using for loop. Pting (I' chO some part of Str· "
.... to pnn
pHPprograu, tli>)
11·_
nptin g (PHP)
4. e ",::b r:>,::br> Converts String mg .substr($
echO in low a,5,3);
C
",::br><:br> ap1.ta lize each wor ercase ".strtolower($ )
ech 0 d of s . " a;
_______
_ trmg .ucwords($a);
?'?
\
echo "<br> $i";
e:i \ocalhost/ php/string,?hP X +
k ➔ .C (i) \ocalhost/php/string.phr,

Output:· * - e :
I C) \@l I lZ_J . is
G) localhost/php/for.php X + Strl111! information technology

t' C Length of String is 22
7 (D localhost/php /for.php
* ~ e Words in String is 2 .
1
2 Revers~ of String is ygolonhc
et noitamrofni
3
4 Position of o in String is 3 ·
5
6 Replace Infonntaion with Info
rm information technology
7
8 Extract some part of String mat
9
10 Converts String in lowercase
information technology
C~i taliz e ~ach word of strin
g Information Technology

5.5 PHP String Functions


5. PHP program to demonstrate stri
ng functions 5.6 PHP Arrays
Coding:
6, PHP pro gra m to pri nft he nam
<?php e of colors store in array variable.
Coding
$a="informa tion technology";
echo "<br><br> String is ".$a; <?p hp
"
echo "<br><br> Length of String $x=array ("Red" ,"Blue" ,"Brown" "Pink","Purple );
is ".strlen($a); ' -
echo "<br><br> Words in String echo "Color names are <br>";
is ".str_word_count($a);
echo "<br><br> Reverse of Stri fore ach($x as $value)
ng is ".strrev($a);
echo "<br><br> Position of o in \
String is ".strpos($a,"o");
echo "<br><br> Replace Informt echo "$value <br >";
aion with Inform ".str_replace
("Information","Inform" ,$a); .
?>
I f echJlolo__:g~y_(Sc_ie_n_ce_) _ _ _ 5-36
) p lt•)'I' (~Ll("ll l'l')
(1113tiOfl
11" lnf,,r111al""' I Pt "' • y,10

if ( ("Volvo",22,18),
Output : • Kul"
array ("Biv, 15 13),
'1[,>1 f vv , ,
5
array "Saa , ,2) ,
b"
Y(
arra "Land Rover",1715) ,
C olor ruu11 '" " ~u c- array (
R cd
Blu e
810 \\ rl
); 0 $ca rs[O][O) .": In stock: ".$cars[O]ll].", sold: . Sc
.
Pulk
P ur p lC'
ech ][OJ "· In t k- .. $c · ars[0]l2] . .<br>"·
echo $cars[1 ... · s oc . .: ars[l ]ll ].", sold:".$cars[]112].".<br>":
$cars[2][0). : In stock: .$cars[2l[ 1].'' sold·" c, l . '
echo ., .. ' · -->1.ars 2]12].. <br>"·
echo $cars[3 ][0]. : In stock: .$cars[3](1].", sold:".Scars[3]12].".<br>";

~ 5.7 PHP User Defined Functions

L--- - gram to print message "Welcome to PHP" using function·


J
pf{P pro
9,
7, PHP progra m to print the cost of product store in array variable
.
coding:
Coding : <?php
<?php function msg() / / declarin g a function
$p = ar rdy("Pen"=>"35", "Ball "=> "37", "eraser"=>"43") ;
~cho "Welcome to PHP"; / / defining function
echo "<br>l'en cost is ".$pl'Pe n'j;
l
echo "<br >Erase r co~t is ".$pl 'erase r'];
msg(); / / calling function
7>
?>
Output:
Output : X +
C ( I) l n.a11u1 u It., "' , I 1,t I
, e
11 r n , ,1,1 u 1,
* • =I 8
l• , 11•r 1 ., . 1 ,. 1 1
Welcome 10 PHP

8
stock.
· PHP program to print the infonna tion about cars sold and c~rs in
Coding :
~ Phf~ -
[
--- --
$car~ = arr,w
- - - - - - - - -- - -
w lltiO!l 'fecru1u•~o, • J - jts

. chnoJogY (Science)
5-37 S iii rl1' .
erver-Side Seri . ,rl" l o "' "10+20=" .sum.(10,20 ). "<br><br>"·
nforrnat1on Te .
rrs I . t Name and Price of the product by passi Pting (Jlt, ec"o
"~) '
pl{P program to pnn . ng argUtne1:11s t "l l +2= " .sum(ll,2). "<br><br>"·
echo ,
. 10. £unction-
o "' .. 1+9=" .sum(31,9). "<br><br>"·
eo• 0 3 · ,

G] loca!hosVphp/ svm!un.php
X +
~ ➔ C ©
I "P duct name is $n and Prite is $price <br><br>"; . localhost/php/ sumfun.php
~~ ro . Jo+20= 30
~1 e
l 11-2= 13
pr("Notebook" ,45); 3 1+9=40
pr("Calculator",400);
pr("Pen" ,30);
?>
l-'=..!..@_J__g _,
Output:
(;- ➔ Q (D localhost/pnp/ funct ion3rg;.ph~ * 4' ~J e :
Product name is Notebook and Pncc ts 45

Product name is Calculator and Price is 400

Product name is P~ and Price is 30

[ ______,_____ _s_.s__P_H_P_F_o_rm_H_an_d_l_in.:!:g~_ _ _ _ _ _ __j


12. Create a HTML form with two input fields to accept number from the user and a
button to print addition of two numbers.
Coding:
Form.htin i
<html>
ll. PHP program to print sum of two numbers using function. <body>
Coding: · <form action="ad d.php" method =post">_
<?php Enter First Number
function sum(int $a,int $b) <input type="text" name="tl"><br><br>
I Enter Second·Nu mber
$c=$a+$b; <input type="tex"t" name="t2"><br><br>
<input type="subm it" value="Add">

~
</ form>
- ~etu
- rn- $c-; - - - - - - -- - - -
</body>
-hn ology (Science) 5
5-39 Te'-'~-~
(ll' ~tJ·ori · -=--=-~~-:_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-:=_-.:. _ -40
0
,.;;1
,r'
ri ,roe==
"text "·name ="name " id ="i"d n
- ame"> b
i.!1Put 11 , . </labe l> < r><br>
t. c ender
7
t. l3 bel d
- "r a 10 n" " ame "
= g en er id=" ·dd "
. ut t)'Pe- . " " " l ....gender" value=" "
t.illP . -"r adio name= gender id- "id male >Male<br><b
<:?php . ut t)'Pe- . " " - ....gender" value="f " r>
t.illP - "radio name= gender" id-"id emale >FeMale<b > b .
$tl o=c$_GET["tl "]; . ut t)'Pe- . " " . - ....gender" value=" " r < r>
t.illP e=="subrrut name= submit" value="sub . ,, . ,, other >Other<br><br>
$tZo=c$_GET["t2"]; t.il'Put typ . nut id= submit">
$c=$t1 +$t2; . ,, ,:_/form::>
,, b <br> Sum of two numbers 1s $c ;
echo < r> t.fbodY;;,
?> ,:_/ html::>
Output : d phP " . _ ·
<:-- ➔ C <D 1oc.;:ilhost / f)r,p;mrm ntm! * <!o ,,,, e •
$servern
arne= pgsql.host-localhost;dbname=coll "·
" . ege ,
Entel Fust !'\umbC':%" 1.2 5
3
me="postgres ; ·
$userl1
Em~ ~coud :Sumbcr r,o
sword="laxmi" ;
$pas
1 Add
$COIU1"'new _PDO($servemame,$usemame,$password);
if(isset($]OST(' submit']))

I
$narne=$_POST(''name''];
$gender=$_POST("gender"]; .
$sqlo=c"INSERT INTO student(name,gender) VALUES ('".$name."','" .$gender."')";
$conn->exec($sql);
13. PHP program to create admission form for student. Accept name and gender from echo "N ew record added successfully";
the student. Create a college database and create table student having the fields
name and gender. ·
Steps lo create database and Table :
1) For database creation ➔ open postgres sql terminal - ➔ create database college; and \ PHP Extra Programs for Practice~ ·
p ress enter.
Write a PHP code which calculates and prints addition, subtraction,
2) For Table creation - ➔ create table student(name text, gender Booleatn); press enter ,
table w ill be created . multiplication, division of tw·o numbers using form.
i\ns. :
Now type code in admission.php
Coding:-
. <!DOCTYPE html>
<html> <html>
<body> <bod y>

<hl align="center" >Addmission Form</ hl > <form method="post" >


<form method="post" > Enter Firs t Number
<label>Enter Name</lab el> <input type ="tex t" n a m e ="first"><br><br>
pu e, t\.eJ S-42
logy
5-41 - - -- fechn°
rnat1. on Technology
(S_c_ie_nc_e_) - - - - - - atiOfl
TPS lnfor yJorJll
d Nu mbe r _ b .dJ1w:J;;,
Ente r Secon
" b f
" t" nam e="seco nd >< r>< r>" ."
" . "
<input type= tex value== Sub mit > L?p J1P pO ST ['su bm it']))
t" nam e= sub
. mit
<input type =".su bmi J(isset($_
</ form>
</ body>
I 3 4·
.1 '_ $- PO ST[ 'rad ius '];
$?'"d·u
</ html> s-
$ra i *$r adi us" $ra diu
s;
<?php .
$a:::: $p
if(isset($_P0ST['su bmi t'])) $ *Z*$radius;
le is: ".$a;
I "~b r>< br> The Are a of circ
$C:::: .
$first= $_P0ST['first']; echo c· f
0 "<b r>< br>The 1rc
um erence of crrcie is: ".$c·'
$second = $_p 0ST ['se con d'); ech
$sum = $first+$second;
7>
$sub = $first-$second; gf
ulates square of a number usin onn ,
$mu! = $first•$second; · .te a PHP cod e wh ich calc
wn
$div = $fir st/$ seco nd; i\JlS,;

n is: ".$s um; Coding:


echo "<br ><b r>T he Add itio
tion is: ".$s ub; <htm l>
echo "<br ><b r>T he Sub trac
lica tion is: ".$m u!; <bo dy>
echo "<br>< br>The Mu ltip
n is: ".$div; <form me tho d=" pos t">
echo "<br><br>The Div isio
Enter a Nu mb er
e=="sq"><br><br>
<in put typ e=" tex t" nam
e=="subrnit" value="Sqaure">
ren ee of, ci<d,. <in put typ e=" sub mit " nam
eakulotes and pri nts "•• and d~umfe </fo rm>
2. Wr it,• PHP cod , which form .
Hint(area=3.14x r' and c==2x3.14xr) usi ng
Ans . : </ bod y>
Cod ing: </ html>
<?p hp
<html>
)
if(isset($_P OS T['s ubm it'])
<body>
(
<form method ="p ost" >
$sq== $_P OS T['s q'];
Enter Radius
$s==$sq*$sq;
radi us" ><b r> <br >
<input type ="tex t" nam e=" echo "<h r>< br> The Squ
are of number is: ".$s;
,,
<input t)rp e-"s b 011' t" · )
> - · u nam e= sub mit " value:::"Sub mit ">
· fonn
</
</ bod y>

You might also like