register_globals off - now I get an odd undefined but it shouldn't be
-
13-04-2021 - |
Question
What I had BEFORE was...
if(DEBUGMODE) $debug_err_msgs[] = 'Some error'; // add a new error to the array
... more code here...
if(DEBUGMODE)$debug_err_msgs[] = 'Some error'; // add a new error to the array
which worked great EXCEPT in functions. SO... I decided to make it GLOBAL by using the $_GLOBALS array. I originally liked the 1st method I chose because it kept adding to the array and I could dump it later on to view what was happening.. Using the $_GLOBALS['debug_err_msgs']
and $_GLOBALS['errorh_string']
is forcing me to .=
(append) the string to the previous one (which is ok... I didn't think you could go... $_GLOBALS['something'][]
and keep adding to the array like I did before I changed my code. SO.. I made changes as below...
PHP
<?php
error_reporting(E_ALL);
set_error_handler("ErrorHandler");
$_GLOBALS['errorh_string'] = "";
if(DEBUGMODE) $_GLOBALS['debug_err_msgs'] = "";
if(DEBUGMODE) $_GLOBALS['debug_err_msgs'] .= 'La la la, some errors';
if(DEBUGMODE) $_GLOBALS['debug_err_msgs'] .= 'more errors... etc';
function ErrorHandler($errno, $errstr, $errfile, $errline)
{
// if ($errno == 8) return;// 8 is undefined variables
$error = "<b>Error[</b>$errno<b>] </b>$errstr<br />";
$_GLOBALS['errorh_string'] .= $error; // append new error to the global string
return true; // dont execute the php internal error handler
}
?>
ERRORS IM GETTING
Notice: Undefined index: errorh_string in /debugOpenBlock.php on line 14
Notice: Undefined index: errorh_string in /debugOpenBlock.php on line 14
Which in the code above, is INSIDE the function
$_GLOBALS['errorh_string'] .= $error; // GIVES ME UNDEFINED
Here is what's weird... if I change the line to read...
$_GLOBALS['errorh_string'] = $error; // NO ERROR NOW
I even tried
$_GLOBALS['errorh_string'] = $_GLOBALS['errorh_string'] . $error; // GIVES ME UNDEFINED
If 'errorh_string'
is a literal? why do I get undefined in it.!?!??! Am I missing something about GLOBALS?
As I was writting this I was thinking I could have used
global $debug_err_msg[]; // make this array global
instead of changing all my code to the way I have it now but... I'm curious what this problem is now... I hate not knowing something :)
BTW - I just recently turned off register_globals in the PHP.INI file. Could this have anything to do with it (note: I NEVER used $_SESSION['somevariable'] as $somevariable (mainly because I didn't know you could do that but... doesn't matter anyways)).
I've read piles of articles about superglobals, register_globals etc but nothing sheds any light on this..
Awaiting wisdom oh greater than I web developers :)
Solution
I can't find another problem, so the issue seems that you just used the wrong variable name. It is called $GLOBALS
, not $_GLOBALS
- unlike the input arrays. (It's not affected by the register_globals setting btw.)
global $debug_err_msg;
You should actually prefer this method. That makes using the variable more legible than with the $GLOBALS[]
access, and it also shows that you intentionally share that variable.