PHP Functions - Accessing Global Variables
Description:
PHP Functions - Accessing Global Variables
About this Kata Series
"PHP Functions" is a Kata Series authored by donaldsebleung which focuses on the perks and interesting features of PHP functions. It is un-numbered which means that the Kata in this Series is not progressive - one kata in this Series does not necessarily depend on another. This means that as long as you know how to write "Hello World" level programs in PHP, you will probably find any Kata in this Series easy to pick up and complete.
There is, however, one main prerequisite. You must be sufficiently familiar with primitive data types in PHP (strings, booleans, floats, integers) and what they mean. You must also have a basic understanding of how to define a simple function in PHP that may or may not receive a fixed number of arguments. A good indicator that you are ready for any Kata in this Series is if you are able to complete Multiply (8kyu) in PHP without looking at external resources in the process. If you are unable to complete that Kata without researching PHP function syntax then you may have to practice with basic PHP functions until you are familiar with them before continuing on this Kata series.
Certain Kata in this Series may have additional prerequisites. If that is the case, the extra prerequisites will be listed under a Level 2 Heading called Prerequisites in said Kata. Additionally, if a certain Kata in this Series requires a thorough understanding of PHP, the kata may be labelled as [Advanced].
Lesson
In PHP, functions by default can only create and/or access variables within its own scope. This means that if, for example, you have a global variable called $x
and you want to double the value of that global variable called $x
within your function called double_x()
...
$x = 21; // Global Variable $x set to 21
function double_x() {
$x *= 2; // We want to double the value of global variable $x - will it work?
}
double_x();
echo $x; // still 21, $x has not doubled
... the function will silently fail and $x
will not be doubled. This is because each function has its own scope in PHP and therefore the variable $x
inside the function was referring to a variable created within the function itself, not the global variable $x
. This is usually a good thing for PHP Developers as they do not need to worry about accidentally modifying global variables.
However, in some cases, we may want the function to actively modify certain global variables. This can be achieved in two ways. The first is to declare which variables are global in the first line of the function using the global
keyword:
$x = 21; // Global Variable $x set to 21
function double_x() {
global $x; // Now all instances where the function refers to $x will refer to the GLOBAL version of $x, **not** just $x inside the function itself
$x *= 2; // Double the GLOBAL variable $x
}
double_x(); // Will this work?
echo $x; // 42
double_x();
echo $x; // 84
You can also declare multiple variables global
inside a function by separating the variables with a comma ,
:
$x = 21;
$y = 10;
function double_x_halve_y() {
global $x, $y; // Now, both $x and $y in the function refer to the global variables $x and $y
$x *= 2; // Double $x
$y /= 2; // Halve $y
}
double_x_halve_y();
echo $x; // 42
echo $y; // 5
However, notice that when certain variables are declared global
in this way, you can no longer declare a variable with the same name that belongs solely to the function.
Another way to access global variables within a function is by using the superglobal $GLOBALS
. Superglobals are special (predefined) global variables that can be directly accessed within functions without an explicit global
declaration. Other superglobals include $_SERVER
, $_POST
and $_GET
(and a few others) but we will not cover those in this lesson.
$x = 21;
$y = -42;
$z = "Hello World";
function reset_xyz_to_42() {
$GLOBALS['x'] = 42; // Resetting global variable $x to 42
$GLOBALS['y'] = 42; // Resetting global varialbe $y to 42
$GLOBALS['z'] = 42; // Resetting global variable $z to 42
$x = "Goodbye World"; // You are then free to declare and define variables with the same name inside the function that belongs to the function itself
}
reset_xyz_to_42();
echo $x; // 42
echo $y; // 42
echo $z; // 42
Task
Note: The lesson provided in this Kata is designed to teach you most, if not all, of the key concepts required to complete the Task in this Kata. However, if in doubt, you are strongly encouraged to conduct your own research.
Declare and define the following functions as instructed.
increment_x()
- This function should increment a global variable$x
by1
every time the function is called.double_x_triple_y_quadruple_z()
- This function should double a global variable$x
, triple a global variable$y
and quadruple a global variable$z
every time the function is calledappend_whitespace_to_string()
- This function should append a whitespae character" "
to a global variable$string
every time the function is called.add_world_to_string()
- This function should add the string"world"
to the end of a global variable$string
every time the function is called.
Switch to another Kata in this Series
You May Also Like
Similar Kata:
Stats:
Created | Sep 16, 2016 |
Published | Sep 16, 2016 |
Warriors Trained | 581 |
Total Skips | 25 |
Total Code Submissions | 1426 |
Total Times Completed | 392 |
PHP Completions | 392 |
Total Stars | 4 |
% of votes with a positive feedback rating | 86% of 93 |
Total "Very Satisfied" Votes | 71 |
Total "Somewhat Satisfied" Votes | 18 |
Total "Not Satisfied" Votes | 4 |
Total Rank Assessments | 8 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 7 kyu |
Lowest Assessed Rank | 8 kyu |