0
0
PHPprogramming~10 mins

Global namespace access in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global namespace access
Start script
Define global variable
Enter function
Access global variable using $GLOBALS or global keyword
Modify or read global variable
Exit function
Use global variable outside function
End script
The script defines a global variable, then inside a function accesses it using special ways, modifies or reads it, then continues outside the function.
Execution Sample
PHP
<?php
$val = 10;
function test() {
  global $val;
  $val += 5;
}
test();
echo $val;
?>
This code shows how to access and modify a global variable inside a function using the global keyword.
Execution Table
StepActionVariableValueNotes
1Define global variable$val10Global scope
2Enter function test()--Function starts
3Declare global $val inside function$val10Access global variable
4Modify $val inside function$val15Increment by 5
5Exit function test()--Function ends
6Echo $val$val15Global variable updated
7End script--Script ends
💡 Script ends after printing updated global variable value 15
Variable Tracker
VariableStartAfter function callFinal
$val101515
Key Moments - 2 Insights
Why do we need the 'global' keyword inside the function?
Without 'global', the function treats $val as a new local variable. Using 'global $val' tells PHP to use the variable from the global scope, as shown in step 3 of the execution_table.
Can we access global variables inside functions without 'global'?
Yes, by using the $GLOBALS array like $GLOBALS['val'], but this example uses 'global' keyword for simplicity (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $val after step 4?
A15
B10
C5
DUndefined
💡 Hint
Check the 'Modify $val inside function' row in execution_table where $val changes to 15.
At which step does the function test() end?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the row with 'Exit function test()' in execution_table.
If we remove the 'global $val;' line, what would be the output at step 6?
A15
B10
CError
D5
💡 Hint
Without 'global', $val inside function is local and global $val remains unchanged as in variable_tracker.
Concept Snapshot
PHP global namespace access:
- Global variables exist outside functions.
- Use 'global $var;' inside functions to access global variables.
- Alternatively, use $GLOBALS['var'] array.
- Without this, functions use local variables.
- Changes inside function affect global variable if accessed properly.
Full Transcript
This example shows how PHP handles global variables inside functions. We start with a global variable $val set to 10. Inside the function test(), we use the 'global' keyword to tell PHP to use the global $val, not a new local one. We then add 5 to $val, making it 15. After the function ends, when we echo $val, it prints 15, showing the global variable was updated. Without 'global', the function would create a local $val and the global $val would stay 10. This trace helps understand how to access and modify global variables inside functions in PHP.