Home >>PHP Object Oriented >PHP Local Variables

PHP Local Variables

Local Variable

=> Local variables are always initialize inside the funtciob body. => Local variable cant work outside function body.

<?php
		
function add()
		
{
			
$x=100;
			
$y=200;
			
$sum=$x+$y;
			
echo "sum of given no=".$sum;
		
}
		
function sub()
		
{
			
$x=500;
			
$y=200;
			
$sub=$x-$y;
			
echo "subtraction of given no=".$sub;
		
}
		
add();
		
sub();
	
     
?>
Output Sum of given no = 300 Subtraction of given no = 300

Here define a variable $x and $y . Variable $x and $y has been defined inside add( ) function so these variables are not accessible outside the add( ) function. Now if you want to access local variable $x and $y inside the sub( ) function then declare it first then use.


No Sidebar ads