Returning Global Variables

Returning Global Variables:
The better way to give a function access to an externally created variable,is by declaring it a global variable from within the function.The global keyword followed by the variable name

For Example:
<?php
$a1="WILLIAM";
$a2="henry";
$a3="gatES";
echo $a1 ." ".$a2." ".$a3."<br>";
fix_names();
echo $a1 ." ".$a2." ".$a3;
function fix_names()
{
global $a1; $a1=ucfirst(strtolower($n1));
global $a2; $a2=ucfirst(strtolower($n2));
global $a3; $a3=ucfirst(strtolower($n3));
}
?>
OUTPUT:
WILLIAM henry gatES
William Henry Gates

now you don’t have to pass parameters to the function,and it doesn’t have to accept them.Once declared,these variables remain global and global and available to the rest of your program,including its functions.

Translate »