Wednesday, July 3, 2013

Regarding Global Variable

What is the output of the following?

<?php

$a = 100;

function myfunction($b) {
 $a = 200;
 
 global $a;
 return $a;
}

print $a;

The output is 100. Because, the variable $a is a global variable. The global declaration within the function tells the function to use the global value, rather than the local value.

No comments:

Post a Comment