Dragen Posted June 8, 2007 Share Posted June 8, 2007 Is there any advantage to using define() instead of just setting a variable? I've not really used define before, but was just wondering what difference it made: <?php // What's the difference between this: define('MY_VARIABLE', 'my value'); // and this: $myvariable = 'my value'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54794-solved-some-advice-on-define/ Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 Define can be used in any scope, so functions and classes can use define without having to use global. Define's value is CONSTANT meaning it cannot be modified later on the script, where as $myvariable can be. Define is really for configuration variables and the likes of that. Quote Link to comment https://forums.phpfreaks.com/topic/54794-solved-some-advice-on-define/#findComment-270965 Share on other sites More sharing options...
paul2463 Posted June 8, 2007 Share Posted June 8, 2007 define() defines a CONSTANT , a variable that will never change its value, in your case whenever you call MY_VARIABLE it will always carry the value of 'my value' just creating a variable allows it to be changed whenever a call is made to change it see the <a href="http://uk2.php.net/manual/en/language.constants.php"> manual here </a> for information on constants Quote Link to comment https://forums.phpfreaks.com/topic/54794-solved-some-advice-on-define/#findComment-270966 Share on other sites More sharing options...
accident Posted June 8, 2007 Share Posted June 8, 2007 I was trying to use some defines but have one issue with it a regular variable i would set up like this $randomStuff = "asdf"; echo "whatever $randomStuff"; would output: whatever asdf However, how would you do this with a define. define("randomStuff", "asdf"); echo "whatever randomStuff"; outputs whatever randomStuff.. How would I make it output asdf? Quote Link to comment https://forums.phpfreaks.com/topic/54794-solved-some-advice-on-define/#findComment-270970 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 you'd need to use this, I think: define("randomStuff", "asdf"); echo "whatever " . randomStuff; Thanks for the advice guys! Quote Link to comment https://forums.phpfreaks.com/topic/54794-solved-some-advice-on-define/#findComment-270975 Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 you'd need to use this, I think: define("randomStuff", "asdf"); echo "whatever " . randomStuff; Thanks for the advice guys! That is right, but as a general rule of thumb, all constants should be capital. define("RANDOM_STUFF", "asdf"); echo "whatever " . RANDOM_STUFF; That way incase someone tries to help you out, they do not think it is an error of leaving out the $ for the variable etc. It is good to follow the generic guidelines. Quote Link to comment https://forums.phpfreaks.com/topic/54794-solved-some-advice-on-define/#findComment-271004 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.