Variables and Scope
Last updated
What is Variables and Scope?
Variables are a central part of PHP, allowing you to store data that can be used later in your scripts. Their values can be of various types including strings, integers, arrays, and objects. PHP has both local and global scope when it comes to variables. Local scope refers to variables that are only accessible within the function they are defined, while global scope means a variable is accessible to any part of the script. However, to use a global variable inside a function, you need to declare it as global. Here's a brief example:
$x = 10; //global variable
function test() {
global $x; // accessing the global variable
echo $x;
}
test(); //prints 10
Free resources to learn Variables and Scope
- Official docsVariables and Scope
More in stage 3: Arrays
Variables and Scope: common questions
Can I learn Variables and Scope in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Variables and Scope in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Variables and Scope tutorial for beginners?
It is lesson 15 of 120 in the free PHP roadmap, in stage 3, Arrays. If you are new to PHP, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.