Define Variable Flashcards
What is a variable in PHP?
A variable in PHP is a container for storing data values
Variables are represented by a dollar sign followed by the name of the variable.
How is a variable represented in PHP?
Variables are represented by a dollar sign followed by the name of the variable
Example: $username = “admin”.
What does the scope of a variable refer to?
The context within which it is defined and accessible.
What is Local Scope?
Variables defined within a function are local to that function and cannot be accessed outside of it
Example: function myFunction() { $localVar = “I am local”; }
What is Global Scope?
Variables defined outside of any function are global and can be accessed anywhere in the script
Example: $globalVar = “I am global”;
Fill in the blank: A _______ variable retains its value even after the function has finished executing.
static
What is the behavior of a static variable in PHP?
A static variable retains its value even after the function has finished executing
Example: function myFunction() { static $count = 0; $count++; echo $count; }
True or False: A local variable can be accessed outside its defining function.
False
True or False: A global variable can be accessed anywhere in the script.
True