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

Why do constants have quotes around when defined and class quoteshave no quotes??

P: n/a
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??

Constants: http://www.talkphp.com/showthread.php?t=1151

Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
Dec 11 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??

Constants:http://www.talkphp.com/showthread.php?t=1151

Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
The article tells you as far as I can see.

One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
Dec 11 '07 #2

P: n/a
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
Constants:http://www.talkphp.com/showthread.php?t=1151
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.

The article tells you as far as I can see.

One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
Dec 11 '07 #3

P: n/a
On Dec 11, 9:29 am, adam.timberl...@gmail.com wrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
Constants:http://www.talkphp.com/showthread.php?t=1151
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
The article tells you as far as I can see.
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").

I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
Because when you do this:

define('FOO', 'BAR');

you're calling a function. That function takes parameters (namely a
string and something else). Literal strings must be quoted.

In the case of a class constant:

class foo {
const BAR = 'BAZ';
}

you're not calling a function -- it's a language construct. The fact
that you don't put quotes around the BAR is simply the way the
designers of PHP chose to implement it.
Dec 11 '07 #4

P: n/a

<ad*************@gmail.comwrote in message
news:93**********************************@18g2000h sf.googlegroups.com...
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??

Constants: http://www.talkphp.com/showthread.php?t=1151

Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
DON'T MULTI-POST !!!

cross-post if you must, but DON'T multi-post.
Dec 11 '07 #5

P: n/a
adam.timberlake wrote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I was
reading the article below and wondered why are normal constants set with
quotes around them, while "class constants" (those little things I
learned yesterday from the article) don't have to have quotes around the
constants when you define them?
You don't *have* to put quotes around a constant when you define it. e.g.

define('FOOBAR', 'Hello world');
define(FOOBAR, 'Hello world');

are both equivalent. However, the latter will raise an E_NOTICE error.
(Most people tend to suppress E_NOTICE errors anyway as they're a nuisance
and are raised any time anyone ever tries to do anything interesting.)

OK. They're not exactly equivalent. The exact difference in semantics can
be spotted when you try to define the same constant twice...

<?php
error_reporting(E_ALL);
define(BAR, 'BAZ');
define(BAR, 'hello');
echo BAZ . "\n";
?>

versus

<?php
error_reporting(E_ALL);
define('BAR', 'BAZ');
define('BAR', 'hello');
echo BAZ . "\n";
?>

But that's an edge case which you're not likely to have to worry about.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 2:04.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 11 '07 #6

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:p8************@ophelia.g5n.co.uk...
adam.timberlake wrote:
>This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I was
reading the article below and wondered why are normal constants set with
quotes around them, while "class constants" (those little things I
learned yesterday from the article) don't have to have quotes around the
constants when you define them?

You don't *have* to put quotes around a constant when you define it. e.g.

define('FOOBAR', 'Hello world');
define(FOOBAR, 'Hello world');

are both equivalent. However, the latter will raise an E_NOTICE error.
(Most people tend to suppress E_NOTICE errors anyway as they're a nuisance
and are raised any time anyone ever tries to do anything interesting.)

OK. They're not exactly equivalent. The exact difference in semantics can
be spotted when you try to define the same constant twice...

<?php
error_reporting(E_ALL);
define(BAR, 'BAZ');
define(BAR, 'hello');
echo BAZ . "\n";
?>

versus

<?php
error_reporting(E_ALL);
define('BAR', 'BAZ');
define('BAR', 'hello');
echo BAZ . "\n";
?>

But that's an edge case which you're not likely to have to worry about.
toby! that's dangerous thinking. DON'T put yourself in a position to
experience bugs by writting them in! it doesn't matter how remote the
possability is. imagine the amount of hours you'll spend looking for this
bug when the nuance that causes it is a pair of tics. i'd be pissed enough
to walk down to the programmer who did it and slap them on the back of the
head when i finally did!
Dec 11 '07 #7

P: n/a
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
This is a really basic question for all you people out there who know
PHP. This is not a problem but just something I'm confused about. I
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have quotes
around the constants when you define them? It seems really weird to me
why PHP would do it this way??
Constants:http://www.talkphp.com/showthread.php?t=1151
Could someome please explain to me the reasons why there are 2 ways to
define them - one via a function, and one via, what is it? A language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
The article tells you as far as I can see.
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").

I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
You asked "Could someome please explain to me the reasons why there
are 2 ways to
define them - one via a function, and one via,"

I pointed out the answer to that question!
Dec 11 '07 #8

P: n/a
On Tue, 11 Dec 2007 17:35:15 +0100, Steve <no****@example.comwrote:
"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:p8************@ophelia.g5n.co.uk...
>You don't *have* to put quotes around a constant when you define it.
e.g.

define('FOOBAR', 'Hello world');
define(FOOBAR, 'Hello world');

are both equivalent. However, the latter will raise an E_NOTICE error.
(Most people tend to suppress E_NOTICE errors anyway as they're a
nuisance
and are raised any time anyone ever tries to do anything interesting.)

OK. They're not exactly equivalent.

<?php
error_reporting(E_ALL);
define(BAR, 'BAZ');
define(BAR, 'hello');
echo BAZ . "\n";
?>

versus

<?php
error_reporting(E_ALL);
define('BAR', 'BAZ');
define('BAR', 'hello');
echo BAZ . "\n";
?>

But that's an edge case which you're not likely to have to worry about.

toby! that's dangerous thinking. DON'T put yourself in a position to
experience bugs by writting them in! it doesn't matter how remote the
possability is. imagine the amount of hours you'll spend looking for this
bug when the nuance that causes it is a pair of tics. i'd be pissed
enough
to walk down to the programmer who did it and slap them on the back of
the
head when i finally did!
Hear, hear.

I always enable full error_reporting on development BTW, and every one of
them has to go... (well, if it's not code in an external library).
--
Rik Wasmus
Dec 11 '07 #9

P: n/a
On Tue, 11 Dec 2007 17:39:29 +0100, Captain Paralytic
<pa**********@yahoo.comwrote:
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote:
>On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have
quotes
around the constants when you define them? It seems really weird to
me
why PHP would do it this way??
Constants:http://www.talkphp.com/showthread.php?t=1151
Could someome please explain to me the reasons why there are 2 ways
to
define them - one via a function, and one via, what is it? A
language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
The article tells you as far as I can see.
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").

I understand how to do it, I just don't understand why?? Why is one
defined without quotes??

You asked "Could someome please explain to me the reasons why there
are 2 ways to
define them - one via a function, and one via,"

I pointed out the answer to that question!
Indeed, to put it more explicitly:
- after the const keyword the next string cannot be anything else then the
name of the constant.
- the first argument of the define function can be literel strings, other
constants, expression results, etc.
--
Rik Wasmus
Dec 11 '07 #10

P: n/a
On Dec 11, 4:43 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.comwrote:
On Tue, 11 Dec 2007 17:39:29 +0100, Captain Paralytic

<paul_laut...@yahoo.comwrote:
On 11 Dec, 14:29, adam.timberl...@gmail.com wrote:
On 11 Dec, 14:22, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Dec, 14:00, adam.timberl...@gmail.com wrote:
was reading the article below and wondered why are normal constants
set with quotes around them, while "class constants" (those little
things I learned yesterday from the article) don't have to have
quotes
around the constants when you define them? It seems really weird to
me
why PHP would do it this way??
Constants:http://www.talkphp.com/showthread.php?t=1151
Could someome please explain to me the reasons why there are 2 ways
to
define them - one via a function, and one via, what is it? A
language
construct? I don't know. The latter having no quotes around to
define!! Really weird.
The article tells you as far as I can see.
One method defines an applicaion global constant the other one "is
only global to the class it was defined in" (or more properly "is only
global to the class in which it was defined").
I understand how to do it, I just don't understand why?? Why is one
defined without quotes??
You asked "Could someome please explain to me the reasons why there
are 2 ways to
define them - one via a function, and one via,"
I pointed out the answer to that question!

Indeed, to put it more explicitly:
- after the const keyword the next string cannot be anything else then the
name of the constant.
- the first argument of the define function can be literel strings, other
constants, expression results, etc.
--
Rik Wasmus
How do I multi-post?
Dec 11 '07 #11

P: n/a
On Tue, 11 Dec 2007 21:40:07 +0100, <ad*************@gmail.comwrote:
How do I multi-post?
First of all, you wanted to reply to Steve, not me.
Secondly, multi-posting is a lesser evil then cross-posting(well sometimes
it's OK, depends on the subject), normally you stick with just on on-topic
group.
Finally: this is usenet, I have no idea how you'd multipost with Google
Groups, but it seems to work for some people. I'd advise you to get a real
newsreader (/access to a news server, possible supplied by your ISP, there
are free ones around) to reap the full benefits of usenet.
--
Rik Wasmus
Dec 11 '07 #12

P: n/a
Rik Wasmus wrote:
Secondly, multi-posting is a lesser evil then cross-posting(well
sometimes it's OK, depends on the subject), normally you stick with just
on on-topic group.
Quite the opposite! Multi-posting is sending several copies of the same
(or very similar) messages to different groups. This is considered
annoying because someone might help out, giving you a long and thoughtful
answer, and spending a lot of time giving you help, without knowing that
someone has already answered your question in another group.

Cross-posting sends a single copy of the message to multiple groups (like
CCing in e-mail). Replies tend to go to all the groups, so everyone can
see the whole conversation. To cross-post, you normally just type the
names of multiple groups into your "Newsgroups" field, separated by
commas.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 4 days, 8:56.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 11 '07 #13

P: n/a

"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:ic************@ophelia.g5n.co.uk...
Rik Wasmus wrote:
>Secondly, multi-posting is a lesser evil then cross-posting(well
sometimes it's OK, depends on the subject), normally you stick with just
on on-topic group.

Quite the opposite! Multi-posting is sending several copies of the same
(or very similar) messages to different groups. This is considered
annoying because someone might help out, giving you a long and thoughtful
answer, and spending a lot of time giving you help, without knowing that
someone has already answered your question in another group.

Cross-posting sends a single copy of the message to multiple groups (like
CCing in e-mail). Replies tend to go to all the groups, so everyone can
see the whole conversation. To cross-post, you normally just type the
names of multiple groups into your "Newsgroups" field, separated by
commas.
bingo.
Dec 12 '07 #14

P: n/a
On Tue, 11 Dec 2007 23:25:54 +0100, Toby A Inkster
<us**********@tobyinkster.co.ukwrote:
Rik Wasmus wrote:
>Secondly, multi-posting is a lesser evil then cross-posting(well
sometimes it's OK, depends on the subject), normally you stick with just
on on-topic group.

Quite the opposite! Multi-posting is sending several copies of the same
(or very similar) messages to different groups. This is considered
annoying because someone might help out, giving you a long and thoughtful
answer, and spending a lot of time giving you help, without knowing that
someone has already answered your question in another group.

Cross-posting sends a single copy of the message to multiple groups (like
CCing in e-mail). Replies tend to go to all the groups, so everyone can
see the whole conversation. To cross-post, you normally just type the
names of multiple groups into your "Newsgroups" field, separated by
commas.
Blah, offcourse I meant 'cross-posting is a lesser evil then
multi-posting'. For some weird reason I always get the terminology mixed
up...
--
Rik Wasmus
Dec 12 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.