Variable scope tells us where a variable can be used in a program. It helps keep things organized and avoids mistakes.
Variable scope in functions in PHP
<?php function functionName() { // local variable $var = 'value'; } // global variable $var = 'value'; function anotherFunction() { global $var; // to use global variable inside function // now $var refers to the global one } ?>
Variables inside functions are local by default.
Use global keyword to access global variables inside functions.
<?php function greet() { $message = "Hello"; // local variable echo $message; } greet();
global.<?php $message = "Hi"; // global variable function greet() { global $message; echo $message; } greet();
<?php $message = "Hi"; function greet() { $message = "Hello"; // local variable, different from global echo $message; } greet(); echo $message;
This program shows how global and local variables behave inside functions. It prints the message from global, changes it, then shows local variable inside another function does not affect global.
<?php $message = "Good morning"; // global variable function sayMessage() { global $message; // use global variable echo $message . "\n"; $message = "Good night"; // change global variable } sayMessage(); echo $message . "\n"; // check changed global variable function localTest() { $message = "Local hello"; // local variable echo $message . "\n"; } localTest(); echo $message . "\n"; // global variable unchanged here
Without global, variables inside functions are separate from global ones.
Changing a local variable does not affect the global variable with the same name.
Use global variables carefully to avoid confusion.
Variables inside functions are local by default.
Use global keyword to access and change global variables inside functions.
Local variables do not affect global variables with the same name.
Practice
global?Solution
Step 1: Understand variable declaration inside functions
Variables declared inside a function are local by default, meaning they exist only within that function.Step 2: Recall the role of the
Without usingglobalkeywordglobal, the variable does not refer to any global variable with the same name.Final Answer:
It is local to the function only. -> Option DQuick Check:
Local scope = inside function only [OK]
- Assuming variables inside functions are global by default
- Confusing local variables with static variables
- Thinking variables inside functions affect global variables automatically
$count inside a PHP function?Solution
Step 1: Recall how to access global variables inside functions
In PHP, to access a global variable inside a function, you must declare it with theglobalkeyword inside that function.Step 2: Check other options for correctness
Declaring the variable again inside the function creates a new local variable.statickeeps value between calls but does not access global.varis not used inside functions.Final Answer:
Useglobal $count;inside the function. -> Option CQuick Check:
Access global with global keyword [OK]
- Re-declaring global variables inside functions without 'global'
- Using 'static' instead of 'global' to access globals
- Using 'var' keyword inside functions
$x = 5;
function test() {
$x = 10;
echo $x;
}
test();
echo $x;Solution
Step 1: Analyze variable scope inside the function
Insidetest(),$xis assigned 10 locally, soecho $x;prints 10.Step 2: Analyze variable outside the function
Outside the function,$xremains 5 because the local$xinside the function does not affect the global$x.Final Answer:
10 5 -> Option AQuick Check:
Local $x = 10, global $x = 5 [OK]
- Assuming function changes global variable without 'global' keyword
- Confusing output order
- Thinking variables inside functions share the same memory as globals
$count = 0;
function increment() {
$count++;
}
increment();
echo $count;Solution
Step 1: Understand variable scope in the function
The function tries to increment$count, but withoutglobal, it treats$countas undefined local variable.Step 2: Fix the scope issue
Addingglobal $count;inside the function tells PHP to use the global$countvariable, allowing increment to work.Final Answer:
Addglobal $count;inside the function. -> Option BQuick Check:
Use global keyword to modify global variables inside functions [OK]
- Forgetting to declare global variables inside functions
- Assuming variables are automatically global
- Using static instead of global for global variables
$value = 3;
function multiply() {
global $value;
$value *= 2;
}
multiply();
function reset() {
$value = 0;
}
reset();
echo $value;
What will be the output and why?Solution
Step 1: Analyze
It usesmultiply()functionglobal $value;and doubles the global$valuefrom 3 to 6.Step 2: Analyze
It declaresreset()function$valuewithoutglobal, so it creates a local variable$valueset to 0, leaving global$valueunchanged.Step 3: Final output
After calling both functions, global$valueremains 6, soecho $value;prints 6.Final Answer:
6 because multiply changes global, reset changes local only. -> Option AQuick Check:
global keyword affects global variable, local does not [OK]
- Assuming reset() changes global variable without 'global'
- Thinking variables inside functions always affect globals
- Confusing local and global variable effects
Ready to prove it?
Pass the test and this pattern moves unknown
