note 106665 deleted from function.define by danbrown

From: Date: Tue, 29 Nov 2011 15:44:21 +0000
Subject: note 106665 deleted from function.define by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184791@lists.php.net to get a copy of this message
Note Submitter: Frogger ---- remark to webmaster@PersonalInformationTechnology's note: The better way is to use all the nice functions php provides, such as defined(), array_key_exists(), property_exists() and so on. A variable test like if ($var) will fail in many ways (in reference to what the developer expected). In this specific case it seems to be obvious to write <?php if(defined('CONST_DEFINED') && CONST_DEFINED) {/* use them */} ?> But this will only let you know, CONST_DEFINED has been defined and has any (positive) value - which could also be a string, a number or something other. <?php if(defined('CONST_DEFINED') && CONST_DEFINED === true) {/* use them */} ?> should be the option used. This guarantees CONST_DEFINED is a defined constant AND that it has a boolean type due to the implicit type compare of the === I.e.: Similar results will give if( array_key_exists('myKey', $myArray)) instead of if (isset($myArray['myKey',])) which could return unexpected results. In case of include necessary files use require() to ensure they have been included or include the lines if (!defined('MY_CONST')) define('MY_CONST', 'any default value or a value which results in a caught error');

« previous php.notes (#184791) next »