Variable scope defines where a variable can be accessed or used in the code. In PHP, variables inside functions are local by default and cannot be accessed outside the function.
Click to reveal answer
beginner
What happens if you try to use a variable declared outside a function inside that function without any special keyword?
The function will not see the outside variable because variables inside functions are local by default. You need to use the global keyword or pass the variable as a parameter to access it.
Click to reveal answer
beginner
How do you access a global variable inside a PHP function?
Use the global keyword inside the function before the variable name. This tells PHP to use the variable from the global scope.
Click to reveal answer
intermediate
What is the static keyword used for in PHP functions?
The static keyword keeps a local variable's value between function calls. It means the variable does not reset each time the function runs.
Click to reveal answer
beginner
Explain the difference between local and global variables in PHP.
Local variables exist only inside the function where they are declared. Global variables exist outside functions and can be accessed anywhere if declared global inside functions.
Click to reveal answer
What keyword allows a function to access a variable declared outside it?
Aglobal
Blocal
Cstatic
Dpublic
✗ Incorrect
The global keyword tells the function to use the variable from the global scope.
What is the scope of a variable declared inside a function without any keyword?
AGlobal
BPublic
CStatic
DLocal
✗ Incorrect
Variables declared inside functions are local to that function by default.
Which keyword keeps a variable's value between function calls?
Aglobal
Blocal
Cstatic
Dconst
✗ Incorrect
The static keyword preserves the variable's value between calls.
If you want to use a global variable inside a function without global, what is a good alternative?
APass it as a function parameter
BDeclare it again inside the function
CUse <code>static</code> keyword
DUse <code>const</code> keyword
✗ Incorrect
Passing the variable as a parameter lets the function use its value without needing global.
What happens if you try to access a local variable outside its function?
AIt works fine
BIt causes an error or undefined variable
CIt accesses the global variable
DIt converts to static
✗ Incorrect
Local variables only exist inside their function and cannot be accessed outside.
Explain how variable scope works inside PHP functions and how to access variables from outside the function.
Think about where variables live and how to share them with functions.
You got /4 concepts.
Describe the purpose of the static keyword in PHP functions and give an example of when it might be useful.
Consider a counter that remembers its value each time a function runs.
You got /4 concepts.
Practice
(1/5)
1. What is the scope of a variable declared inside a PHP function without using global?
Variables declared inside a function are local by default, meaning they exist only within that function.
Step 2: Recall the role of the global keyword
Without using global, the variable does not refer to any global variable with the same name.
Final Answer:
It is local to the function only. -> Option D
Quick Check:
Local scope = inside function only [OK]
Hint: Variables inside functions are local unless declared global [OK]
Common Mistakes:
Assuming variables inside functions are global by default
Confusing local variables with static variables
Thinking variables inside functions affect global variables automatically
2. Which of the following is the correct way to access a global variable $count inside a PHP function?
easy
A. Use var $count; inside the function.
B. Declare $count again inside the function.
C. Use global $count; inside the function.
D. Use static $count; inside the 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 the global keyword inside that function.
Step 2: Check other options for correctness
Declaring the variable again inside the function creates a new local variable. static keeps value between calls but does not access global. var is not used inside functions.
Final Answer:
Use global $count; inside the function. -> Option C
Quick Check:
Access global with global keyword [OK]
Hint: Use 'global' keyword to access global variables inside functions [OK]
Common Mistakes:
Re-declaring global variables inside functions without 'global'
Using 'static' instead of 'global' to access globals
Using 'var' keyword inside functions
3. What will be the output of the following PHP code?