note 106665 added to function.define

From: Date: Fri, 25 Nov 2011 16:33:38 +0000
Subject: note 106665 added to function.define
Groups: php.notes 
Request: Send a blank email to php-notes+get-184678@lists.php.net to get a copy of this message
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'); ---- Server IP: 69.147.83.197 Probable Submitter: 213.209.121.3 ---- Manual Page -- http://www.php.net/manual/en/function.define.php Edit -- https://master.php.net/note/edit/106665 Del: integrated -- https://master.php.net/note/delete/106665/integrated Del: useless -- https://master.php.net/note/delete/106665/useless Del: bad code -- https://master.php.net/note/delete/106665/bad+code Del: spam -- https://master.php.net/note/delete/106665/spam Del: non-english -- https://master.php.net/note/delete/106665/non-english Del: in docs -- https://master.php.net/note/delete/106665/in+docs Del: other reasons-- https://master.php.net/note/delete/106665 Reject -- https://master.php.net/note/reject/106665 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184678) next »