php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22080 NOTICE error reporting fails when an undeclared var is global within function
Submitted: 2003-02-05 14:34 UTC Modified: 2003-02-06 05:42 UTC
From: skander at skander dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.0 OS: Linux
Private report: No CVE-ID: None
 [2003-02-05 14:34 UTC] skander at skander dot com
Hi,

I've turned error_reporting() to die() the execution of my scripts as soon as any error, warning, notice, etc is encountered. 

Yet, in the example global_test() function below, $some_undefined_variable, which is never set, is declared global, and $another_undefined_variable is never set and isn't declared global. isset() returns false on both variables, yet only the line "print $another_undefined_variable;" triggers an error.

Here's the code:
-----------------------
<? 

function allErrorsFatal ($errno, $errstr, $errfile, $errline) {
  global $ERROR_STR;
  echo "<b>Error</b> [" . $ERROR_STR[$errno] . "] $errstr<br>\n";
  echo "Fatal error in line ".$errline." of file ".$errfile."<br>";
  exit -1;
  break;
} 

$ERROR_STR = array(1 => 'ERROR', 2 => 'WARNING', 4 => 'PARSE',
                   8 => 'NOTICE', 16 => 'CORE_ERROR', 32 => 'CORE_WARNING',
                   64 => 'COMPILE_ERROR', 128 => 'COMPILE_WARNING', 256 => 'USER_ERROR',
                   512 => 'USER_WARNING', 1024 => 'USER_NOTICE');

error_reporting (E_ALL);
set_error_handler("allErrorsFatal");


function global_test() {
	global $some_undefined_variable;
	if (isset($some_undefined_variable)) {
		print "some_undefined_variable is set - \n";
	} else {
		print "some_undefined_variable is not set - \n";
	}
	print $some_undefined_variable;
	if (isset($another_undefined_variable)) {
		print "another_undefined_variable is set - \n";
	} else {
		print "another_undefined_variable is not set - \n";
	}
	print $another_undefined_variable;
}

global_test();

?>
---------------------------


Outputs:
---------------------------
some_undefined_variable is not set - 
another_undefined_variable is not set - 
<b>Error</b> [NOTICE] Undefined variable:  another_undefined_variable<br>
Fatal error in line 33 of file /[...]/global_test.php<br>
---------------------------


IMHO, the line "print $some_undefined_variable;" should trigger the notice. Apparently, the global keyword incorrectly affects the way $some_undefined_variable is handled by the parser later on.

-Kai Schutte

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-06 05:42 UTC] mgf@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The global declaration *defines* the variable (effectively setting its value to NULL).  isset() returns FALSE for variables which are not set or which are set to NULL.  Hence, both isset()s return FALSE, but the first print succeeds because $some_undefined_variable is set to NULL, whilst the second fails because $another_undefined_variable is, in fact, undefined!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 04:01:34 2024 UTC