note 72069 added to function.define

From: Date: Mon, 01 Jan 2007 02:59:07 +0000
Subject: note 72069 added to function.define
Groups: php.notes 
Request: Send a blank email to php-notes+get-121155@lists.php.net to get a copy of this message
An improvement on the function from bobbykjack at yahoo dot co dot uk on the concept by richard dot quadling at bandvulc dot co dot uk: <?php function adefine($constant_name, $value=null) { static $increment = 0; // 1 for bitmask if (is_null($value)) { define($constant_name, ++$increment); // $increment=$increment<<1 for bitmask } else { define($constant_name, $value); if (is_numeric($value)) { $increment = $value; } } } ?> If you pass it a second argument it defines it normally, and resets the increment if the value is numeric. This way the function can replace define, and you can reset the counter for a new set of constants. <?php adefine ('RULE_CALLBACK_FORMAT', 1); // 1 adefine ('RULE_CHANGE_CALLBACK_ON_ERROR'); // 2 adefine ('RULE_CHANGE_COMPARE_DATE'); // 3 adefine('KEYWORD', 'hodgepodge'); // hodgepodge <-- defined normally adefine ('RULE_CHANGE_ON_DATE'); // 4 adefine ('ERROR_DESC', 1); // 1 <-- Counter reset adefine ('ERROR_EXPECTED_RESULT'); // 2 ?> ---- Server IP: 64.71.164.2 Probable Submitter: 207.32.22.145 ---- Manual Page -- http://www.php.net/manual/en/function.define.php Edit -- https://master.php.net/note/edit/72069 Del: integrated -- https://master.php.net/note/delete/72069/integrated Del: useless -- https://master.php.net/note/delete/72069/useless Del: bad code -- https://master.php.net/note/delete/72069/bad+code Del: spam -- https://master.php.net/note/delete/72069/spam Del: non-english -- https://master.php.net/note/delete/72069/non-english Del: in docs -- https://master.php.net/note/delete/72069/in+docs Del: other reasons-- https://master.php.net/note/delete/72069 Reject -- https://master.php.net/note/reject/72069 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#121155) next »