Frieda
0
Q:

php global variable


 <?php 
$x = 75; 
$y = 25;
 
function addition() { 
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}
 
addition(); 
echo $z; 
?>  
0
<?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  
3
$GLOBALS["foo"]
0

New to Communities?

Join the community