note 78188 deleted from function.define by betz

From: Date: Tue, 04 Dec 2007 22:44:04 +0000
Subject: note 78188 deleted from function.define by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132817@lists.php.net to get a copy of this message
Note Submitter: john dot navratil at sbcglobal dot net ---- Without really thinking, it is an easy assumption (especially for C coders) that defined constants are available after their lexical presence in a script. It is, of course, temporally after the execution of the define statement. All us knuckle-dragging C programmers may be tempted to write: <?php myEcho("Hello, world"); define ("OUTPUT_FILE", "/tmp/asdf"); function myEcho($msg) { $f = fopen(OUTPUT_FILE, "a"); fwrite($f, $msg."\n"); fclose($f); } ?> and expect OUTPUT_FILE to be defined. What you get is " Use of undefined constant OUTPUT_FILE - assumed 'OUTPUT_FILE' " because the 'define()' *FUNCTION* had not been called before its implicit use by myEcho(). Programming is always easier when one thinks, don't you think?

« previous php.notes (#132817) next »