To become familiar with functions and form handling and file handling in PHP
To become familiar with functions and form handling and file handling in PHP
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.
Function might return some values to its caller using the return keyword as shown
in the example below:
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.
$_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.
Lab Tasks
Part B: Write PHP code to collect data from various forms for
the following scenarios: (You may use arrays and functions
where necessary)
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.