Tutorialsplane

PHP Variable Scope


PHP Variable Scope:– Scope of a variable means who can see and use it. In other words we can say that when we use a variable inside a function it refers to local variable and if it is defined outside the function ; it is called as globale variable because we can access it anywhere within the program.


PHP Variable Scope | Example

Let us take an example to understand scope of a variable.

Below we get two compile errors while executing below program because we are trying to access $x i.e global variable inside a function. Similarily we can not access a local variable outside the function.

Example

<?php $x=10;

function play(){
	
    $y=20;
	echo $y;
	echo $x;// compile error
	echo "<br?>";
	echo "hey";
	echo "<br/>";
}

play();

echo $x;

echo $y;//compile error

?>

PHP Tutorial