# [Fwd: [PHP] constant() - php5]

- Thread: https://externals.io/message/17019
- Original archive: https://news-web.php.net/php.internals/17019
- Started by Jochem Maas on 2005-06-29T23:00:57+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #17019 — Jochem Maas — 2005-06-29T23:00:57+00:00

URL: https://externals.io/message/17019#17019

hi,

I just wanted to say about 'include' ... er no actually
I have this little thing with `constant()` which I asked about
on php-generals and the response from a couple of regulars
suggest it might be a bug, or more correctly, an inconsistency.

Jason Barnett wrote also:
> tested with PHP 5.0.5-dev
> 
> <?php
> 
> /* Causes `E_WARNING` */
> echo "constant('UNDEFINED_CONSTANT')\n";
> echo constant('UNDEFINED_CONSTANT');
> 
> /* Causes `E_NOTICE` */
> echo "UNDEFINED_CONSTANT\n";
> echo UNDEFINED_CONSTANT;
> 
> ?> 

should(may) I submit a bugreport?

-------- Original Message --------
anybody here know what the logic is behind `constant()` triggering a warning
if the named constant is not found?

contrived example:

<?

$cnst = "DEBUG";

// what I want to do but can't
if ($dbg = constant($cnst)) {
	// do stuff
}

// the only real option, it seems - bit long winded to get round a stupid (IMHO)
if (defined($cnst) && ($dbg = constant($cnst))) {
	// do stuff
}

PLEASE - nobody need bother to tell me about the @ operator, I am
aware of it but I don't want to use it in this case. (addition: not for a warning)

thanks & rgds,
jochem

---

## #17057 — Jochem Maas — 2005-07-01T13:25:25+00:00

URL: https://externals.io/message/17019#17057

Pawel Bernat wrote:
> > […]
> 
> What is stupid with validating name before using it?

its inconsistent to trigger an `E_WARNING` when doing:

echo constant('CNST');

when:

echo CNST;

only triggers an E_NOTICE.
(assuming, in both cases that CNST is not defined).

IMHO it should at most trigger an E_NOTICE.

> 
> p.

---

## #17058 — Derick Rethans — 2005-07-01T13:43:30+00:00

URL: https://externals.io/message/17019#17058 · In reply to: #17057

> echo constant('CNST');
> 
> when:
> 
> echo CNST;
> 
> only triggers an E_NOTICE.
> (assuming, in both cases that CNST is not defined).
> 
> IMHO it should at most trigger an E_NOTICE.

Did you compare the output of the two statements? 
echo constant('CNST');
shows nothing (except the warning)

echo CNST;
shows "CNST" (and a notice).

This makes perfect sense to me to differentiate between them like this.

Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #17060 — Jochem Maas — 2005-07-01T14:13:44+00:00

URL: https://externals.io/message/17019#17060 · In reply to: #17058

Derick Rethans wrote:
> 
> 
> 
> > […]
> 
> 
> Did you compare the output of the two statements? 

I did.

> echo constant('CNST');
> shows nothing (except the warning)
> 
> echo CNST;
> shows "CNST" (and a notice).
> 
> This makes perfect sense to me to differentiate between them like this.

ok - agreed that the echo behaviour is logical - but I wasn't actually
pertaining to the echo behaviour (and what was being echo'ed wasn't relevant to my
original question).

my point is that using a constant directly in your code when that constant doesn't exist
only causes an `E_NOTICE` but passing a string to `constant()` when a constant of the given
name doesn't exist causes an E_WARNING.

I would either expect both to cause the same level of error OR that trying to
use an undefined constant directly in code would cause a lower level of error.

but if you say the error output behaviour is expected/correct/desired then I'm
happy to except it (and adjust my expectations accordingly)
- if you (anyone) could explain why (because I don't grok the logic behind
this behaviour) I would be very grateful, maybe it will bring me one step closer
to being able to call myself a real programmer. :-/

anyway thanks for taking the time to reply,
I gather that you, Derick (amongst others!), have a plate full of PHP work
in the form of unicode and date related stuff (which I am very much looking
forward to!) - i.e. you are busy-busy, time is short, etc etc.

kind rgds,
Jochem


> 
> Derick

---

## #17063 — Nicholas Telford — 2005-07-01T16:07:08+00:00

URL: https://externals.io/message/17019#17063 · In reply to: #17060

Due to PHPs dynamic typing, unquoted strings are treated as strings 
unless a constant by that name exists. Thankfully it's clever enough to 
raise a notice to tell you it couldn't find a constant by that name, 
which makes debugging much easier.

The reason `constant()` throws a warning rather than a notice is because 
PHP knows you're looking for a constant by that name and flags it as a 
more serious error, wheras before, it could just be that you want to use 
an unquoted string :)

If you think it's a bit strange, it may seem so, but logically, if an 
unquoted number is equivilent to it's quoted counter-part, the same must 
be true for strings.

Nicholas Telford

Jochem Maas wrote:
> Derick Rethans wrote:
> 
> > […]
> 
> 
> I did.
> 
> > […]
> 
> 
> ok - agreed that the echo behaviour is logical - but I wasn't actually
> pertaining to the echo behaviour (and what was being echo'ed wasn't 
> relevant to my
> original question).
> 
> my point is that using a constant directly in your code when that 
> constant doesn't exist
> only causes an `E_NOTICE` but passing a string to `constant()` when a 
> constant of the given
> name doesn't exist causes an E_WARNING.
> 
> I would either expect both to cause the same level of error OR that 
> trying to
> use an undefined constant directly in code would cause a lower level of 
> error.
> 
> but if you say the error output behaviour is expected/correct/desired 
> then I'm
> happy to except it (and adjust my expectations accordingly)
> - if you (anyone) could explain why (because I don't grok the logic behind
> this behaviour) I would be very grateful, maybe it will bring me one 
> step closer
> to being able to call myself a real programmer. :-/
> 
> anyway thanks for taking the time to reply,
> I gather that you, Derick (amongst others!), have a plate full of PHP work
> in the form of unicode and date related stuff (which I am very much looking
> forward to!) - i.e. you are busy-busy, time is short, etc etc.
> 
> kind rgds,
> Jochem
> 
> 
> > […]
