0% found this document useful (0 votes)
15 views9 pages

WAD Unit-3

This document provides an overview of PHP functions and data handling, including user-defined functions, function arguments, and the header() function. It explains the advantages of using functions, such as code reusability and ease of understanding, and illustrates various examples of function usage, including passing arguments, default values, and returning values. Additionally, it details the header() function for sending raw HTTP headers and its applications in redirecting users and controlling caching.

Uploaded by

ROSELIN ANDREW
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

WAD Unit-3

This document provides an overview of PHP functions and data handling, including user-defined functions, function arguments, and the header() function. It explains the advantages of using functions, such as code reusability and ease of understanding, and illustrates various examples of function usage, including passing arguments, default values, and returning values. Additionally, it details the header() function for sending raw HTTP headers and its applications in redirecting users and controlling caching.

Uploaded by

ROSELIN ANDREW
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Unit-3.

0: Function and Data


Handling
3.1 creating user defined function
3.1.1 without arguments
3.1.2arguments with default value
3.1.3 function returning value
3.2 header()function
3.2.1 http redirect
3.2.2 content - disposition
3.3 Data Handing
3.3.1 superglobals in php :$globals,
$_server, $_request, $_post,
$_get, $_cookie, $_session
3.3.2 need of superglobals
PHP Functions
PHP function is a piece of code that can be reused many times. It can take input as
argument list and return value. There are thousands of built-in functions in PHP.

Advantage of PHP Functions


Code Reusability: PHP functions are defined only once and can be invoked many
times, like in other programming languages.

Less Code: It saves a lot of code because you don't need to write the logic many times.
By the use of function, you can write the logic only once and reuse it.

Easy to understand: PHP functions separate the programming logic. So it is easier to


understand the flow of the application because every logic is divided in the form of
functions.

PHP User-defined Functions


We can declare and call user-defined functions easily. Let's see the syntax to declare
user-defined functions.

Syntax
1. function functionname(){
2. //code to be executed
3. }

PHP Functions Example


File: function1.php
1. <?php
2. function sayHello(){
3. echo "Hello PHP Function";
4. }
5. sayHello();//calling function
6. ?>
Output:

Hello PHP Function

PHP Function Arguments


We can pass the information in PHP function through arguments which is separated by
comma.
PHP supports Call by Value (default), Call by Reference, Default argument
values and Variable-length argument list.

Let's see the example to pass single argument in PHP function.

File: functionarg.php
1. <?php
2. function sayHello($name){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Sonoo");
6. sayHello("Vimal");
7. sayHello("John");
8. ?>
Output:

Hello Sonoo
Hello Vimal
Hello John

File: functionarg2.php

1. <?php
2. function sayHello($name,$age){
3. echo "Hello $name, you are $age years old<br/>";
4. }
5. sayHello("Sonoo",27);
6. sayHello("Vimal",29);
7. sayHello("John",23);
8. ?>
Output:

Hello Sonoo, you are 27 years old


Hello Vimal, you are 29 years old
Hello John, you are 23 years old

PHP Call By Reference


Value passed to the function doesn't modify the actual value by default (call by value).
But we can do so by passing value as a reference.

By default, value passed to the function is call by value. To pass value as a reference,
you need to use ampersand (&) symbol before the argument name.

Let's see a simple example of call by reference in PHP.


File: functionref.php
1. <?php
2. function adder(&$str2)
3. {
4. $str2 .= 'Call By Reference';
5. }
6. $str = 'Hello ';
7. adder($str);
8. echo $str;
9. ?>
Output:

Hello Call By Reference

PHP Function: Default Argument Value


We can specify a default argument value in function. While calling PHP function if you
don't specify any argument, it will take the default argument. Let's see a simple example
of using default argument value in PHP function.

File: functiondefaultarg.php
1. <?php
2. function sayHello($name="Sonoo"){
3. echo "Hello $name<br/>";
4. }
5. sayHello("Rajesh");
6. sayHello();//passing no value
7. sayHello("John");
8. ?>
Output:

Hello Rajesh
Hello Sonoo
Hello John

PHP Function: Returning Value


Let's see an example of PHP function that returns value.

File: functiondefaultarg.php
1. <?php
2. function cube($n){
3. return $n*$n*$n;
4. }
5. echo "Cube of 3 is: ".cube(3);
6. ?>
Output:

Cube of 3 is: 27

PHP header()
The header() is a pre-defined network function of PHP, which sends a raw HTTP
header to a client. One important point to be noted about the header() function is that it
must be called before sending any actual output.

The header() function sends an HTTP header in raw form to the client or browser.
Before sending any other output, the HTTP functions manipulate the information sent by
the web-server to the client or browser.

Syntax
1. void header (string $header, boolean $replace = TRUE, int $http_response_code)

Parameters
The header() function accepts three parameters, which are discussed below in detail:

$header (mandatory)
The header parameter contains the header string to send. This function has two types of
special header calls.

The first header starts with an "HTTP/" string, which locates the HTTP status code for
sending.

The second special case of header starts with "Location:" header. It not only sends
back the header to the browser, but it also provides a REDIRECT (302) status code to
the browser, until the 201 or 3xx status code has already been set.

$replace (optional)
This parameter is used for specifying whether a previous same header should be
replaced by the header or add another header of same type. The $replace is
a boolean type optional parameter.

The default value is TRUE, which means that it replaces the previous same header. But
you can bind several headers of same type if FALSE is passed as second argument.

http_response_code (optional)
The $http_response_code is an optional parameter, which forces the HTTP response
code to a specified value.
Note: The http_response_code parameter has an effect if the header is not blank.

Return Value
The PHP header() function does not return any value.

Changes
After PHP version 5.1.2, this function stops sending more than one header to prevent
the header injection attacks. It allows only one header at a time.

Uses

o It changes the page location.


o It sets the time zone.
o It sends the STOP status.
o This function sets the caching control.
o It initiates the force download.

Examples
With the help of below examples, you can understand the actual working of header()
function in runtime environment-

Example 1: Redirecting browser


The following code will redirect your user to some another page.

1. <?php
2. // This will redirect the user to the new location
3. header('Location: http://www.javatpoint.com/');
4.
5. //The below code will not execute after header
6. exit;
7. ?>
Output

It will redirect to the new URL location, which is given in header() function of the above
program, i.e., www.javatpoint.com. If any line of code is written after the header(), it will
not execute.
Example 2: Redirection interval
The following code will redirect your user to another page after 10 seconds.

1. <?php
2. // This will redirect after 10 seconds
3. header('Refresh: 10; url = http://www.javatpoint.com/');
4. exit;
5. ?>
Output

The output will be same as the example 1, but it will take 10 seconds to load.

Note: If any line of code is written after the header() function, it will not execute.

Example 3: Don't cache pages


By using the following code, you can prevent the browser to cache pages.

1. <?php
2. // PHP program to describes header function
3.
4. // Set a past date
5. header("Expires: Tue, 03 March 2001 04:50:34 GMT");
6. header("Cache-Control: no-cache");
7. header("Pragma: no-cache");
8. ?>
9. <html>
10. <body>
11. <p>Hello Javatpoint!</p>
12.
13. <!-- PHP program to display
14. header list -->
15. <?php
16. print_r(headers_list());
17. ?>
18. </body>
19. </html>
Output

Hello Javatpoint!
Array (
[0] => X-Powered-By: PHP/7.3.13
[1] => Expires: Tue, 03 March 2001 04:50:34 GMT
[2] => Cache-Control: no-cache
[3] => Pragma: no-cache
)

Absolute URI
Absolute URI is required by some older clients, which includes the hostname, scheme,
and absolute path, whereas most contemporary clients accept relative URI as argument
to Location. To make absolute URI, you can
use $SERVER['PHP_SELF'], $SERVER['HTTP_HOST'], and dirname().

Example 4
Create two php files, one of which for containing header file code and another for
redirecting to a new page on the browser.

headercheck.php

1. <?php
2. $host = $_SERVER['HTTP_HOST'];
3. $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
4. $newpage = 'welcome.php';
5.
6. /* Redirect to a different page requested in the current directory*/
7. header("Location: http://$host$uri/$newpage");
8. exit;
9. ?>

welcome.php

1. <!-- welcome.php file redirect to new page -->


2. <html>
3. <head>
4. <title> Welcome page </title>
5. </head>
6. <body>
7. <h1> <center> Welcome to javaTpoint </center> </h1>
8. </body>
9. </html>
Output

You might also like