note 21150 modified in language.functions by levim

From: Date: Thu, 05 Dec 2013 22:48:46 +0000
Subject: note 21150 modified in language.functions by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197772@lists.php.net to get a copy of this message
Consider: <?php function a() { function b() { echo 'I am b'; } echo 'I am a'; } a(); a(); ?> As you might NOT expect, the second call to a() fails with a "Cannot redeclare b()" error. This behaviour is correct, insofar as PHP doesn't "allow functions to be redefined." A work around: function a() { if ( ! function_exists('b') ) { function b() { echo 'I am b'; } } echo 'I am a'; } --was-- Consider: function a() { function b() { echo 'I am b'; } echo 'I am a'; } a(); a(); As you might NOT expect, the second call to a() fails with a "Cannot redeclare b()" error. This behaviour is correct, insofar as PHP doesn't "allow functions to be redefined." A work around: function a() { if ( ! function_exists('b') ) { function b() { echo 'I am b'; } } echo 'I am a'; } http://php.net/manual/en/language.functions.php

« previous php.notes (#197772) next »