0% found this document useful (0 votes)
2 views

To become familiar with functions and form handling and file handling in PHP

The document outlines a practical lab session for a Web Engineering course focused on PHP functions and form handling. It covers the definition and usage of functions, superglobals, and the differences between GET and POST methods for form data submission. Additionally, it includes lab tasks that require students to implement PHP code for various functions and form handling scenarios.

Uploaded by

Tabish Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

To become familiar with functions and form handling and file handling in PHP

The document outlines a practical lab session for a Web Engineering course focused on PHP functions and form handling. It covers the definition and usage of functions, superglobals, and the differences between GET and POST methods for form data submission. Additionally, it includes lab tasks that require students to implement PHP code for various functions and form handling scenarios.

Uploaded by

Tabish Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Software Engineering

Mehran University of Engineering and Technology, Jamshoro

Course: SW417– Web Engineering


Instructor Mr. Mansoor Samo Practical/Lab No. 10
Date CLOs CLO-2
Signature Assessment Score 1 Mark

Topic To become familiar with functions and form handling in PHP


Objectives - To learn functions
- To learn handling form data using GET and POST
- To learn Superglobals

Lab Discussion: Theoretical concepts and Procedural steps


Part A: Functions: Function is a block of code that performs some task. Functions
help organize the code in a better manner and enhance code reusability. There are a
number of built-in functions in PHP while user defined functions can be created as
follows:

function functionName() {
code to be executed;
}

A user defined function declaration starts with the word "function" followed by any
functionName followed by parenthesis () and finally function body in curly braces {}.

Note: User defined PHP functions are case sensitive while the built-in PHP
functions are case-insensitive.

Sample function code is given below:

Functions with Arguments:


Information can be passed to functions through arguments. An argument is just like a
variable. Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want separated by commas.
Functions Returning Values:

Function might return some values to its caller using the return keyword as shown
in the example below:

Function Default Argument Values:

Function arguments can be given default values which are printed if the
arguments are not supplied when the function is called.
By definition, Function overloading is not possible in PHP but a hack to achieve
function overloading is to give arguments with default values. Arguments with default
values do not give any error if left empty.
Part B: Form Handling & SuperGlobals:

Superglobals: PHP superglobals are built-in associative arrays that are always
available in all scopes throughout the PHP code. There is no need to initialize or declare
them i.e. they can be accessed from any function, class or file without having to do
anything special. The PHP superglobals are as follows:

 $GLOBALS
 $_SERVER
 $_REQUEST
 $_POST
 $_GET
 $_FILES
 $_ENV
 $_COOKIE
 $_SESSION

Form Handling:

The data from an HTML form can be sent to the server using any server side language.
This data can be sent to the server using either GET method or the POST method. To
access the HTML form data sent via any of the above methods using PHP we follow the
steps given below:

 Create an HTML form with some input controls to collect data from the user and
a submit button to submit the data to the server. Give the action attribute of the
form the name of the php script in which the data will be accessed and in the
method attribute describe the mechanism (GET or POST) which will be used to
send the data from your HTML form to the PHP code.

 Create a PHP script welcome.php (given in action attribute of the form above)
to access the data sent via the HTML form above, Since the data is sent via the
post method of the HTML form, it will be retrieved using the PHP $_POST
superglobal array. Similarly, if it would have been sent via
the get method of the form, it would have been retrieved via the $_GET
superglobal array.

Output: To run the form and the script, place both the files in the htdocs folder of the
server and run them using the browser.

$_GET & $_POST Superglobals:

$_POST $_GET
PHP $_POST is widely used to collect PHP $_GET can also be used to collect
form data after submitting an HTML form data after submitting an HTML
form with method="post". form with method="get".
Information is invisible to other (all Information sent from a form with the
names/values are embedded within GET method is visible to every one(all
the body of the HTTP request) variable names and values are
displayed in the URL).
No limits on the amount of information to Limits on the amount of information to
send. send. The limitation is about 2000
characters.

More Reliable Less Reliable


Slow Fast
Uses the HTTP POST mechanism to Uses the query string in the URL to
send the data send the data.

Lab Tasks

Part A: Write PHP code to implement various functions


according to the requirements given below:

1. Take a number as parameter and returns its factorial.

2. Create arithmetic functions with two and three variables


as arguments using function overloading that returns
addition, subtraction, multiplication and division of two
or three numbers supplied as arguments.

3. Create a function that takes any number as input (with


multiple digits) and converts them into words. For
example:
Number: 534678
Output: fivethreefoursixseveneight

Part B: Write PHP code to collect data from various forms for
the following scenarios: (You may use arrays and functions
where necessary)

1. Create a phone directory program where user stores


the names and phone number in an array with the
help of html form.
Hint: Store the numbers and names into an associative
array in the PHP script.

Part C: Superglobals

 Create a PHP script that handles both GET and POST requests dynamically.
 Check if the request method is GET or POST using conditional statements.
 Use the appropriate super global variable ($_GET or $_POST) to retrieve form
data based on the request method.
 Process the form data and display the results accordingly.
 Test the script by accessing it through a web browser and submitting the form
with different request methods.

You might also like