11, October 2008

PHP and Constants - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: PHP and Constants
« previous next »
Pages: [1] Print

Author Topic: PHP and Constants  (Read 553 times)
Bill Cosby is my Father
*
Posts: 4
28 credits
Members referred : 0


« on: Feb 01, 2008, 10:57:41 PM »

I have been coding in PHP for several years, and it recently struck me that I never use constants in PHP.

I tend to assign constant values to a regular variable rather than use the DEFINE ("foo", "bar") syntax.

Looking through a bunch of random scripts on my computer I notice that not many other people seem to either.

Do you folks use constants as a matter of course with PHP and should I start too?
Atari ST fan
*
Gender: Male
Posts: 8
48 credits
Members referred : 0


« Reply #1 on: Feb 02, 2008, 12:16:33 AM »

Do you folks use constants as a matter of course with PHP and should I start too?
I never have.  Unless it's a constant built into PHP (or built into a CMS/script that I'm working with), I don't use them.

I never liked constants much - for the same reason I don't like globals.  Values should be declared in the appropriate scope, and passed to where they need to be.  I'd much rather declare a variable in a config file and pass it to my functions than make use of a constant.

- Walkere
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6349
38918 credits
Members referred : 374


It's time to use PHP5!


« Reply #2 on: Feb 02, 2008, 10:14:04 AM »

Constants are cool for settings like your database parameters, use them only if you don't need to change the values later in your script


Last blog : Upload images for usage in TinyMCE
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8116
41653 credits
Members referred : 3



« Reply #3 on: Feb 02, 2008, 11:28:31 AM »

I use constants too for settings in my applications (db settings, session settings, etc.)

It is cool to use them, because this way you are sure that you wont override an important variable.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : Free Unlimited Bandwith and disk space to good to be true?
Bill Cosby is my Father
*
Posts: 4
28 credits
Members referred : 0


« Reply #4 on: Feb 02, 2008, 06:26:25 PM »

I always knew they existed, what they did, what they are for and how to use them. 

The thing is I have habitual variables that I use in pretty much everything I do.  For example i use $handle for files, $dbconnect for db connection values, $sql for holding SQL  query strings etc etc

So I guess I have always used these habitual variables to store my constant values, thus I know automatically not to use them. They are my own version of reserved variables I suppose.

I do see how this is not good practise when developing products that will go to a wider community, or applications that will be potentially modified by a 3rd party in the future.
Atari ST fan
*
Gender: Male
Posts: 8
48 credits
Members referred : 0


« Reply #5 on: Feb 02, 2008, 11:14:29 PM »

Just remembered one situation in which constants are VERY helpful.

Let's say you want to create a set of flags as options for a function.  You decide to use the bits in a number to toggle the flags on and off.

So "Flag 1" is represented by the first bit, "Flag 2" is represented by the second bit, etc.  You can define these as constants (1, 2, 4, 8, 16, etc) and then easily combine them with the bitwise "or" function...

For example...
Code:
define("FLAG1", 1);
define("FLAG2", 2);

$flags = FLAG1 | FLAG2;

if ($flags & FLAG2) {
  //  Flag 2 is set, so do whatever we need to do
}

Without constants, it becomes one heck of a headache to work with bitwise operators like this.

- Walkere
Community Supporter ?
Hunky Junky Monky Man!
**
Gender: Male
Posts: 68
436 credits
Members referred : 0


Schwa?


« Reply #6 on: Feb 09, 2008, 05:00:40 AM »

At work, we've used them for database and other credentials.

I personally don't use them on my projects.

Plus - for fun - run benchmarks against them - not that great. Wink
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7563
Tags : php constants variables Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: PHP and Constants
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Oct 11, 2008, 08:53:17 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.905
Total Topics: 7.557
Total Members: 4.150
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: neli67

32 Guests, 3 Users online :

13 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.