The document explains the concept of local, global, and static variables in PHP. Local variables are confined to the function they are declared in, while global variables can be accessed throughout the program if declared as global within a function. Static variables retain their value between function calls, allowing for persistent state across invocations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
14 views2 pages
BGD
The document explains the concept of local, global, and static variables in PHP. Local variables are confined to the function they are declared in, while global variables can be accessed throughout the program if declared as global within a function. Static variables retain their value between function calls, allowing for persistent state across invocations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
PHP Local Variables
A variable declared in a function is considered local; that is, it can be
referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function: <? $x = 4; function assignx () { PHP 33 $x = 0; print "\$x inside function is $x. "; } assignx(); print "\$x outside of function is $x. "; ?> This will produce the following result. $x inside function is 0. $x outside of function is 4. PHP Function Parameters PHP Functions are covered in detail in PHP Function Chapter. In short, a function is a small unit of program which can take some input in the form of parameters and does some processing and may return a value. Function parameters are declared after the function name and inside parentheses. They are declared much like a typical variable would be: <? // multiply a value by 10 and return it to the caller function multiply ($value) { $value = $value * 10; return $value; } $retval = multiply (10); Print "Return value is $retval\n"; ?> This will produce the following result. Return value is 100 PHP 34 PHP Global Variables In contrast to local variables, a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example: <? $somevar = 15; function addit() { GLOBAL $somevar; $somevar++; print "Somevar is $somevar"; } addit(); ?> This will produce the following result. Somevar is 16 PHP Static Variables The final type of variable scoping that I discuss is known as static. In contrast to the variables declared as function parameters, which are destroyed on the function's exit, a static variable will not lose its value when the function exits and will still hold that value should the function be called again. You can declare a variable to be static simply by placing the keyword STATIC in front of the variable name. <? function keep_track() { STATIC $count = 0; $count++; print $count; print " "; PHP 35 } keep_track(); keep_track(); keep_track(); ?> This will produce the following resul