7. Functions in PHP – II
Total Page:16
File Type:pdf, Size:1020Kb
7. Functions in PHP – II 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 ( ! ) Notice: Undefined variable: var in H:\wamp\www\PathshalaWAD\function\function localscope demo.php on line 12 Call Stack # Time Memory Function Location 1 0.0003 240416 {main}( ) ..\function localscope demo.php:0 The value of $var inside the function is: Page 1 of 7 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: ( ! ) Notice: Undefined variable: globalscope in H:\wamp\www\PathshalaWAD\function\function global scope demo.php on line 7 Call Stack # Time Memory Function Location 1 0.0004 239864 {main}( ) ..\function global scope demo.php:0 2 0.0004 240008 localscope( ) ..\function global scope demo.php:9 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 Page 2 of 7 //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']; } Page 3 of 7 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 ( ! ) Notice: Undefined variable: var in H:\wamp\www\PathshalaWAD\function\static scope of variable in function.php on line 14 Call Stack # Time Memory Function Location 1 0.0003 241632 {main}( ) ..\static scope of variable in function.php:0 The value of $var inside the function is: 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) Page 5 of 7 { 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. Function Description func_num_args() Returns a number which specifies number of arguments passed in to the function. func_get_args() Returns an array. The array has all the arguments which are passed to the function. func_get_arg($arg) Returns the argument at specify index $arg <?php function fact() { $count = func_num_args(); echo "<br> This function has $count arguments"; } Page 6 of 7 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. Function Description include [( ] $path [ ) ] This function includes and runs the specified file. However, If this function fails, it displays warning and continues the execution. The parentheses are optional for this function. include_once [ ) ] $path [ ) ] Same as include. The only difference is that include_once make sure that the specified file is include only once. require($path) This function works same as include but the difference is that, if this function fails, it causes a fatal error and stop the script execution. require_once ($path) Same as require. The only difference is that require_once make sure that the specified file is required only once. exit ([$status]) Exit the current php script. status $status is optional. If $status is specified, then this function display the status before exit the script. die ([$status]) Works same as the exit function. 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'); Page 7 of 7 .