Bird
Raised Fist0
PHPprogramming~10 mins

Global keyword behavior in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Global keyword behavior
Start
↓
Define global variable
↓
Enter function
↓
Use 'global' keyword
↓
Access/modify global variable
↓
Exit function
↓
Global variable updated
↓
End
This flow shows how a global variable is defined outside a function, then accessed and modified inside a function using the 'global' keyword.
Execution Sample
PHP
<?php
$x = 5;
function test() {
  global $x;
  $x = 10;
}
test();
echo $x;
?>
This code defines a global variable $x, modifies it inside a function using 'global', then prints the updated value.
Execution Table
StepActionVariableValueNotes
1Define global variable$x5Global scope
2Call function test()--Enter function scope
3Declare 'global $x' inside function$x5Link local $x to global $x
4Assign $x = 10 inside function$x10Modifies global $x
5Exit function test()--Return to global scope
6Echo $x$x10Prints updated global variable
💡 Function ends and global variable $x is updated to 10
Variable Tracker
VariableStartAfter function callFinal
$x51010
Key Moments - 2 Insights
Why does $x inside the function change the global $x?
Because of the 'global $x;' declaration inside the function (see step 3 in execution_table), the local $x refers to the global $x, so modifying it changes the global variable.
What happens if we don't use 'global' inside the function?
Without 'global', $x inside the function is a new local variable, so changes won't affect the global $x (not shown in table but implied).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $x after step 4 inside the function?
A10
B5
CUndefined
D0
💡 Hint
Check the 'Value' column at step 4 where $x is assigned 10 inside the function.
At which step does the global variable $x get updated?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Action' and 'Notes' columns around step 4 where $x is assigned 10.
If we remove 'global $x;' from the function, what would be the output at step 6?
A10
B5
CError
D0
💡 Hint
Without 'global', the function modifies a local $x, so global $x remains unchanged as shown in variable_tracker.
Concept Snapshot
PHP 'global' keyword:
- Declares variables inside functions as global.
- Allows access and modification of global variables.
- Without 'global', variables inside functions are local.
- Changes to global variables persist after function ends.
Full Transcript
This example shows how PHP's 'global' keyword works. We start by defining a global variable $x with value 5. When we enter the function test(), we declare 'global $x;' which links the local $x to the global one. Then we assign 10 to $x inside the function, which updates the global variable. After the function ends, printing $x shows 10, confirming the global variable was changed. Without 'global', the function would only change a local copy, leaving the global $x unchanged.

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

  1. Step 1: Understand variable scopes in PHP

    Variables defined outside functions are in the global scope and not accessible inside functions by default.
  2. 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.
  3. Final Answer:

    It allows a function to access variables defined outside its scope. -> Option D
  4. 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

  1. Step 1: Recall PHP syntax for global variables

    In PHP, to access a global variable inside a function, you write global $variableName; without parentheses.
  2. 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.
  3. Final Answer:

    global $count; -> Option A
  4. 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

  1. Step 1: Understand the initial value of $x

    $x is set to 5 in the global scope before the function call.
  2. 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.
  3. Final Answer:

    15 -> Option A
  4. 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
  • Expecting output 5 or 10 instead of 15
  • Assuming error due to global keyword
4. Identify the error in this PHP code snippet:
$count = 10;
function increment() {
$count = $count + 1;
}
increment();
echo $count;
medium
A. Variable $count is undefined.
B. Syntax error in function declaration.
C. Missing global keyword inside function to access $count.
D. Cannot echo variable outside function.

Solution

  1. Step 1: Check variable scope inside function

    Inside increment(), $count is treated as a new local variable, not the global one.
  2. Step 2: Missing global keyword

    To modify the global $count, the function must declare global $count; before using it.
  3. Final Answer:

    Missing global keyword inside function to access $count. -> Option C
  4. Quick Check:

    global needed to access outside variable [OK]
Hint: Declare global inside function to modify outside variable [OK]
Common Mistakes:
  • Assuming $count inside function is global by default
  • Thinking echo outside function causes error
  • Confusing syntax errors with scope errors
5. Given the code below, what will be the output?
$a = 3;
$b = 4;
function sum() {
global $a, $b;
$b = $a + $b;
}
sum();
echo $b;

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

  1. 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.
  2. 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.
  3. Final Answer:

    Output is 7 with global $a, $b; and 4 with global $a; only. -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard