0
0
PHPprogramming~10 mins

Why global state is dangerous in PHP - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why global state is dangerous in PHP
Start: Global variable set
Function A reads/modifies global
Function B reads/modifies global
Unexpected changes in global
Hard to track bugs
Program behaves unpredictably
Global variables can be changed anywhere, causing unexpected results and bugs that are hard to find.
Execution Sample
PHP
<?php
$counter = 0;
function increment() {
  global $counter;
  $counter++;
}
increment();
increment();
echo $counter;
?>
This code uses a global variable $counter that a function increments twice, showing how global state changes.
Execution Table
StepActionGlobal $counter ValueOutput
StartInitialize $counter = 00
Call increment() #1Increment $counter by 11
Call increment() #2Increment $counter by 12
Echo $counterPrint current $counter value22
💡 Program ends after printing the final value of $counter which is 2
Variable Tracker
VariableStartAfter increment() #1After increment() #2Final
$counter0122
Key Moments - 2 Insights
Why can changing a global variable in one function affect another function unexpectedly?
Because both functions use the same global variable, changes in one affect the shared state seen by the other, as shown in steps 2 and 3 of the execution_table.
Why is it hard to find bugs when using global variables?
Because any part of the code can change the global variable at any time, making it unclear where a wrong value came from, as the global $counter changes in multiple places.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $counter after the first call to increment()?
A1
B0
C2
DUndefined
💡 Hint
Check the 'Global $counter Value' column at step 'Call increment() #1'
At which step does the program output the value 2?
AStart
BEcho $counter
CCall increment() #1
DCall increment() #2
💡 Hint
Look at the 'Output' column in the execution_table
If we remove the 'global' keyword inside increment(), what happens to $counter?
A$counter increments globally as before
BError: undefined variable
C$counter inside function is local and global $counter stays 0
DProgram crashes
💡 Hint
Without 'global', the function uses a local variable, so global $counter does not change (see variable_tracker)
Concept Snapshot
Global variables in PHP can be accessed and changed anywhere using the 'global' keyword.
This shared state can cause unexpected bugs because changes in one place affect others.
Tracking bugs is hard since any function can modify the global variable.
Avoid globals for safer, predictable code by passing variables as parameters instead.
Full Transcript
This example shows a global variable $counter initialized to 0. Two calls to the function increment() increase $counter by 1 each time using the global keyword. The execution table traces each step: starting at 0, after first increment it becomes 1, after second increment it becomes 2, and finally the program prints 2. The variable tracker shows $counter changing from 0 to 2 across calls. Beginners often get confused because changes in one function affect the global state seen by others, making bugs hard to find. The quiz asks about $counter values at different steps and what happens if the global keyword is removed. The key lesson is that global state is dangerous because it can be changed anywhere, causing unpredictable behavior and making debugging difficult.