{"id":129,"date":"2018-07-12T09:45:05","date_gmt":"2018-07-12T09:45:05","guid":{"rendered":"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=129"},"modified":"2018-12-07T09:29:45","modified_gmt":"2018-12-07T09:29:45","slug":"functions-in-php-ii","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/chapter\/functions-in-php-ii\/","title":{"rendered":"Functions in PHP \u2013 II"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/JKFEyG8zsqU\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Scope of variables in Function<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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<\/p>\r\n&nbsp;\r\n\r\n1. Local\r\n\r\n2. Global\r\n\r\n3. Static\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nTo demonstrate the concept, let us take an example.\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 local variable scope function localscope()\r\n\r\n{\r\n\r\n$var = 5;\u00a0 \/\/local scope\r\n\r\necho '&lt;br&gt; The value of $var inside the function is: '. $var;\r\n\r\n}\r\n\r\nlocalscope();\u00a0 \/\/ call the function\r\n\r\n\/\/\u00a0 using $var outside the function will generate an error echo '&lt;br&gt; The value of $var inside the function is: '. $var;\r\n\r\n&nbsp;\r\n\r\nThe output\u00a0 will be:\r\n\r\n&nbsp;\r\n\r\nThe value of $var inside the function is: 5\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-132 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83.png\" alt=\"\" width=\"642\" height=\"145\" \/>\r\n\r\nThe value of $var inside the function is:\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\n&lt;?php\r\n\r\n\/\/variable scope is global\r\n\r\n$globalscope = 20;\r\n\r\n\/\/\u00a0 local variable scope function localscope()\r\n\r\n{\r\n\r\necho '&lt;br&gt; The value of global scope variable is :'.$globalscope;\r\n\r\n}\r\n\r\nlocalscope();\u00a0 \/\/ call the function\r\n\r\n\/\/ using $var outside the function will generate an error\r\n\r\necho '&lt;br&gt; The value of $globalscope outside the function is: '. $globalscope;\r\n\r\nThen the output will be:\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-133 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84.png\" alt=\"\" width=\"649\" height=\"167\" \/>\r\n\r\n&nbsp;\r\n\r\nThe value of global scope variable is :\r\n\r\n&nbsp;\r\n\r\nThe value of $globalscope outside the function is: 20\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nLet us understand it by example.\r\n\r\n\/\/global statement to access global variable in function\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \/\/variable scope is global\r\n\r\n$globalscope = 20;\r\n\r\n\/\/\u00a0 local variable scope function localscope()\r\n\r\n{\r\n\r\nglobal $globalscope;\r\n\r\necho '&lt;br&gt; The value of global scope variable is :'.$globalscope;\r\n\r\n}\r\n\r\nlocalscope();\u00a0 \/\/ call the function\r\n\r\n\/\/ using $var outside the function will display its value\r\n\r\necho '&lt;br&gt; The value of $globalscope outside the function is: '. $globalscope;\r\n\r\n&nbsp;\r\n\r\nThe output will be:\r\n\r\n&nbsp;\r\n\r\nThe value of global scope variable is :20\r\n\r\n&nbsp;\r\n\r\nThe value of $globalscope outside the function is: 20\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nFollowing example shows.\r\n\r\n\/\/variable scope is global\r\n\r\n$globalscope = 20;\r\n\r\necho '&lt;br&gt; The value of $globalscope outside the function is: '. $globalscope;\r\n\r\n\/\/\u00a0\u00a0 local variable scope function localscope()\r\n\r\n{\r\n\r\necho '&lt;br&gt; The value of global scope variable is :'.$GLOBALS['globalscope'];\r\n\r\n}\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n\u00a0 \u00a0 localscope();\u00a0\u00a0\u00a0 \/\/ call the function\r\n\r\n\/\/ using $var outside the function will generate an error\r\n\r\necho '&lt;br&gt; The value of $globalscope outside the function is: '. $globalscope;\r\n\r\n&nbsp;\r\n\r\nThen the output will be\r\n\r\nThe value of $globalscope outside the function is: 20\r\n\r\nThe value of global scope variable is :20\r\n\r\nThe value of $globalscope outside the function is: 20\r\n\r\n&nbsp;\r\n\r\n<strong>Static<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\n\/\/\u00a0 static variable function localscope()\r\n\r\n{\r\n\r\nstatic $var = 5;\u00a0 \/\/local scope\r\n\r\necho '&lt;br&gt; The value of static variable $var inside the function is: '. $var; $var++;\r\n\r\n}\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 localscope();\u00a0 \/\/ call the function<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">localscope(); \/\/ call the function<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">localscope();\u00a0\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">\/\/ call the function<\/span>\r\n\r\n&nbsp;\r\n<div>\r\n<p style=\"text-align: justify\">\/\/\u00a0 using $var outside the function will generate an error echo '&lt;br&gt; The value of $var inside the function is: '. $var;<\/p>\r\nThen the output will be\r\n\r\n&nbsp;\r\n\r\nPage <strong>4<\/strong> of <strong>7<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n\u00a0 \u00a0 The value of static variable $var inside the function is: 5\r\n\r\n&nbsp;\r\n\r\nThe value of static variable $var inside the function is: 6\r\n\r\n&nbsp;\r\n\r\nThe value of static variable $var inside the function is: 7\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-134 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85.png\" alt=\"\" width=\"636\" height=\"176\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>Default value for Parameters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nfunction setgender($gender = 'Male')\r\n\r\n{\r\n\r\necho '&lt;br&gt; Your gender is : '.$gender;\r\n\r\n}\r\n\r\n\/\/call the function\r\n\r\nsetgender();\r\n\r\nsetgender('Female');\r\n\r\n&nbsp;\r\n\r\nThe output will be\r\n\r\n&nbsp;\r\n\r\nYour gender is : Male\r\n\r\nYour gender is : Female\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\nfunction setdate($date = Null)\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(!isset($date))<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">$date = date('d-M-Y');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">echo '&lt;br&gt; The date is : '.$date;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">\/\/call the function<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">setdate();<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">setdate('25-Jun-2015');<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Then the output will be<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The date is : 15-Jun-2015<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">The date is : 25-Jun-2015<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Variable Length Parameter list<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-135 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86.png\" alt=\"\" width=\"643\" height=\"87\" \/>\u00a0 \u00a0&lt;?php\r\nfunction fact()\r\n{\r\n\r\n$count = func_num_args();\r\n\r\necho \"&lt;br&gt; This function has $count arguments\";\r\n\r\n}\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fact(1,2,3);\u00a0 \/\/3 arguments<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fact(1);\u00a0\u00a0\u00a0\u00a0 \/\/1 arguments<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fact(); \/\/ 0 arguments<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">fact('A','B','C','D','E','F','G'); \/\/ 7 Arguments<\/span>\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>Using reuire() and Include() functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">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.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-136 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87.png\" alt=\"\" width=\"664\" height=\"235\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe following code snippet shows use of include and require.\r\n\r\n&nbsp;\r\n<p style=\"padding-left: 30px\">include 'show.php'; \/\/parentheses are optional\r\ninlcude ('show.php'); \/\/ the file index.php in current directory\r\nrequire('.\/validation.php'); \/\/ the file validation.php in current directory\r\ninclude('view\/header.html'); \/\/ the file in view folder\r\ninlcude('..\/table.php'); \/\/ the file is one directory up<\/p>\r\n<p style=\"padding-left: 30px\">exit;<\/p>\r\n<p style=\"padding-left: 30px\">exit();<\/p>\r\n<p style=\"padding-left: 30px\">exit('Error in Database Connectivity');<\/p>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Functions in PHP \u2013 II<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/JKFEyG8zsqU\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<strong>References and suggested additional reading:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\r\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress<\/p>\r\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\r\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O'Reilly Media<\/p>\r\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP 5, Wrox<\/p>\r\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\r\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\r\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\r\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\r\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\r\n<p style=\"text-align: justify\">11. http:\/\/www.php.net<\/p>\r\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\r\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/JKFEyG8zsqU\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Scope of variables in Function<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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<\/p>\n<p>&nbsp;<\/p>\n<p>1. Local<\/p>\n<p>2. Global<\/p>\n<p>3. Static<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>To demonstrate the concept, let us take an example.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 local variable scope function localscope()<\/p>\n<p>{<\/p>\n<p>$var = 5;\u00a0 \/\/local scope<\/p>\n<p>echo &#8216;&lt;br&gt; The value of $var inside the function is: &#8216;. $var;<\/p>\n<p>}<\/p>\n<p>localscope();\u00a0 \/\/ call the function<\/p>\n<p>\/\/\u00a0 using $var outside the function will generate an error echo &#8216;&lt;br&gt; The value of $var inside the function is: &#8216;. $var;<\/p>\n<p>&nbsp;<\/p>\n<p>The output\u00a0 will be:<\/p>\n<p>&nbsp;<\/p>\n<p>The value of $var inside the function is: 5<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-132 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83.png\" alt=\"\" width=\"642\" height=\"145\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83.png 642w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83-300x68.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83-65x15.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83-225x51.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-83-350x79.png 350w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/p>\n<p>The value of $var inside the function is:<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;?php<\/p>\n<p>\/\/variable scope is global<\/p>\n<p>$globalscope = 20;<\/p>\n<p>\/\/\u00a0 local variable scope function localscope()<\/p>\n<p>{<\/p>\n<p>echo &#8216;&lt;br&gt; The value of global scope variable is :&#8217;.$globalscope;<\/p>\n<p>}<\/p>\n<p>localscope();\u00a0 \/\/ call the function<\/p>\n<p>\/\/ using $var outside the function will generate an error<\/p>\n<p>echo &#8216;&lt;br&gt; The value of $globalscope outside the function is: &#8216;. $globalscope;<\/p>\n<p>Then the output will be:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-133 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84.png\" alt=\"\" width=\"649\" height=\"167\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84.png 649w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84-300x77.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84-65x17.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84-225x58.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-84-350x90.png 350w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The value of global scope variable is :<\/p>\n<p>&nbsp;<\/p>\n<p>The value of $globalscope outside the function is: 20<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>Let us understand it by example.<\/p>\n<p>\/\/global statement to access global variable in function<\/p>\n<\/div>\n<div>\n<p>\u00a0 \/\/variable scope is global<\/p>\n<p>$globalscope = 20;<\/p>\n<p>\/\/\u00a0 local variable scope function localscope()<\/p>\n<p>{<\/p>\n<p>global $globalscope;<\/p>\n<p>echo &#8216;&lt;br&gt; The value of global scope variable is :&#8217;.$globalscope;<\/p>\n<p>}<\/p>\n<p>localscope();\u00a0 \/\/ call the function<\/p>\n<p>\/\/ using $var outside the function will display its value<\/p>\n<p>echo &#8216;&lt;br&gt; The value of $globalscope outside the function is: &#8216;. $globalscope;<\/p>\n<p>&nbsp;<\/p>\n<p>The output will be:<\/p>\n<p>&nbsp;<\/p>\n<p>The value of global scope variable is :20<\/p>\n<p>&nbsp;<\/p>\n<p>The value of $globalscope outside the function is: 20<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>Following example shows.<\/p>\n<p>\/\/variable scope is global<\/p>\n<p>$globalscope = 20;<\/p>\n<p>echo &#8216;&lt;br&gt; The value of $globalscope outside the function is: &#8216;. $globalscope;<\/p>\n<p>\/\/\u00a0\u00a0 local variable scope function localscope()<\/p>\n<p>{<\/p>\n<p>echo &#8216;&lt;br&gt; The value of global scope variable is :&#8217;.$GLOBALS[&#8216;globalscope&#8217;];<\/p>\n<p>}<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>\u00a0 \u00a0 localscope();\u00a0\u00a0\u00a0 \/\/ call the function<\/p>\n<p>\/\/ using $var outside the function will generate an error<\/p>\n<p>echo &#8216;&lt;br&gt; The value of $globalscope outside the function is: &#8216;. $globalscope;<\/p>\n<p>&nbsp;<\/p>\n<p>Then the output will be<\/p>\n<p>The value of $globalscope outside the function is: 20<\/p>\n<p>The value of global scope variable is :20<\/p>\n<p>The value of $globalscope outside the function is: 20<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Static<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 static variable function localscope()<\/p>\n<p>{<\/p>\n<p>static $var = 5;\u00a0 \/\/local scope<\/p>\n<p>echo &#8216;&lt;br&gt; The value of static variable $var inside the function is: &#8216;. $var; $var++;<\/p>\n<p>}<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 localscope();\u00a0 \/\/ call the function<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">localscope(); \/\/ call the function<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">localscope();\u00a0\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">\/\/ call the function<\/span><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p style=\"text-align: justify\">\/\/\u00a0 using $var outside the function will generate an error echo &#8216;&lt;br&gt; The value of $var inside the function is: &#8216;. $var;<\/p>\n<p>Then the output will be<\/p>\n<p>&nbsp;<\/p>\n<p>Page <strong>4<\/strong> of <strong>7<\/strong><\/p>\n<\/div>\n<div>\n<p>\u00a0 \u00a0 The value of static variable $var inside the function is: 5<\/p>\n<p>&nbsp;<\/p>\n<p>The value of static variable $var inside the function is: 6<\/p>\n<p>&nbsp;<\/p>\n<p>The value of static variable $var inside the function is: 7<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-134 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85.png\" alt=\"\" width=\"636\" height=\"176\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85.png 636w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85-300x83.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85-225x62.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-85-350x97.png 350w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Default value for Parameters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>function setgender($gender = &#8216;Male&#8217;)<\/p>\n<p>{<\/p>\n<p>echo &#8216;&lt;br&gt; Your gender is : &#8216;.$gender;<\/p>\n<p>}<\/p>\n<p>\/\/call the function<\/p>\n<p>setgender();<\/p>\n<p>setgender(&#8216;Female&#8217;);<\/p>\n<p>&nbsp;<\/p>\n<p>The output will be<\/p>\n<p>&nbsp;<\/p>\n<p>Your gender is : Male<\/p>\n<p>Your gender is : Female<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p>function setdate($date = Null)<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(!isset($date))<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">$date = date(&#8216;d-M-Y&#8217;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">echo &#8216;&lt;br&gt; The date is : &#8216;.$date;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">\/\/call the function<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">setdate();<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">setdate(&#8217;25-Jun-2015&#8242;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Then the output will be<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The date is : 15-Jun-2015<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">The date is : 25-Jun-2015<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Variable Length Parameter list<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-135 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86.png\" alt=\"\" width=\"643\" height=\"87\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86.png 643w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86-300x41.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86-65x9.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86-225x30.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-86-350x47.png 350w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/>\u00a0 \u00a0&lt;?php<br \/>\nfunction fact()<br \/>\n{<\/p>\n<p>$count = func_num_args();<\/p>\n<p>echo &#8220;&lt;br&gt; This function has $count arguments&#8221;;<\/p>\n<p>}<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fact(1,2,3);\u00a0 \/\/3 arguments<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fact(1);\u00a0\u00a0\u00a0\u00a0 \/\/1 arguments<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fact(); \/\/ 0 arguments<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">fact(&#8216;A&#8217;,&#8217;B&#8217;,&#8217;C&#8217;,&#8217;D&#8217;,&#8217;E&#8217;,&#8217;F&#8217;,&#8217;G&#8217;); \/\/ 7 Arguments<\/span><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Using reuire() and Include() functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">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.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-136 aligncenter\" src=\"http:\/\/itp9.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87.png\" alt=\"\" width=\"664\" height=\"235\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87.png 664w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87-300x106.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87-225x80.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-content\/uploads\/sites\/29\/2018\/07\/1-87-350x124.png 350w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The following code snippet shows use of include and require.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"padding-left: 30px\">include &#8216;show.php&#8217;; \/\/parentheses are optional<br \/>\ninlcude (&#8216;show.php&#8217;); \/\/ the file index.php in current directory<br \/>\nrequire(&#8216;.\/validation.php&#8217;); \/\/ the file validation.php in current directory<br \/>\ninclude(&#8216;view\/header.html&#8217;); \/\/ the file in view folder<br \/>\ninlcude(&#8216;..\/table.php&#8217;); \/\/ the file is one directory up<\/p>\n<p style=\"padding-left: 30px\">exit;<\/p>\n<p style=\"padding-left: 30px\">exit();<\/p>\n<p style=\"padding-left: 30px\">exit(&#8216;Error in Database Connectivity&#8217;);<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Functions in PHP \u2013 II<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/JKFEyG8zsqU\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>References and suggested additional reading:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,<\/p>\n<p style=\"text-align: justify\">2. W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress<\/p>\n<p style=\"text-align: justify\">3. Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,<\/p>\n<p style=\"text-align: justify\">4. Robin Nixon: Learning PHP, MySQL, and JavaScript, O&#8217;Reilly Media<\/p>\n<p style=\"text-align: justify\">5. Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP 5, Wrox<\/p>\n<p style=\"text-align: justify\">6. Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible<\/p>\n<p style=\"text-align: justify\">7. Joel Murach, Ray Harris: Murach\u2019s PHP and MySQL, Shroff\/Murach<\/p>\n<p style=\"text-align: justify\">8. Ivan Bayross, Web Enabled Commercial Application Development Using HTML\/Javascript\/DHTML\/PHP , BPB Publications<\/p>\n<p style=\"text-align: justify\">9. Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams<\/p>\n<p style=\"text-align: justify\">10. Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education<\/p>\n<p style=\"text-align: justify\">11. http:\/\/www.php.net<\/p>\n<p style=\"text-align: justify\">12. http:\/\/www.w3schools.com\/<\/p>\n<p style=\"text-align: justify\">13. http:\/\/www.tutorialspoint.com\/<\/p>\n","protected":false},"author":3,"menu_order":7,"template":"","meta":{"_acf_changed":false,"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-hiren-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-129","chapter","type-chapter","status-publish","hentry","contributor-dr-hiren-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":8,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/129\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/129\/revisions\/472"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapters\/129\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/pressbooks\/v2\/chapter-type?post=129"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/contributor?post=129"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp9\/wp-json\/wp\/v2\/license?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}