|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2021-04-08 15:00 UTC] rg at mejoramos dot com
 Description:
------------
no't possible pass $VAR to GLOBAL context without used ARRAY $GLOBALS, please see the code.
Test script:
---------------
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function OtherFunction(){
	GLOBAL $ManyFunctionsNeedThisVar;
	print_r($GLOBALS['a']);
	print_r($ManyFunctionsNeedThisVar);
	}
class OtherClass{
	function __construct(){
		$GLOBALS['a'] = 888;
		$ManyFunctionsNeedThisVar = 777;
		OtherFunction();
		}
	}
$OtherObject = new OtherClass();
Expected result:
----------------
String concatenated, correct output:
888777
Actual result:
--------------
888
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 04:00:01 2025 UTC | 
Thanks master @dharman when we declare $ManyFunctionsNeedThisVar = 777; this is GLOBAL. When we declare function __construct(){ // --> this is GLOBAL ? $GLOBALS['a'] = 888; // --> this is GLOBAL ? $ManyFunctionsNeedThisVar = 777; // --> this is GLOBAL ? OtherFunction(); }