Jessica Posted January 30, 2007 Share Posted January 30, 2007 Is there any reason to use constants over variables when creating a config-type file?For instance, I have a file which gets included on every page, which contains paths, settings, etc.Is there any good reason they should be constants rather than variables? The only thing I can see is that "the scope of a constant is global", meaning if I want to use it in a function I don't have to use global $var;Your thoughts? Which is better? Does one save time somehow, etc? Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/ Share on other sites More sharing options...
rantsh Posted January 31, 2007 Share Posted January 31, 2007 Not only that the "Scope" of the constant is global, the constant is [duh!] constant!!!... so there's no chance you re-write it, besides it'll help you separate stuff, say constants for configuration and variables for operations and stuff... Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173272 Share on other sites More sharing options...
corbin Posted January 31, 2007 Share Posted January 31, 2007 I use variables for config things, but I never knew constants were global, nor had I thought about it... I think I might start using constants now lol... Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173278 Share on other sites More sharing options...
utexas_pjm Posted January 31, 2007 Share Posted January 31, 2007 Try this:[code]<?phpdefine('FOO', 'bar');$foo = 'bar';function someThirdPartyCode_BAD(){ global $foo; $foo = 'not-bar'; if($foo == 'bar'){ print "Program Works <br />"; }else{ print "Obscure Bug <br />"; } }function someThirdPartyCode_NOT_AS_BAD(){ define('FOO', 'not-bar'); if(FOO == 'bar'){ print "Program Works <br />"; }else{ print "Obscure Bug <br />"; }}someThirdPartyCode_BAD();someThirdPartyCode_NOT_AS_BAD();?>[/code]I typically create a constants.php file and store all my constant definitions there. Best,Patrick Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173289 Share on other sites More sharing options...
toplay Posted January 31, 2007 Share Posted January 31, 2007 You all said it already. As utexas_pjm code demonstrates, you can't change a constant once it has been defined (PHP will give a notice when the second define is attempted).Global scope and not being able to change the value of a constant are the best advantages for their use. Using constants for filenames, paths and such is good because outside hacking attempts can't change their contents/value. Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173313 Share on other sites More sharing options...
Jessica Posted January 31, 2007 Author Share Posted January 31, 2007 Makes sense, good points guys! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173319 Share on other sites More sharing options...
rantsh Posted January 31, 2007 Share Posted January 31, 2007 but in that code... the constant FOO and the var $foo are 2 different things, actually if you have 2 vars $foO and $Foo, PHP treats them differently... am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173323 Share on other sites More sharing options...
toplay Posted January 31, 2007 Share Posted January 31, 2007 [quote author=rantsh link=topic=124774.msg517660#msg517660 date=1170210755]but in that code... the constant FOO and the var $foo are 2 different things, actually if you have 2 vars $foO and $Foo, PHP treats them differently... am I wrong?[/quote]You are correct. However, the code example has defined 'FOO' constant at the beginning and attempts to define it again within someThirdPartyCode_NOT_AS_BAD() function, and that was what I was referring to (about PHP giving a notice on the second define attempt). Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173332 Share on other sites More sharing options...
pocobueno1388 Posted January 31, 2007 Share Posted January 31, 2007 rantsh - Variables are case sensitive, so yes...PHP would treat those differently. Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173334 Share on other sites More sharing options...
rantsh Posted January 31, 2007 Share Posted January 31, 2007 HAHAHAHA!!!! I DID NOT SCROLL DOWN!!!! LOL!!!!! :D :D :D :D ;D ;D ;D ;D Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173335 Share on other sites More sharing options...
Jessica Posted January 31, 2007 Author Share Posted January 31, 2007 Textpad rocks. Find in Files, Replace in all, and in less than a minute I fixed all my configuration to use Constants. :DIt makes a lot more sense now. Thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173338 Share on other sites More sharing options...
toplay Posted January 31, 2007 Share Posted January 31, 2007 [quote author=rantsh link=topic=124774.msg517674#msg517674 date=1170211435]HAHAHAHA!!!! I DID NOT SCROLL DOWN!!!! LOL!!!!! :D :D :D :D ;D ;D ;D ;D[/quote]LOL - It happens to the best of us. Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173339 Share on other sites More sharing options...
pocobueno1388 Posted January 31, 2007 Share Posted January 31, 2007 Amen. Quote Link to comment https://forums.phpfreaks.com/topic/36398-constants-vs-variables-for-config-files/#findComment-173340 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.