Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
2K views

5 Server Side Scripting (PHP)

1. PHP is a widely used open source server-side scripting language that runs on web servers and allows developers to create dynamic web applications. 2. Key features of PHP include being interpreted rather than compiled, which means no compilation is required, as well as being platform independent, simple, and faster than other scripting languages. 3. PHP code is executed on the server side, with the results sent to the client, so the PHP code is hidden from users.

Uploaded by

ES Birnur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
2K views

5 Server Side Scripting (PHP)

1. PHP is a widely used open source server-side scripting language that runs on web servers and allows developers to create dynamic web applications. 2. Key features of PHP include being interpreted rather than compiled, which means no compilation is required, as well as being platform independent, simple, and faster than other scripting languages. 3. PHP code is executed on the server side, with the results sent to the client, so the PHP code is hidden from users.

Uploaded by

ES Birnur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Server-Side Scripting (PHP)

Chapter at a Glance
5.1 Introductlon 5.2. Server Side
to PHP Scripting

5.3 features of
PHP

PHP Peatures : • Simple, Faster, Open Source. Platform Independent.


Case Sensitive, Error reporting, Interpreted etc.

5.4firsf Sample
code of

II

PHP file extension. PHI' Case St'nsitivity, PHP nata Types: - Control structures in PHI' : -
php, how toexecute PHI' variables, rules String, Integer, Float, If statement. if else statement.
PHP program, for declaring 80011.',111, Array, loop structure in PHI' : -
how 10 save code variables, different NUll Comments in For, foreach with syntax and
in specific folder variable scopps PHI', Single line and examples
in PHPwitll multi-line comments,
examples

5.5PHP 5.6 lIMP SIring


Arrays Functions

(:r~ate an 'array, types of array; indexed strleru), strrevO, substrt), strtolowN(),


,lfmy, mulu-dlmenstonal arrays, PHI' associaltv •..• trimf), ucwordst), stfjeplaceO,
arrays with syntax and examples. str_'word_col1ntO, stropst), substr.:,o:ountO

5.7 PHPUser 5,8 PHI' form


Defined Functions Handling

Function meaning, syntax, Simple HTl\Il form with php code, GET vs POST
fundi on ilrguments, returning " with examples, create database connection object,
value to a function with examples SQL statement and its execution, cookies ,)11(1
sessions in PHI'.
TPS Information Technology (Science) 5-2 Server-Side Scripting (PHP)

5.2 Server Side Scripting

1. A is a Computer system that serves as a central repository of data and


programs~
Ans. : Server
2. The server side environment that runs a scripting language is called .
Ans.: Web Server
3. PHP is side scripting language.
Ans. : Server

5.3 Features of PHP

4. PHP is language, there is no need for compilation.


Ans. : Interpreted

5.4 First Sample code of PHP


5. PHP files have extension ~ .
Ans. :.php
6. . keyword is used to display text on web page.
Ans.: echo
7. . is a symbol or name that stands for a value.
Ans. : Variable
8. A variable starts with the sign followed by the name of the variable.
Ans.:$
9. A variable declared outside a function has a scope and can only be
accessed outside a function.
Ans. : Global
10. A variable declared within a function has a scope and can only be
accessed within that function.
Ans.: Local
C!' 11. . are the statements in PHP code, which are not visible in the output of the
program.
Ans.: Comments
12. The statement allows programmer to make decision based on one more
conditions.
Ans.: if
TPS Information Technology (Science) 5-3 Server-Side Scripting (PHP)

13. The statement allows programmer to make decision based on either this
or that condition.
Ans. : if else
14. . are used to execute the same block of code repeatedly as long as certain
condition is satisfied.
Ans.: loop
15. . loop works only on arrays.
Ans. : foreach
16. . functions returns the length of a string.
Ans. : strlenO
17. . functions counts the number of words in a string.
Ans.: stcword_countO
18. . function reverses a string.
Ans. : strrevO
19. . function searches for a specific text within a string.
Ans. : strposO
20. . function replaces some characters with some other characters in a string.
Ans. : stcreplaceO
·21. function returns a part of a string.
Ans. : substrO
22. . function converts a string to lowercase.
Ans. : strtolowerO
23. . function counts the number of times a substring occurs in a string.
Ans. : substr_countO
24. . converts the first character of each word in a string to uppercase.
Ans. : ucwordsO
25. .. . .. .. .. functions removes whitespace and other predefined characters from both
sides of a string.
Ans.: trimO

5.5 PHP String Functions

26. A is series of characters.


Ans.: string
27. A is a block of statements that can be used repeatedly in a program.
Ans. : function
TPS Information Technology (Science) 5-4 Server-Side Scripting (PHP)

5.6 PHP Arrays

28. An is a special variable which can hold more than one value at a time.
Ans.: array
29. An stores multiple values in single variable.
Ans.: array
30. The PHP functions is used to create an array variable.
Ans. : arrayO
31. There are types of arrays in PHP.
Ans.: Three
32. The arrays are the arrays with numeric index.
Ans. : Indexed
33. The arrays are the arrays with named keys.
Ans. : Associative
34. The arrays are the arrays containing one or more arrays.
Ans.: Multi-dimensional
35. The function is used to return the length of an array.
Ans. : countO

5.7 PHP User Defined Functions

36. A is a block of statements that can be repeatedly in a program.


Ans. : function
37. Information can be passed to functions through .
Ans. : Arguments
38. . are specified after the function name, inside the parentheses.
Ans. : Arguments
39. The statement is used to let a function return a value.
Ans. :retum

5.8 PHP Form Handling

40. The form data is sent with the ; method.


Ans. : HTTP POST
41. . is an array of variables passed via the URL parameters.
Ans.:$_GET
TPS Information Technology (Science) 5-5 Server-Side Scripting (PHP)

42. . is an array of variables passed via the URL parameters.


Ans. : $_POST
43. Information sent from a form with method is visible to everyone.
Ans.: GET
44. . method may be used for sending non sensitive data.
Ans.: GET
45. Information sent from a form with .method is invisible to everyone.
Ans.:POST
46. . method has no limits on the amount of information to send.
Ans.:POST
47. .. object helps to connect PHP code with any database server.
Ans.:PDOO
48. . method in PHP is used to check whether variable has value or not.
Ans. : issetO
49. A is a small text file that the server sends on the user's computer.
Ans. : Cookie
50. . is used to identify user or its machine and track activities created on the
user computer.
Ans. : Cookie
51. When browser requests server page are sent along with the request.
Ans. : cookie
52. . is used to store user information on server to track user activities.
Ans. : session
53. . helps web application to maintain user information on all the pages.
Ans. : Session

5.1 Introduction to PHP

1. PHP is widely used open source general purpose scripting language.


Ans.: True
2. PHP does not supports databases.
Ans.: False
3. PHP runs on client side.
Ans.: False
TPS Information Technology (Science) 5-6 Server-Side Scripting (PHP)

5.2 Server Side Scripting

4. The server side environment that runs a scripting language is termed web server.
Ans. : True
5. PHP hides the code from the user.
Ans. : True

5.3 Features of PHP

6. PHP is very difficult to use.


Ans.: False
7. There is no need of compilation for PHP code.
Ans. : True
8. PHP is faster than other scripting language.
Ans. : True
9. You need to pay for use of PHP.
Ans.: False
10. PHP is platform independent.
Ans.: True
11. PHP is case sensitive only at the time of variable declaration.
Ans. : True

5.4 First Sample code of PHP

12. The PHP code can also be embedded with HTML tags using <?PHP and? >
Ans.: True
13. PHP files are saved with .htrnl extension.
Ans.: False
14. echo keyword is used to display text on web page.
Ans. : True
15. Variables are used to store values.
Ans. : True
16. A variable starts with $ sign, followed by name of variable.
Ans.: True
17. A variable name can starts with a number.
Ans.: False·
TPS Information Technology (Science) 5-7 Server-Side Scripting (PHP)

18. A variable name can contain only alpha-numeric characters and underscores.
Ans. : True
19. A variable declared outside a function has a GLOBAL SCOPE.
Ans. : True
20. A variable declared within a function has a LOCAL SCOPE.
Ans.: True
21. Variables can store data of different types.
Ans. : True
22. To check data type of variable var _ put ( ) method is used.
Ans.: False
23. Comments are the non executable statement in PHP code.
Ans.: True
24. If statement allows to make decision based on one or more conditions.
Ans. : True
25. For each loop works only on arrays.
Ans.: True
26. StrLengthO function returns the length of a string.
Ans.: False
27. Strword countt) function counts the number of words in a string e,

Ans.: True
28. StrrevO function reverses a string.
Ans. : True
29. Strpost) function returns a string.
Ans. : True
30. Str_replaceO function replaces some characters with some other characters in a
string.
Ans. : True
31. Substrt) function returns a part of a string.
Ans.: True
32. Strlowert) function converts a string to lower case.
Ans.: False
33. Substrjcountt) function counts the number of times a substring occurs in a string.
Ans. : True
34. Ucwordsl) function converts the first characters of each word in a string to
uppercase.
Ans. : True
TPS Information Technology (Science) 5-8 Server-Side Scripting (PHP)

35. trimt) function adds white space and other pre defined characters from both sides of
a string.
Ans.: True

5.5 PHP String Functions

36. A string is a series of characters.


Ans. : True
37. A function is a block of statements that can be used repeatedly in a program.
Ans.: True

5.6 PHP Arrays

38. Array can store multiple values at a time.


Ans.: True
39. In PHP, arrayf) function is used to create a array.
Ans.: True
40. Syntax of creating array variable is a = arr (values).
Ans.: False
41. In PHP there are six types of arrays.
Ans.: False
42. An array with a numeric index is called indexed arrays.
Ans.: True
43. Multi-dimensional arrays are the arrays with named keys.
Ans.: False
44. Associative array contains one or more arrays.
Ans.: False
45. Array index always starts with zero.
Ans.: True
46. The lent) function is used to return the length of an array.
Ans.: false
47. PHP can handle multiple levels of multi dimensional arrays.
Arts. : True
48. We can create our own functions in PHP.
Ans.: True
TPS Information Technology (Science) 5-9 Server-Side Scripting (PHP) .

5.7 PHP User Defined Functions

49. A function is a block of statements that can be used repeatedly in a program.


Ans. : True
50. A function name cannot start with a number.
Ans. :True
51. Information can be passed to functions through methods.
Ans.: False
52. Arguments are specified after the function name, inside the parentheses.
Ans.: true
53. Return statement is used to return a value of a function.
Ans.: True

5.8 PHP Form Handling

54. The form data is sent with the HTTP Post method.
Ans.: True
55. The PHP superglobals $_GET and $_POST are used to collect form data.
Ans.: True
56. $_GET and $_POST syperglobals which means that theyare never accessible.
Ans.: False
57. $_GET is an array of variables passed via the URL parameters.
Ans.: True
58. $_POST is an array of variables passed via the HTTP POST method.
Ans.: True
59. Information sent from a form with GET method is not visible to everyone.
Ans.: False
60. GET has limits on the amount of information to send.
Ans. : True
61. GET should NEVER be used for sending passwords or other sensitive information.
Ans.: True
62. Information sent from a form with POST method is invisible to everyone.
Ans. : True
63. POST has limits on the amount of information to send.
Ans.: False
TPS Information Technology (Science) 5-10 Server-Side Scripting (PHP)

64. issetO method in PHP is used to check whether variable has value or not.
Ans. : True
65. A cookie is a small text file the client sends on the user's computer.
Ans.: False
66. When browser requests server page, cookies are sent along with the request.
Ans. : True
67. Cookies store visited page on browser to optimize search.
Ans.: True
68. Session is used to store user information on server to track user activities.
Ans. : True
69. Session helps web application to maintain user information on all the pages.
Ans.: True
70. PHP Data Object helps us to connect PHP code in UniformMethod of access to
multiple databases.
Ans.: True

5.1 Introduction to PHP

1. ., . .. . . ... . .. .. . ... is a widely used open source server side programming language
which runs on various platforms.
(a) Word (b) PHP (c) Excel (d) PowerPoint
Ans.: (b)

5.2 Server Side Scripting


2. A is a Computer system that serves as a central repository of data and
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)
TPS Information Technology (Science) 5-11 Server-Side Scripting (PHP)

5.3 Features of PHP

4. . is a script executed on server which generate dynamic HTML pages.


(a) PHP . (b) MS Word (c) MSExcel (d) Client
Ans.: (a)

5.4 First Sample code of PHP

5. The PHP code can also be embedded with HTML tags using , .
(a) <!Php!> (b) <Pphp and T»
(c) <=php> (d) <#php#>
Ans.: (b)
6. PHP files are saved 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)

9. A variable in PHP starts with the sign followed by name of the variable.
(a) # (b) @ (c) = (d) $
Ans.: (d)

10. PHP variable name cannot starts with a .


(a) Number (b) Character
(c) Alphanumeric (d) Dollar
Ans.: (a)
11. Correct method for declaring PHP variable is .
(a) # age (b) $ age (c) @ age (d) age
Ans.: (b)
12. A variable declaration a function has a GLOBAL SCOPE.
(a) inside (b) middle (c) outside (d) topside
Ans.: (c)
IPS Information Technology (Science) 5-12 Server-Side Scripting (PHP)

13. A variable declared a function has a LOCAL SCOPE


(a) Inside (b) outside· (c) between (d) within
Ans. : (d)
14. PHP stores all variables in an array called $GLOBAL[Index]
(a) local (b) global (c) Static (d) within
Ans.: (b)
15. To check data type of variable in PHP method is used.
(a) var_dumpO (b) var _delO
(c) var removet) (d) var_deleteO
Ans.: (a)
16. . are the statements in PHP code which are not visible in the output of the
program.
(a) Methods (b) comments (c) Objects (d) values
Ans.: (b)
17. How to add a single line comment in PHP ?
(a) // (b) # (c) $ (d) @

Ans.: (a)

18. How to a multiline comment in PHP ?


(a) // (b) # (c) $ (d) /*and*/
Ans.: (d)
19. . statement allows programmer to make decision, based on one or more
conditions.
(a) if (b) loop (c) variable (d) method
Ans.: (a)

20. . statement allows programmer to make decisions based on either this or


that decision.
(a) loop (b) if else
(c) variable (d) method
Ans.: (b)

21. . are used to execute the same block of code repeatedly as long as a certain
condition is satisfied.
(a) Variable (b) Method (c) Object (d) loops
Ans.: (d)
22. ............ loop works only on arrays.
(a) for next (b) for (c) foreach (d) forone
Ans.: (c)
TPS Information Technology (Science) 5-13 Server-Side Scripting (PHP)

23. . function returns the length of a string.


(a) strrevO (b) strleru) (c) strlengthO (d) substri)
Ans.: (b)
24. . function counts the number of words in a string.
(a) strleru) (b) strword countf)
(c) substrt) (d) trimt)
Ans.: (b)
25. . .. , function reverses a string.
(a) strrevO (b) trirru) (c) UCwordsO (d) stropst)
Ans.: (a)

26. . function searches for a specific text within a string.


(a) substrt) (b) strrevt) (c) strpost) (d) str replacet)
Ans.: (c)
27. . function replaces some characters with same other character in a string.
(a) strrevt) (b) str ~replaceO
(c) trimt) (d) strpost)
Ans.: (b)
28. . function returns a part of a string.
(a) substrt) (b) trimt) (c) strrevO (d) strIenf)
Ans.: (a)
29. . function converts a string to lowercase.
(a) UCwordsO (b). strtolowert)
(c) strlowert) (d) trimt)
Ans.: (b)
30. . function counts the number of times substring occurs in a string.
(a) substrt) (b) substr counn)
(c) trimt) (d) strleru)
Ans. (b)
31. . function converts the first character of each word in a string to upper case.
(a) substrt) (b) strtolowert)
(c) trimt) (d) ucwordst)
Ans.: (d)
32. ............ function removes whitespace and other predefined characters from both
sides of a string.
(a) trimr) (b) strleru) (c) strapst) (d) strrevt)
Ans.: (a)
TPS Information Technology (Science) 5-14 Server-Side Scripting (PHP)

5.5 PHP String Functions

33. A is series of characters.


(a) String (b) Number (c) Object (d) Value
Ans.: (a)
34. A is a block of statements that can be used repeatedly in a program.
(a) function (b) class (c) method (d) variable
Ans.: (a)

5.6 PHP Arrays

35. ..: is a variable which can hold more than one value at a time.
(a) Array (b) Method (c) Object (d) Constant
Ans.: (a)
36. In PHP function is used to create array.
(a) art'() (b) arrayt) (c) valuef) (d) variablet)
Ans.: (b)
37. Syntax of creating array variable is .
(a) @ a = arrayf) (b) #a = arrt)
(c) = arrayi) (d) $a = array (values)
Ans.: (d)
38. There are types of arrays in PHP.
(a) Two (b) One (c) Three (d) Four
Ans.: (c)
39. . arrays are the arrays with a numeric index.
(a) Associative (b) Indexed
(c) Multi-dimensional (d) Normal
Ans.: (b)
40. . arrays are the arrays with named keys.
(a) Indexed (b) Associative
(c) Dimensional (d) Normal
Ans.: (b)
41. . arrays are arrays containing one or more arrays.
(a) Multi-dimensional (b) Indexed
(c) Associative (d) Numbered
Ans.: (a)
TPS Information Technology (Science) 5-15 Server-Side Scripting (PHP)

42. Array index always starts with .


(a) one (b) two (c) zero (d) four
Ans.: (c)

5.7 PHP User Defined Functions

43. A is a block of statements that can be repeatedly in a program.


(cr) function (b) condition (c) object (d) value
Ans.: (a)
44. Information can be passed to functions through .
(a) Condition (b) arguments (c) array (d) variable
Ans.: (b)

5.8 PHP Form Handling

45. The form data is sent with the method.


(a) HTTP POST (b) FTPPOST
(c) STP POST (d) SMPT POST
Ans.: (a)
46. . is an array of variables passed via the URL parameters.
(b) #GET (c) ._GET
Ans.: (a)
47. . is an array of variables passed via the HTTP POST method.
(a) $POST (b) $_POST (c) @_POST (d) #_POST
Ans.: (b)
48. Information sent from a form with method is visible to everyone.
(a) POST (b) GET (c) HTTP (D) FTP
Ans.: (b)
49. Information sent from a form with method is invisible to everyone.
(a) POST (b) GET (c) HTTP (d) PTP
Ans.: (a)
50. . .. , NEVER be used for sending sensitive information.
(a) POST (b) HTTP (c) GET (d) PTP
Ans.: (c)
51. . method in PHP is used.to check whether variable has value or not.
(a) outsett) (b) insett) (c) nosett) (d) issetf)
Ans.: (d)
TPS Information Technology (Science) 5-16 Server-Side Scripting (PHP)

52. A is a small text file that the server.sends on the user's computer.
(a) Cookie (b) session
(c) Presentation (d) Method
Ans.: (a)
53. ... , are sent along when browser requests server pages.
(a) Cookie (b) Session (c) Method (d) Server
Ans.: (a)
54. . .. , is used to store information on server to track user activities.
(a) Cookie (b) session (c) Presentation (d) Object
Ans.: (b)
55. . helps web application to maintain user information on all the pages.
(a) Session (b) presentation
(c) Cookie (d) object
Ans.: (a)

5.1 Introduction to PHP

1. PHP runs on following platform.


(a) Linux (b) Excel (c) Word (d) Windows
Ans. : (a), (d)
2. PHP is compatible with following servers.
(a) Unix (b) XAMMP (c) Linux (d) Apache
Ans. : (b), (d)

5.2 Server Side Scripting


3. Programming languages for server side programming are .
(a) PHP (b) Tally (c) Excel (d) Python
Ans. : (a), (d)

5.3 Features of PHP


4. Features of PHP are .
(a) Interpreted (b) Open source
(c) Difficult (d) Platform dependent
Ans.: (a), (b)
TPS.Information Technology (Science) 5-17 Server-Side Scripting (PHP)

5.4 First Sample code of PHP

5. Popular PHP Frameworks are ,.


(a) Echo (b) Laravel (c) ServerSide (d) Symfony
Ans. : (b), (d)
6. Variables are used for storing values such as and............ .
(a) Methods (b) Numeric values
(c) Character Strings (d) Functions
Ans. : (b), (c)
7. A variable starts with the sign followed by of the variable.
(a) $ (b) # (c) name (d) value
Ans. : (a), (c)
8. A variable declared a function has GLOBAL SCOPE and a variable
declared a function has LOCAL SCOPE.
(a) inside (b) outside (c) within (d) between
Ans. : (b), (c)
9. Following are the data types used in PHP .
'(a) Boolean (b) Text (c) Number (d) Array
Ans. : (a), (d)
10. Control structures in PHP are .
(a) for (b) if statement
(c) for each (d) if else statement
Ans. : (b), (d)
11. . Loop structure in PHP are .
(a) for (b) for each (c) if (d) if else
Ans. : (a), (b)
12. Comments in PHP is possible in following ways.
(a) II (b) \\ (c) /*and*1 (d) 1# and #1
Ans. : (a), (c)
13. Comments are of and type.
(a) Single line (b) No line (c) Two line (d) Multi-line
Ans. : (a), (d)
14. Following are the pre defined functions in PHP to manipulate string.
(a) trimt) (b) substrt) (c) lengthO (D) strreverset)
Ans, : (a), (b)
TPS Information Technology (Science) 5-18 Server-Side Scripting (PHP)

5.7 PHP User Defined Functions

15. A function name can start with a or .


(a) symbol (b) Number (c) Letter (d) Underscore
Ans. : (c), (d)

5.8 PHP Form Handling

16. Form data can be sub~itted by or method.


(a) Return (b) forward (c) GET (d) POST
Ans. : (c), (d)
17. T~e PHP super globals and are used to collect form data.
(a) $_GET (b) $POST (c) $GET (d) $_POST
Ans. : (a), (d)
18. .., is an array of variables passed via URL parameters and is an
array of variables passed via HTTP POST method.
(a) $GET (d) $POST
Ans. : (b), (c)
19. . has limits on the amount of information to sends whereas has no
limits on the amount of information to send.
(a) Value (b) Method (c) GET (d) POST
Ans. : (c), (d)

5.1 Introduction to PHP

1. PHP runs on following platforms .


(a) Systems (b) Words (c) Linux (d) Excel
(e) Unix (f) Windows
Ans. : (c), (e), (f)
2. PHP is compatible with following servers.
(a) XAMMP (b) Linux (c) Apache
(d) Unix (e) Lightpad (f) Word
Ans. : (a), (c), (e)
TPS Information Technology (Science) 5-19 ,Server-Side Scripting (PHP)

5.2 Server Side Scripting

3. Programming languages for server side programming are


(a) PHP (b) Windows (c) Linux (d) Python
(e) Unix (f) Java and JSP
Ans, : (a), (d), (f)

5.3 Features of PHP

4. Features of PHP are .


(a) Difficult (b) Simple (c) Error Creating
(d) Platform Independent (e) Error Reporting
(f) Platform Dependent
Ans. : (b), (d), (e)

5.4 First Sample code of PHP

5. Variables are used for storing values such as , : , and

(a) Character (b) Numeric (c) Objects (d) Methods


(e) Functions (f) Memory addresses
Ans. : (a), (b), (f)
6. The scope of variable in PHP are .
(a) Local (b) Regional (c) National (d) Global
(e) Static (f) International
Ans. : (a), (d), (e)
7. PHP data types are .
(a) Text (b) String (c) Number (d) Integer
(e) Empty (f) Boolean
Ans. : (b), (d), (f)
8. Following are the predefined functions in PHP to manipulate string
(a) Lowert) (b) stropsf) (c) substrt) (d) strleru)
(e) upperf) (f) strlengtht)
Ans. : (b), (c), (d)
TPS Information Technology (Science) 5-20 Server-Side Scripting (PHP)

5.6

9. In PHP three types of arrays are . , • 0·•••••••••••••••••• 0" 0'

....................... ,
(a) inside (b) multiple (c) indexed (d) associate
(e) outside (f) multi-dimensional
Ans. : (c), (d), (f)

5.1 Introduction to PHP

PHP (Hypertext Pre-processor ) is a widely-used open source general-purpose


scripting language. PHP runs on various platforms (Linux, Unix, Mac OS X, Windows
etc.). PHP supports a wide range of databases. PHP is easy to learn and runs efficiently on
the server side.

5.2 Server Side Scripting


A server is a computer system that serves as a central repository of data and
programs and is shared by the clients. The server-side environment mAT runs a scripting
language is termed web server. A user's request is fulfilled by running A script directly
on the web server to generate dynamic html pages. This html is then sent to the client
browser.
Few programming languages for server side programming are:-
1) PHP 2) Java and JSP 3) Python

5.3 Features of PHP

PHP is the most popular and frequently used worldwide server-side scripting
language. Following are some of the features of PHP :
• Simple: It is very simple and easy to use, as compared to other scripting languages.
• Interpreted: It is an interpreted language, i.e. there is no need for compilation.
• Open Source: Open source means you need not pay for use of PHP. You can freely
download and use.
• Platform Independent: PHP code will be run on every platform, Linux, Unix, Mac
OS X, Windows.
• Error Reporting: PHP have some predefined error reporting constants to generate a
warning or error notice.
TPS Information Technology (Science) 5-21 Server-Side Scripting (PHP)

5. 4 First sample code of PHP

The PHP code usually enclosed in a special start and end processing instructions
<?php and?> that allows us to move in to and out of PHP mode. All PHP files have .php
extension.
Simple example to display "Hello world" message on the web page
Coding: .
<!DOCTYPEhtml>
<html>
<body>
<h1>My First Web Page</hl>
<?php
echo "Hello World";
?>
</body>
</html>
Steps to save and execute PHP programs:-
1. Type the above program and save it as "demo.php" using any text editor. (for eg
Notepad, gedit).
2. Create a folder with yourname (for example php).
Note: Create folder in server's root directory. For Ubuntu the path of root directory
is var/www /htmL For Windows the path of root directory is c:/XAMPP /htdocs.
3. Save the "demo.php" file in your folder.
4. Open browser and type in address bar http://localhost/php/.Click on demo.php.
Output :.ibi[§Ji ..21
~ be;!host/pcp/dem"php X

:My Fit'st 'Veb Page


Hello World
TPS Information Technology (Science) 5-22 Server-Side Scripting (PHP)

PHP case sensitivity


In PHPf the variable names are case sensitive. However keywords, function and
class names are not case sensitive. The echo keyword is not case sensitive.
PHP variables
Variable is a name that stands for a value. Variables are used to store values
such as numeric and character values etc.
Rules for declaring PHP variables:
• A variable starts with the $ sign, followed by the name of the variable
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-Zf
0-9 and _)
f

• Variable names are case-sensitive ($name and $name are two different variables)
• There are three different variable scopes in PHP :
1. local
2. global
3. static
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function (variable $a). And a variable declared within a function has a
LOCAL SCOPE and can only be accessed within that function (variable $b). In some cases,
if we want a local variable not to be deleted then the use of "static" keyword is must.
Example
PHP Program to print sum of two numbers.
Coding:
<Pphp
$a=lO;
$b=20;
$c=$a+$b;
echo "<brc-cbr> Sum of two numbers is $c";
?>
TPS Information Technology (Science) 5-23 Server-Side Scripting (PHP)

Output:

f- C (j) iocaiho5t'php/,uf]1,pnp
*, e
Sum of two numbers is30

PHP Data Types


Variables can store different types of data. PHP supports following data types:
1) String 2) Integer
3) Float 4) Boolean
5) Array 6) Null
Note: To check the data type of the variable var dumpt) method is used.
Comments in PHP
Comments are the statement in PHP block which are not executed in the program.
There are two types of comments, Single line comment and multi-line comments.
How to use Single line comment
/ / Single line comment
How to use Multi line comment
/* Multi
line
comment*/
TPS Information Technology (Science) 5-24 Server-Side Scripting (PHP)

Operators in PHP are:


1. Arithmetic Operators
Operator Definition Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
~
% Modulus a%b
2. Relational Operators
Operator Definition Example.
> Greater than a>b
< Less than a<b
>= Greater than or equal to ar-=b
<= Less than or equal to a<=b
-- Equal to a=b

Control Structures in PHP


1. If statement in PHP:-
If statement allows the programmer to make decision based on one or more
conditions, and execute statement accordingly.
Syntax :-
if(condition)

Block of statements;

2. If else if statement in PHP:-


If else if statement allows the programmer to make decision based on eitherth
that conditions.
J
Syntax-
if(condition)

Block of statements;
TPS Information Technology (Science) 5-25 Server-Side Scripting (PHP)

else

Block of statements;

Example
PHP program to check and print greatest among two numbers.
Coding:
<?php
$a=100;
$b=200;
if($a>$b)
echo "$a is greater than $b";
else
echo "$b is greater than $a";
?>
Output:-
Icca''''os!/phpigreat.php X

C (D loca!host/pi1p/greatphp

200 is greater than 100

2. Loop Structure in PHP


Loops are used to execute the same block of code repeatedly as long as certain
condition is satisfied.
Syntax For Loop
for(initialization;condition;increment)

Statements of loop;
TPS Information Technology (Science) 5-26 Server-Side Scripting (PHP)

Example
PHP program to print numbers from 1 to 10 using for loop.
Coding:
<?php
for($i=l;$i<=lO;$i++ )
. {
echo "<br> $i";

?>
Output:- x

~ CO (j) tocalhcst/php/tor.php

'}

3
.t
5
6
7
8
9
10

3. Foreach Loop
This loop works only for arrays.
Syntax:-
foreach($array as $value)
{
Statements of loop;

5.5 PHP String Functions


A string is a series of characters. Following are the predefined string functions in
PHP.
IPS Information Technology (Science) 5-27 Server-Side Scripting (PHP)

Function Description
strlenl) Returns the length of a string (i.e. total no. of characters)
str_ word_countO Counts the number of words in a string
strrevt) Reverses a string "

strposi) Searches for a specific text within a string and returns the
character position of the first match and if no match is found,
then it will return false
strJeplaceO Replaces some characters with some other characters in a string
substrt) Returns a part of a string
strtolowerO Converts a string to lowercase
substr_countO Counts the number of times a substring occurs ina string
ucwordst) Converts the first character of each word in a string to uppercase
triml) Removes whitespace and other predefined characters from both
sides of a string
Example
PHP program to demonstrate string functions
Coding:

<?php
$a="information technology";

echo "cbre-cbr> String is ".$a;

echo "<bre-cbr> Length of String is ".strlen($a);


echo "cbre-cbr> Words in String is ".stcword_count($a);

echo "cbrc-cbrc- Reverse of String is ".strrev($a);

echo "<bre-cbr> Position of 0 in String is ".strpos($a,"o");

echo "cbre-cbr> Replace Informtaion with Inform "str replace

("Information","Inform",$a);

echo "xbrc-cbr> Extract some part of String ".substr($a,5,3);

echo "xbrc-cbr> Converts String in lowercase ".strtolower($a);

echo "cbrc-cbr> Capitalize each word of string ".ucwords($a);

?>
TPS Information Technology (Science) 5-28 Server-Side Scripting (PHP)

Output:

lo::a'rost/phpistrin'SI,p,""p

C ill e
String is information technology

Length of String is 22

\\lords ill String is 2

Reverse of String is ygolonhcer noitamrefni

Position of 0 in String IS 3

Replace Informtaicn 'with Inform information technology

Extract some part of String mat

Converts Stringin Iowercase information technology

Capitalize each word of string Information Technology

5.6 PHP Arrays


An array is a special variable, which can hold more than one value ata time.
Creating array in PHP :
In PHP, the arrayt) function is used to create an array.
Syntax is:-
$x=array(values)
There are three types of arrays:
Indexed Arrays: - Arrays with a numeric index. Index always start with zero (0).
Syntax is: - $x=(valuel,value2, ... value n)
PHP program to print the name of colors store in array variable.
Coding
<?php
$x=array("Red","Blue","Brown","Pink","Purple");
echo "Color names are -cbrc-":
TPS Information Technology (Science) 5-31 Server-Side Scripting (PHP)

Output:
li!il b~aihost/php/ms9,php X

C CD localhost/php/msg.php

\Ye1come to PHP

PHP Function Arguments


Information can be passed to function through arguments. Arguments are specified
after function name,
PHP program to print Name and Price of the product by passing arguments to
function.
Coding:
<?php
function pr($n,$price)
{
echo "Product name is $n and Price is $price <br>cbr>":

pr("N otebook" ,45);


pr("Calculator",400);
pr("Pen",30);
?>
TPS Information Teclmology (Science) 5-32 Server-Side Scripting (PHP)

Output:

f- C (i) locajhcst/php/fum.tionarqs.php "" '$ '£'1 e ;


Product name is Notebook and Price is 45

Product name is Calculator and Price is 400

PCOOUL""'t name is Pen and Price is 30

Returning Value:
To let a function return a value, return statement is used.
PHP program to print sum of two numbers using function.
Coding:

<Pphp

function sum(int $a,int $b)

$c=$a+$b;
return $c;

echo "10+20= " .sum(10,20). "<bre-cbr>":

echo "11+2= " .sum(11,2). "cbrc-cbr>":

echo "31+9= " .sum(31,9). "<bre-cbr>":

?>
TPS Information Technology (Science) 5-29 Server-Side Scripting (PHP)

foreach($x as $value)

echo "$value <br>":

?>
Output:

Color names are


Red
Bfue
Brov •.'n
Pink
Purple

Associative Arrays : - Arrays with named index. This uses name key instead of
index to identify record/value.
Syntax: - $x=(keyl>= valuel, key2>= value2, .... Key n>= value n)
PHP program to print the cost of product store in array variable.
Coding:
<?php
$p = array("Pen"=>"35", "Ball"=>"37", "eraser"=>"43");
echo "cbrc-Pen cost is ".$p['Pen'];
echo "<brc-Eraser cost is ".$p['eraser'];
?>
Output:

Pen cost is :35


Eraser cost is 43
TPS Information Technology (Science) 5-30 Server-Side Scripting (PHP)

Multi-dimensional Arrays: - Arrays containing one or more arrays.


PHP program to print the information about cars sold and cars in stock.
Coding:
<?php
$cars = array

array("Volvo",22,18) ,
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo $cars[O][O].": In stock: ".$cars[O][l].", sold: ".$cars[O][2].".<br>";
echo $cars[l][O].": In stock: ".$cars[l][l].", sold: ".$cars[1][2].".<br>";
echo $cars[2][O].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][O].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>

5.7 PHP User Defined Functions

A function is a block of statements that can be used repeatedly in a program.


Syntax is:
function functionnamet)

statements;

PHP program to print message "Welcome to PHP" using function.


Coding:
<?php
function msgt) / / declaring a function
{
echo "Welcome to PHP"; j /defining function

msgt); / / calling function


?>
TPS Information Technology (Science) 5-33 Server-Side Scripting (PHP)

Output:

10+20=30

11+2= 13

31+9=40

5.8 PHP Form Handling

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.html

<html>

<body>
<form action="add.php" method =post">

Enter First Number

<input type='fext" name="tl"><br><br>

Enter Second Number


<input type="text" name=TZ'c-cbrc-cbr>

<input type="submit" valuee.'Add">

</form>

</body>
</html>
TPS Information Technology (Science) 5-34 Server-Side Scripting (PHP)

add.php
<?php
$tl=$_ GET["tl "];
$t2=$_GET["t2"];
$c=$tl +$t2;
echo "cbrc-cbr> Sum of two numbers is $c";
?>
Output:

The PHP superglobals $_GET and $_POST are used to collect form data.
GETvsPOST
Both GET and POST are treated as superglobals which means that they are always
accessible regardless of scope. It can be accessed from any function, class or file
without having to do anything special.
Example to create admission form for student. Accept name and gender from the
student. Create a college database and create table student having the fields name
and gender. .
Steps to create database and Table:
1) For database creation ~ open postgres sql terminal=-s create database college; and
press enter.
2) For Table creation -~ create table student(name text, gender Boolean); press enter,
table will be created.
TPS Information Technology (Science) 5-35 Server-Side Scripting (PHP)

Now type code inadmission.php


<!DOCTYPEhtml>
<html>
<body>
<hl align="center">Addmission Form</hl>
<form method=Ipost''>
<labels-Enter Name-c/Iabel>
<input type='fext" name="name" id="id_name"><br><br>
<label>Gender</label>
<input type="radio" name="gender" id="id-sender" valueemale'c-Malecbro-cbr>
<input type="radio" name="gender" id="id_gender" value="female">FeMale<br><br>
<input typeeradio'' name=gender" id="id_gender" valuee'tother'c-Other-cbrc-cbr>
<input type="submit" name="submit" valueesubmit" id="submit">
</form>
</body>
-c/html»
<?php
$servername="pgsql:host=localhost;dbname=college";.
$username="postgres";
$password ="laxmi";
$conn=new PDO($servernarrie,$username,$password);
if(isset($~POST['submit']) )

$name=$_POST["name"];
$gender=$_POST["gender"];
$sql="INSERT INTO student(name,gender) VALUES (,".$name."','".$gender.''')'';
$conn ->exec($sql);
echo "New record added successfully";

?>
TPS Information Technology (Science) 5-36 Server-Side Scripting (PHP)

5.2 Server Side Scripting

1. Explain Server Side Scripting?


Ans.:
(i) A server is a computer system that serves as a central control of data and programs
shared by clients.
(ii) The server side environment that runs a scripting language is termed as web server.
(iii)A user's request is fulfilled by running a script directly on the web server.
(iv) It is used to provide interactive web sites.
(v) Programming language for server side programming are PHP, Python JSP.

5.3 Features of PHP

2. Explain features of PHP (any 5).


Ans. PHP is most popular and frequently used worldwide server side scripting
language. Following are features of PHP :
(i) Simple: It is very simple and easy to use, as compared to other scripting languages.
(ii) Interpreted: It is an interpreted language, i.e. there is no need for compilation.
(Hi) Faster: It is faster than other scripting language e.g. JSP and ASP.
(iv) Open Source: Open source means you need not pay for use of PHP. You can freely
download and use.
(v) Platform Independent: PHP code will be run on every platform, Linux, Unix, Mac
as x, Windows.

I 5.4 First sample code of PHP

3. Explain how to save and execute PHP program.


Ans.:
(i) Type the PHP code using any text editor (Notepad).
(ii) Save with .php extension in appropriate folder.
(iii) Go to browser and type http://localhost/php/first.php
4. What is a variable?
Ans.:
(i) Variable is a symbol or name that contains value.
(ii) Variables are used for storing values such as numbers or characters,
(iii) The stored values can be used in any part of the program.
TPS Information Technology (Science) 5-37 Server-Side Scripting (PHP)

5. What are the rules for declaring PHP variables?


Ans.:
Following are the rules for declaring variables in PHP:-
(i) A variable starts with the $ sign, followed by the name of the variable
(ii) A variable name must start with a letter or the underscore character
(Hi) A variable name cannot start with a number
(iv) A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _)
(v) Variable names are case-sensitive ($age and $AGE are two different variables)
6. Explain variable scopes in PHP.
Ans.:
There are three different variables scopes in PHP :
• local
• global
• static
(i) A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function. ' '
(ii) A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function.
(iii) If we want a local variable not to be deleted then we have to use of static keyboard.
7. Explain PHP data types.
Ans. :
(i) String: - A string is a sequence of characters. A string can be any text inside quotes.
You can use single or double quotes: .
(ii) Integer:- An integer data type is a non-decimal number between -2,147,483,648 and
2,147,483,647.
(iii) Float (floating point numbers) :- A float (floating point number) is a number with a
decimal point or a number in exponential form.
{iv) Boolean: A Boolean represents two possible states: TRUE or FALSE.
(v) Array.- An array stores multiple values in one single variable.
(vi) NULL:- Null is a special data type' which can have only one value: NULL. A
variable of data type NULL is a variable that has no value assigned to it.
8. Explain Single line and multi line comments in PHP.
Ans.:
(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.
TPS Information Technology (Science) 5-38 Server-Side Scripting (PHP)

(iii) PHP supports several ways of commenting: Single line comment and multi line
comment.
(iv) Single line comment can be written in two ways, they are :-
/ / This is a single-line comment
# This is also a single-line comment
(v) Multi line comment can be written as.-
/* This is a multiple-lines comment block that spans over multiple lines */
9. Explain PHP If statement with syntax.
Ans.:
if statement - executes some code if one condition is true.
Syntax of if statement is
if (condition) {
code to be executed if condition is true;

10. Explain PHP If else statement with syntax.


Ans.:
The if...else statement executes some code if a condition is true and another code if
that condition is false.
Syntax of if else is
if (condition) {
code to be executed if condition is true;
}else {
code to be executed if condition is false;

11. Explain PHP for loop with syntax.


Ans.:
(i) Loops are used to execute the same block of code again and again, as long as a
certain condition is true.
(ii) The for loop is used when you know in advance how many times the script should
run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed for each iteration;
TPS Information Technology (Science) 5-39 Server-Side Scripting (PHP)

12. Explain PHP foreach with syntax.


Ans.:
The foreach loop works only on arrays, and is used to loop through each key /value
pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}

5.5 PHP String 'Functions

13. Explain String functions used in PHP (any 5).


Function Description
strleru) Returns the length of a string (i.e. total no. of characters)
str_ word_countO Counts the number of words in a string
strrevt) Reverses a string
strpost) Searches for a specific text within a string and returns the
character position of the first match and if no match is found,
then it will return false
str replacet) Replaces some characters with some other characters in a string

5.6 PHP Arrays

14. Explain how to create arrays in PHP with syntax.


Ans.:
(i) An array stores multiple values in one single variable
(ii) In PHP the arrayt) function is used to create an array.
(iii) Syntax to create array is :- $a=array(value)
15. Explain types of Arrays in PHP.
Ans.:
In PHP, there are three types of arrays:
(i) Indexed arrays - Arrays with a numeric index. For example.- $cars = array("Volvo",
"BMW", "Toyota");
(ii) Associative arrays - Arrays with named keys. For example :- $age =
array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
TPS Information Technology (Science) 5-40 Server-Side Scripting (PHP)

(iii) Multidimensional arrays - Arrays containing one or more arrays. Fro example :-
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);

5.7 PHP User Defined Functions

16. Explain how to declare user defined function in PHP.


Ans.:
(i) A function is a block of statements that can be used repeatedly in a program.
(ii) A function will not execute automatically when a page loads.
(iii) A function will be executed by a call to the function.
(iv) A user-defined function declaration starts with the word function:
Syntax
function functionNameO {
code to be executed;

17. Explain PHP function arguments.


Ans.:
(i) Information can be passed to functions through arguments. An argument is just like
a variable.
(ii) Arguments are specified after the function name, inside the parentheses. You can
add as many arguments as you want, just separate them with a comma.
(iii) Example is :-

<?php
function familyName($fname) {
echo "$fname Refsnes.cbr>":

familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familylvamei''Borge''):
?>
TPS Information Technology (Science) 5-41 Server-Side Scripting (PHP)

5.8 PHP Form Handling

18. Explain difference between GET and POST.


Ans.:
(i) Both GET and POST are treated as superglobals which means that they are always
accessible regardless of scope.

(ii) It can be accessed from any function, class or file without. having to do anything
special.

(iii) $_GET is an array of variables passed via the URL parameters.

(iv) $_POST is an array of variables passed via the HTTP POST method.

19. Explain issett) method in PHP.


Ans.:
(i) issetf) method is used in PHP to check whether variable has value or not.

(ii) This helps us to know if the button is clicked or not.

20. Short note on Cookies and d Session in PHP.


Ans.:
(a) Cockiest-

(i) A cookie is often used to identify a user.

(ii) A cookie is a small file that the server embeds on the user's computer.

(iii) Each time the same computer requests a page with a browser, it will send the
cookie too.

(iv) With PHP, you can both create and retrieve cookie values.

(b)· Session
(i) Session is used to store user information on server to track user activities.

(ii) It helps in web application to maintain user information on all the pages.

(iii) For example, if you login to gmail account, the session helps to access youtube
account also.
TPS Information Technology (Science) 5-42 Server-Side Scripting (PHP)

1. Write a PHP code which calculates and prints addition, subtraction, multiplication,
division of two numbers using form.
Ans.:
Coding :-
<html>
<body>
<form method=post''>
Enter First Number
<input type="text" name='first'c-cbr>-cbr>
Enter Second Number
<input type='fext'' name="second"><br><br> .
<input type="submit" name='tsubmit" value="Submit">
</form>
</body>
</html>
<?php
if(isset($_POST[' submit'])
{
$first = $_POST['first'];
$second = $_POST['second'];
$sum = $first+$second;
$sub = $first-$second;
$mul = $first*$second;
$div = $first/$second;
echo" <br><br> The Addition is: ".$sum;
echo" <bre-cbr> The Subtraction is: ".$sub;
echo "<brc-cbre-The Multiplication is: ".$mul;
echo "<brc-cbre-The Division is: ".$div;

?>
TPS Information Technology (Science) 5-43 Server-Side Scripting (PHP)

2. Write a PHP code which calculates and prints area and circumference of a circle.
Hint(area=3.14x r- and c=2x3.14xr) using form.
Ans.:
Coding:

<html>
<body>
<form method="post">
Enter Radius
<input type='fext'' nameeradius'c-cbrc-cbrc-
<input type='eubmit'' name="submit" value='Submit"> ..
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) )
{
$p=3.14;
$radius = $_POST['radius'];
$a= $p*$radius*$radius;
$c == $p*2*$radius;
echo "<brc-xbrc-The Area of circle is: ".$a;
echo "<bre-cbrc-The Circumference of circle is: ".$c;

?>
3. Write a PHP code which calculates square of a number using form.
Ans.:
Coding:
<html>
<body>
<form method=rpost">
Enter a Number
<input type="text" nameesq'c-cbrc-cbro
<input type="submit" nameesubmit" value='Sqaure">
TPS Information Technology (Science) 5-44 Server-Side Scripting (PHP)

</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$sq = $_POST['sq'];
$s=$sq*$sq;
echo "<brc-cbrx'Ihe Square of number is: ".$s;

?>
4. Write a PHP code which calculates cube of a number using form.
Ans.:
<html>
<body>
<form method="post">
Enter a Number
<input type="text" name='tsq'c-cbrc-cbr>
<input type="submit" name="submit" value="Cube">
< I form>
</body>
</html>
<?php
if(isset( $_POST[' submit']) )
{
$sq = $_POST['sq'];
$s=$sq*$sq*$sq;
echo "<brc-cbre-The Cube of number is: ".$s;

?>
TPS Information Technology (Science) 5-45 Server-Side Scripting (PHP)

5. Write a PHP code to check and print greatest among two numbers using form an
function.
Ans.:
Coding:
<html>
<body>
dorm method=post">
Enter First Number
<input type='fext" name='f'c-cbrc-cbr>
Enter Second Number
<input type="text" name="s"><br><br>
<input type="submit" name="submit" value="Greatest">
</form>
.</body>
</html>
<?php
function grtt)
{
if(isset($_POST['submit'J))
{
$f = $_POST['f'];
$s = $_POST['s'];
if($f>$s)
echo" <br><br>The greater number is " .$f;
else
echo" <bre-xbr> The greater number is " .$s;

grtt):
?>
TPS Information Technology (Science) 5-46 Server-Side Scripting (PHP)

6. Write a PHP code to check and print greatest among three numbers using form.
Ans.:
Coding:
<html>
<body>
<form method=l'post">
Enter First Number
<input type='fext" name="f"><br><br>
Enter Second Number
<input type="text" namees'c-cbro-cbr>
Enter Third Number
<input type="text" namev'f'c-cbro-cbr>
<input type="submit" name="submit" value="Greatest 3">
</form>
</body>
</htm1>
<?php
function grtO

if(isset($_POST['submit'])
{
$f = $_POST['£'];
$s = $_POST['s'];
$t = $_POST['t'];
if($f>$s && $f>$t)
echo "<bre-cbrc-The greater number is" .$f;
else

if($s>$f && $s>$t)


echo "<br>«br> The greater number is " .$s;
else

if($t>$f && $t>$s)


echo "cbrc--cbrc-The greater number is" .$t;
TPS Information Technology (Science) 5-47 Server-Side Scripting (PHP)

grtt):
?>
7. Write a PHP code to check and print whether the number is Odd or Even using
form.
Ans.:
Coding:
<html>
<body>
<form method=post">
Enter a Number
<input type='fext" name=T'e-cbr>«br>
<input type=Hsubmit" name='tsubmit'' value='Even Odd">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])
{
$f = $_POST['£,];
if($f%2==O)
echo "<brc-xbrc-The number is Even number";
else
echo "<bre-cbr> The number is Odd number";

?> ..
TPS Information Technology (Science) 5-48 Server-Side Scripting (PHP)

8. Write a PHP code to check and print whether the number is positive, negative or
Zero using form.
Ans.:
Coding:
-chtml»
<body>
<form method="post">
Enter a Number
<input typee'fexr" namee'f'c-cbre-cbr»
<input type="submit" name="submit" value="Positive Negative Zero">
</form>
</body>
</html>
<?php
if(isset($_POST['submit'D)
{
$f = $_POST['f'];
if($f>O)
echo "<bre-cbrc-Thenumber is Positive number";
else

if($f<O)
echo "<brc-cbre-Thenumber is Negative number";
else
echo "<bre-cbrc-Thenumber is Zero";

?>
9. Write a PHP code to print even numbers between 1-20.
Ans.:
Coding:

<?php I
for($i=2;$i<=20;$i=$i+2)
~{~---<
TPS Information Technology (Science) 5-49 Server-Side Scripting (PHP)

echo "<br>".$i;

?> I
10. Write a PHP code to input a number and print the factorial of a number.
Ans.:
Coding:
<html>
<body>
<form method=Ipost">
Enter a Number
<input type='fext" name="f"><br><br>
<input type="submit" name=t'submit" value="Factorial">
</form>
</body>
</html>
<?php
$fc=l;
if(isset($_POST['submit']) )
{
$f = $_POST['f];
for($i=$f;$i>=l;$i-- )
{
$fc=$fc*$i;

echo "cbrc-cbre-The Factorial is ".$fc;

?>
11. Write a PHP code to print multiplication table of a number.
Ans.:
Coding:
<html>
<body>
<form method;::"post">
TPS Information Technology (Science) 5-50 Server-Side Scripting (PHP)

Enter a Number
<input type="text" name=T'c--cbrc-cbr>
<input type="submit" name=rsubmit'' value=Table">
</form>
</body>
</html>
<Pphp
if(isset($_POST['submit'D)
{
$f ==$_POST['£,];
for($i=1;$i<=10;$i++ )
{
$m=$f*$i;
echo "<brc--cbr> ".$f. "x".$i."=".$m;
J

?>
12. Write a PHP code to check whether the number is prime number or not.
Ans.:
Coding:
<html>
<body>
<form method='tpost">
Enter a Number
<input type="text" namee'f'c-cbrc-cbrc-
<input type="submit" nameesubmit" value="Prime number">
</form>
</body>
</html>
<?php
$p==l;
if(isset($ _POST['submit'D)
TPS Information Technology (Science) 5-51 Server-Side Scripting (PHP)

for($i=2;$i <$f;$i++)
{
H($f%$i ==0)
$p=O;
}
H($p==l)
echo "<brc-cbre-Number is Prime Number";
else
echo "<bre-cbrc-Number is not a Prime Number";

?>
13. Write a PHP code to count the number of words in the given string.
Ans.:
Coding:
<?php
$a="Hypertext Preprocessor";
echo "<bre-cbr> String is ".$a;
echo "<bre-cbr> Words in String is "strword countrsa):
?>
14. Create a website with two PHP webpage in which each webpage is connected. The
first page of the website contains two form fields for taking 'name' and 'password'
from users. On onclick event, details of forms should be displayed on second web
page.
Ans.:
Coding:
Form.html
<html>
<body>
<form action="wc.php" method="post">
Enter Name
<input type="text" namee'tname'c-cbrc-cbr»
Enter Password
<input type="password" name="pass"><br><br>
<input type="submit" name="submit" value='Submit''>
TPS Information Technology (Science) 5-52 Server-Side Scripting (PHP)

</form>
</body>
</html>
wc.php
<html>
<body>
Welcome
<?php
echo $_POST["name"]; 't» <bre-cbr>
Your Password is
<Pphp
echo $_POST["pass"];
?>

Fill in the blanks

1. PHP is scripting language.


Ans. : Server
2. PHPis language i.e. there is no need for compilation.
Ans, : interpreted
3. A variable starts with sign followed by variable name.
Ans. :'$
4. An is a variable, which can hold more than one value at a time.
Ans.: array
5. Information can be passed to functions through _
Ans. : arguments

State TruelFalse

1. PHP is platform dependent scripting language.


Ans.: False
2. $_POST is an array of variables passed via the URL parameters.
Ans.: False
TPS Information Technology (Science) 5-53 Server-Side Scripting (PHP)

3. . A Function is a block of statements that can be used repeatedly in a program.


Ans.: True
4. PHP cannot be embedded along, with HTMLtags.
Ans.: False
5. GET should NEVER be used for sending sensitive information.
Ans. :True

Multiple Choice Question (1 correct)

1. The program file of PHP have extension.


(a) .asp (b) .php
(c) .js (d) .txt
Ans.: (b)
2. A variable declared a function has global scope.
(a) outside (b) anywhere
(c) inside (d) none
Ans.: (a)
3. The ____ function returns a part of a string.
(a) trimt) (b) ucwordst)
(c) substrt) (d) strpost)
Ans.: (c)

Multiple Choice Question. (2 correct)

1. The ~ __ & are valid datatype ill PHP.


(a) Double (b) Varchar
(c) Integer (d) Array (e) BigInt
Ans. : (c,d)
2. Single line comment in PHP is possible using _
(a) II (b) 1* *1
(c) # (d) <!> (e) $
Ans.: (a,b)
TPS Information Technology (Science) 5-54 Server-Side Scripting (PHP)

Multiple Choice Question. (3 correct)


1. In PHP, three types of arrays are _
(a) Indexed (b) Simple
(c) Associative (d) Multidimensional
(e) Complex (f) General
Ans. : (a,c,d)
2. The scope of variable can be _
(a) local (b) global (c) universal
(d) static (e) final (f) outside
Ans. : (a,b,d)

Brief Questions
1. Explain any two features of PHP?
Ans.: Refer answer the following Ql 5.3
2. What are the rules to declare variable inPHP?
Ans.: Refer answer the following Q3 5.4
3. What is server side scripting?
. Ans. : Refer answer the following Ql 5.2
4. List the supported datatypes in PHP
Ans. : Refer answer the following Q5 5.4
5. Explain any two string manipulation function.
Ans. : Refer answer the following Ql 5.5

Write Programs for the following


1. Write a PHP code which calculates square of any number using form.
Ans.: Refer PHP Extra programs for practice Program No 3
2. Write a PHPcode to count no. of words in the given string.
Ans.: Refer PHP Extra programs for practice Program No 13
3. Create a website with two PHP webpage in which each webpage is connected.
The first page of the website contains two form fields for taking 'name' and
'password' from users. On onclick event, details of forms should be displayed on
second webpage.
Ans.: Refer PHP Extra programs for practice Program No 14

ODD

You might also like