What are the different scopes of variables?
What are the different scopes of variables?
Here is an explanation of the different scopes of variables in PHP:
In PHP, variables can have different scopes depending on where they are defined and used. The main variable scopes are:
Let's discuss each scope in detail:
Example:
function myFunction() {
$x = 5; // $x has local scope
echo "Value of x inside the function: $x";
}
myFunction(); // Calls the function
echo "Value of x outside the function: $x"; // Error: $x is not defined
global
keyword or the $GLOBALS
superglobal array.Example:
$x = 5; // $x has global scope
function myFunction() {
global $x;
echo ...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào