> From: "Al" [EMAIL PROTECTED] Friday, May 21, 2004 9:02 AM > Subject: [PHP] CONSTANTS and good coding practice
> Can someone explain to me the value of using defined custom constants, > in the context of good coding practice. Constants are useful anywhere you find yourself typing a constant (i.e. 20, 'Ralph') and the value is used in more than one place in the code. Say you have a script that fetches 10 articles from a db, and then loads them into an html table. You could hard code the 10 into the sql statement, for loop, while loop or what ever logic used to fetch and load the table. If you instead used a constant, then when the client asks that the table now list 15 rows, all you need to do is change the value in one place in the code instead of searching for 10 everywhere and evaluating if the 10 found has anything to do with the logic. Arguably, you could use a variable for the same purpose, but constants can't change value, and thus you protect yourself from self inflicted coding injuries (as well as injection of values over get, post, etc). The only oddity I find in using constants in php, is that you can't use them inside single or double quotes and always have to break out and use concatenation. --- The future will be better tomorrow --- http://www.spiceplace.com/ ---------------------------------------- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php