PHP: global variable

PHP: global variable

July 12, 2020
$x = 5; // global scope
$y = 5; 
function myTest() {
	//global $x;

	echo "<pre>";
	echo $GLOBALS['x'];
	echo $GLOBALS['y'];
	echo "</pre>";
  // using x inside this function will generate an error
  //echo "<p>Variable x inside function is:" . $x .". </p>";

echo "<p>Variable x outside function is: $x</p>";

/*A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

One Comment

  1. 20bet September 5, 2023 at 4:12 pm - Reply

    Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.

Leave A Comment