7 Functions in PHP – II

Hiren Joshi

epgp books

 

 

 

Scope of variables in Function

 

Scope of variable is the part of PHP script where the variable can be accessed or used. PHP supports three different scopes for a variable. These scopes are

 

1. Local

2. Global

3. Static

 

A variable declared within the function has local scope. That means this variable is only used within the function body. This variable is not used outside the function.

 

To demonstrate the concept, let us take an example.

 

//  local variable scope function localscope()

{

$var = 5;  //local scope

echo ‘<br> The value of $var inside the function is: ‘. $var;

}

localscope();  // call the function

//  using $var outside the function will generate an error echo ‘<br> The value of $var inside the function is: ‘. $var;

 

The output  will be:

 

The value of $var inside the function is: 5

 

The value of $var inside the function is:

 

If a variable is defined outside of the function, then the variable scope is global. By default, a global scope variable is only available to code that runs at global level. That means, it is not available inside a function. Following example demonstrate it.

 

<?php

//variable scope is global

$globalscope = 20;

//  local variable scope function localscope()

{

echo ‘<br> The value of global scope variable is :’.$globalscope;

}

localscope();  // call the function

// using $var outside the function will generate an error

echo ‘<br> The value of $globalscope outside the function is: ‘. $globalscope;

Then the output will be:

 

 

The value of global scope variable is :

 

The value of $globalscope outside the function is: 20

 

To access a global variable from within a function global statement is used. The global statement import a variable from global scope to local scope.

 

Let us understand it by example.

//global statement to access global variable in function

  //variable scope is global

$globalscope = 20;

//  local variable scope function localscope()

{

global $globalscope;

echo ‘<br> The value of global scope variable is :’.$globalscope;

}

localscope();  // call the function

// using $var outside the function will display its value

echo ‘<br> The value of $globalscope outside the function is: ‘. $globalscope;

 

The output will be:

 

The value of global scope variable is :20

 

The value of $globalscope outside the function is: 20

 

PHP stores all global level variables in $GLOBALS super global variable. In fact all super global variables are array. This array is an super global array like the $_POST and $_GET.

 

Following example shows.

//variable scope is global

$globalscope = 20;

echo ‘<br> The value of $globalscope outside the function is: ‘. $globalscope;

//   local variable scope function localscope()

{

echo ‘<br> The value of global scope variable is :’.$GLOBALS[‘globalscope’];

}

 

    localscope();    // call the function

// using $var outside the function will generate an error

echo ‘<br> The value of $globalscope outside the function is: ‘. $globalscope;

 

Then the output will be

The value of $globalscope outside the function is: 20

The value of global scope variable is :20

The value of $globalscope outside the function is: 20

 

Static

 

Generally when function is completed, all of its variable is wiped out. In case a function executes many times, each time the function start with fresh copy of variable and the previous value has no effect. Sometimes it can be required to not delete the local variable. This variable may be required to use for further task. Static keyword is used to retain the variable. Note that the scope of static is local. Static variable is assigned only with predetermined values.

 

//  static variable function localscope()

{

static $var = 5;  //local scope

echo ‘<br> The value of static variable $var inside the function is: ‘. $var; $var++;

}

    localscope();  // call the function

localscope(); // call the function

localscope();  // call the function

 

//  using $var outside the function will generate an error echo ‘<br> The value of $var inside the function is: ‘. $var;

Then the output will be

 

Page 4 of 7

    The value of static variable $var inside the function is: 5

 

The value of static variable $var inside the function is: 6

 

The value of static variable $var inside the function is: 7

 

 

Default value for Parameters

 

In PHP a function parameter can have default value. If the parameter has assigned default value, then this parameter becomes option parameter. When the function is called and the value is not passed for the default value parameter, then the function will take the default value. To set the default value of a parameter type parameter name then assignment operator and write the default value (parametername = value). The default value of parameter can only be a scalar value or an array of a scalar value. Scalar value is a string literal, a numeric literal, a Boolean value or Null. In other words, default parameter value cannot be an expression, a function call or an object.

 

function setgender($gender = ‘Male’)

{

echo ‘<br> Your gender is : ‘.$gender;

}

//call the function

setgender();

setgender(‘Female’);

 

The output will be

 

Your gender is : Male

Your gender is : Female

 

In case it is required to provide more complex value to the parameter, then set null value as the default value for the parameter. Then in the body of function, use the function isset to test whether the parameter is set or not. As the parameter is not set, the parameter can be set by an expression.

 

function setdate($date = Null)

{

if(!isset($date))

{

$date = date(‘d-M-Y’);

}

echo ‘<br> The date is : ‘.$date;

}

//call the function

setdate();

setdate(’25-Jun-2015′);

Then the output will be

The date is : 15-Jun-2015

The date is : 25-Jun-2015

 

Variable Length Parameter list

 

Sometimes you may require to work with variable parameters. As the parameters are not known it will be difficult to write code for that. PHP has in-built functions which support you to work with variable length parameters.

 

   <?php
function fact()
{

$count = func_num_args();

echo “<br> This function has $count arguments”;

}

fact(1,2,3);  //3 arguments

fact(1);     //1 arguments

fact(); // 0 arguments

fact(‘A’,’B’,’C’,’D’,’E’,’F’,’G’); // 7 Arguments

 

Using reuire() and Include() functions

 

PHP provides very useful function to pass the control to another webpage. These functions are require() and include(). Require() or include() function load a file into PHP script. The file which is loading can anything normally a PHP script contains like PHP statements, text, HTML tags, PHP functions or PHP classes. These statements work very similar to the server side includes statement provided by many website. Require and include have their variant require_once and include_once. Following table shows function name with their description.

 

 

 

The following code snippet shows use of include and require.

 

include ‘show.php’; //parentheses are optional
inlcude (‘show.php’); // the file index.php in current directory
require(‘./validation.php’); // the file validation.php in current directory
include(‘view/header.html’); // the file in view folder
inlcude(‘../table.php’); // the file is one directory up

exit;

exit();

exit(‘Error in Database Connectivity’);

you can view video on Functions in PHP – II

References and suggested additional reading:

 

1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,

2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress

3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,

4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O’Reilly Media

5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP 5, Wrox

6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible

7. Joel Murach, Ray Harris: Murach’s PHP and MySQL, Shroff/Murach

8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML/Javascript/DHTML/PHP , BPB Publications

9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams

10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education

11. http://www.php.net

12. http://www.w3schools.com/

13. http://www.tutorialspoint.com/