By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Difference between 'const' and 'define()'

P: n/a
What is the difference between 'const' and 'define()' ? When would I
prefer using 'const' over 'define', or vice versa? It seems if i do:

const secondsInMinute = 60;
define("secondsInMinute", 60);

Aren't these two the same thing?

- Jason

Oct 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Jason" <ja********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
What is the difference between 'const' and 'define()' ? When would I
prefer using 'const' over 'define', or vice versa? It seems if i do:

const secondsInMinute = 60;
define("secondsInMinute", 60);

Aren't these two the same thing?
const defines a class constant, while define defines a global constant. You
use const-defined constants inside class using self::constant, not
$this->constant, and a define-defined constant in a normal, non-object shit.
When using const, the value must be a static value, not a result of a
mathematical operation or function. const is restricted in many ways
compared to define, and should be used in classes/objects
--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Oct 12 '06 #2

P: n/a
It's all about the scope.
class BlahBlah
{
const MY_CONSTANT = 45676;
}
define('MY_CONSTANT', 76344);
echo BlahBlah::MY_CONSTANT . "\n" . MY_CONSTANT;
// returns:
// 45676
// 76344
Class constants are the closest thing to namespaces as php will get for this
version.

Jason wrote:
What is the difference between 'const' and 'define()' ? When would I
prefer using 'const' over 'define', or vice versa? It seems if i do:

const secondsInMinute = 60;
define("secondsInMinute", 60);

Aren't these two the same thing?

- Jason
Oct 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.