global variable php
snippet in php

global variable php

user3722


 <?php 
$x = 75; 
$y = 25;
 
function addition() { 
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
 
addition(); 
echo $z; 
?>  

php global variable

user869

<?php 
// Demonstrate how to declare global variable 
  
// Declaring global variable 
$x = "hi,"; 
$y = " ankur"; 
$z = " here."; 
  
// Display value 
// Concatenating String 
echo $x.$y.$z; 
  
?> 
  
// output
hi, ankur here.
  
<?php
// global variable inside using global keyword
  
// Declaring global variable 
$x = "hi,"; 
$y = " ankur"; 
$z = " here.";
$a = 100;
$b = 50;

function showname(){
  	//using global keyword
	global $x, $y, $z;
    return $x.$y.$z;
}

function addition(){
    // using GLOBALS['variable name']
	$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];  	
}

// printing output
echo showname()."\n";
addition();
echo $b;
?>
  
//output
hi, ankur here.
150