0
0
PHPprogramming~10 mins

Variable scope in functions in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variable scope in functions
Start Program
Define Global Variable
Call Function
Inside Function
Access Global?
Use global var
Function Ends
Back to Global
Program Ends
This flow shows how a program defines a global variable, calls a function, and how variables inside the function can be local or access the global variable.
Execution Sample
PHP
<?php
$number = 10;
function test() {
  $number = 5;
  echo $number;
}
test();
echo $number;
?>
This code shows a global variable and a local variable with the same name inside a function, printing both.
Execution Table
StepCode LineVariableValueActionOutput
1$number = 10;number10Global variable set
2function test() {...}--Function defined
3test();--Function called
4$number = 5;number (local)5Local variable inside function set
5echo $number;number (local)5Print local variable5
6Function ends--Return to global scope
7echo $number;number (global)10Print global variable10
💡 Function ends and program finishes after printing both local and global variables.
Variable Tracker
VariableStartAfter Function CallFinal
number (global)101010
number (local)undefined5undefined
Key Moments - 3 Insights
Why does echo inside the function print 5, but outside prints 10?
Inside the function, $number is a new local variable set to 5 (see execution_table step 4 and 5). Outside, $number is the global variable still holding 10 (step 7). They are different variables.
Does the local $number inside the function change the global $number?
No, the local $number is separate and does not affect the global $number (see variable_tracker). The global $number remains 10 after the function call.
What if we want to change the global $number inside the function?
You must declare 'global $number;' inside the function to access and modify the global variable (not shown here). Without it, $number inside is local.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does $number have inside the function at step 4?
Aundefined
B10
C5
D0
💡 Hint
Check the 'Value' column for step 4 in execution_table.
At which step does the program print the global $number?
AStep 5
BStep 7
CStep 6
DStep 3
💡 Hint
Look for 'echo $number;' outside the function in execution_table.
If we add 'global $number;' inside the function before setting it, what would happen to the global $number?
AIt changes to 5
BIt stays 10
CIt becomes undefined
DIt causes an error
💡 Hint
Recall key_moments explanation about modifying global variables inside functions.
Concept Snapshot
Variable scope in PHP functions:
- Variables inside functions are local by default.
- Global variables are separate unless declared with 'global'.
- Local variables do not affect global variables.
- Use 'global $var;' inside functions to access global variables.
- Echo inside function prints local; outside prints global if names clash.
Full Transcript
This example shows how PHP handles variable scope in functions. A global variable named $number is set to 10. Inside the function test(), a new local variable $number is set to 5. When the function prints $number, it shows 5 because it uses the local variable. After the function ends, printing $number outside shows 10, the global variable. The local variable inside the function does not change the global one. To modify the global variable inside a function, you must declare it with 'global $number;'. This helps avoid confusion between local and global variables.