note 78188 added to function.define

From: Date: Tue, 02 Oct 2007 14:38:42 +0000
Subject: note 78188 added to function.define
Groups: php.notes 
Request: Send a blank email to php-notes+get-130984@lists.php.net to get a copy of this message
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? ---- Server IP: 209.41.74.194 Probable Submitter: 67.65.165.249 ---- Manual Page -- http://www.php.net/manual/en/function.define.php Edit -- https://master.php.net/note/edit/78188 Del: integrated -- https://master.php.net/note/delete/78188/integrated Del: useless -- https://master.php.net/note/delete/78188/useless Del: bad code -- https://master.php.net/note/delete/78188/bad+code Del: spam -- https://master.php.net/note/delete/78188/spam Del: non-english -- https://master.php.net/note/delete/78188/non-english Del: in docs -- https://master.php.net/note/delete/78188/in+docs Del: other reasons-- https://master.php.net/note/delete/78188 Reject -- https://master.php.net/note/reject/78188 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130984) next »