📖 Scenario: Imagine you have a small PHP script that keeps track of a counter. You want to increase this counter inside a function, but the counter is defined outside the function.
🎯 Goal: You will learn how to use the global keyword in PHP to access and modify a variable defined outside a function from inside that function.
📋 What You'll Learn
Create a variable called $counter with the value 0 outside any function.
Create a variable called $increment with the value 5.
Create a function called increaseCounter that uses the global keyword to access $counter and adds $increment to it.
Call the function increaseCounter once.
Print the value of $counter.
💡 Why This Matters
🌍 Real World
Using the <code>global</code> keyword helps when you want to share and update data across different parts of a PHP script, like counters, settings, or shared resources.
💼 Career
Understanding variable scope and the <code>global</code> keyword is important for PHP developers to manage data correctly and avoid bugs in web applications.
Progress0 / 4 steps
1
Create the initial counter variable
Create a variable called $counter and set it to 0.
PHP
Hint
Use $counter = 0; to create the variable.
2
Add the increment variable
Create a variable called $increment and set it to 5.
PHP
Hint
Use $increment = 5; to create the variable.
3
Create the function to increase the counter
Create a function called increaseCounter that uses the global keyword to access $counter and adds $increment to it.
PHP
Hint
Inside the function, write global $counter, $increment; to access the variables, then add $increment to $counter.
4
Call the function and print the counter
Call the function increaseCounter() once, then print the value of $counter using echo.
PHP
Hint
Call increaseCounter(); then print $counter with echo $counter;.
Practice
(1/5)
1. What does the global keyword do in PHP?
easy
A. It deletes a variable from the global scope.
B. It creates a new local variable inside the function.
C. It makes a variable constant and unchangeable.
D. It allows a function to access variables defined outside its scope.
Solution
Step 1: Understand variable scopes in PHP
Variables defined outside functions are in the global scope and not accessible inside functions by default.
Step 2: Role of the global keyword
The global keyword inside a function tells PHP to use the variable from the global scope instead of creating a new local one.
Final Answer:
It allows a function to access variables defined outside its scope. -> Option D
Quick Check:
global keyword = access outside variables [OK]
Hint: Global lets functions use outside variables directly [OK]
Common Mistakes:
Thinking global creates a new variable inside the function
Assuming global deletes variables
Confusing global with constants
2. Which of the following is the correct way to use the global keyword inside a function to access a variable $count defined outside?
easy
A. global $count;
B. use $count;
C. global($count);
D. extern $count;
Solution
Step 1: Recall PHP syntax for global variables
In PHP, to access a global variable inside a function, you write global $variableName; without parentheses.
Step 2: Check each option
global $count; uses correct syntax. use $count; is invalid in PHP. global($count); uses parentheses which is incorrect. extern $count; uses 'extern' which is not a PHP keyword.
Final Answer:
global $count; -> Option A
Quick Check:
Correct syntax = global $var; [OK]
Hint: Use 'global $var;' without parentheses inside functions [OK]
Common Mistakes:
Adding parentheses after global
Using 'use' or 'extern' keywords incorrectly
Forgetting the $ sign before variable name
3. What will be the output of the following PHP code?
$x = 5; function test() { global $x; $x = $x + 10; } test(); echo $x;
medium
A. 15
B. 5
C. 10
D. Error
Solution
Step 1: Understand the initial value of $x
$x is set to 5 in the global scope before the function call.
Step 2: Effect of the function test()
Inside test(), global $x; makes $x inside the function refer to the global $x. Then $x is increased by 10, so $x becomes 15 globally.
Final Answer:
15 -> Option A
Quick Check:
global $x modified to 15 [OK]
Hint: global lets function change outside variable value [OK]
Common Mistakes:
Thinking $x inside function is local and unchanged
What if global $a, $b; is replaced with global $a; only?
hard
A. Output is 7 in both cases.
B. Output is 7 with global $a, $b; and 4 with global $a; only.
C. Output is 4 with global $a, $b; and 7 with global $a; only.
D. Code causes error without global $b;.
Solution
Step 1: When using global $a, $b;
Both $a and $b inside the function refer to global variables. $b is updated to 3 + 4 = 7 globally.
Step 2: When using global $a; only
$a inside function is global, but $b is local and uninitialized. So $b = $a + $b uses local $b (null or 0), $b becomes 3 + 0 = 3 locally, global $b remains 4. echo $b outputs global $b which is 4.
Final Answer:
Output is 7 with global $a, $b; and 4 with global $a; only. -> Option B
Quick Check:
global vars must be declared to modify them [OK]
Hint: Declare all globals you want to use inside function [OK]
Common Mistakes:
Assuming global $a; also makes $b global
Expecting output 7 without declaring global $b
Thinking missing global causes syntax error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.