note 108660 deleted from function.set-error-handler by danbrown

From: Date: Tue, 15 May 2012 20:46:27 +0000
Subject: note 108660 deleted from function.set-error-handler by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187996@lists.php.net to get a copy of this message
Note Submitter: scarywound at google mail ---- <?php // Assume this function definition: function foo($bar, $baz = 'qux') { return $bar.$baz; } // Oh crap, something went wrong, and we call it without arguments: $foo = foo(); // Produces: // PHP Warning: Missing argument 1 for foo() bla bla bla // PHP Notice: Undefined variable: bar in bla bla bla // And script doesn't terminate. That's really stupid. // Here is a simple solution: set_error_handler(function($errno, $errstr) { if (preg_match('/Missing argument/', $errstr)==1) trigger_error('Bad luck! Missing argument causes fatal error', E_USER_ERROR); }, E_WARNING); $foo = foo(); // PHP Fatal error: Bad luck! Missing argument causes fatal error in bla bla bla ?>

« previous php.notes (#187996) next »