Page 1 of 1
Using Constants in PHP
Posted: Fri Oct 17, 2008 3:31 pm
by volomike
I've found a gotcha with using constants in PHP. Perhaps you can suggest a fix.
If I do the following:
<?= EMAIL ?>
And I had not previously done:
define('EMAIL','
test@test.com');
Then I would end up seeing the word 'EMAIL' appear on the page! {smacks forehead}
Is there a way I can flip that off so that I would see an empty string, instead, such as an INI setting or something?
Re: Using Constants in PHP
Posted: Fri Oct 17, 2008 4:28 pm
by requinix
Don't think so.
You would also see an error message if your settings had them configured right.
Re: Using Constants in PHP
Posted: Fri Oct 17, 2008 4:31 pm
by onion2k
I don't think so. When PHP encounters an undefined constant it assumes it assigns it it's name as a value. I guess you could use echo (defined('EMAIL')) ? EMAIL: "Unknown"; or something to avoid the constant name being displayed. Personally, I always define the constant before I use it. In an include called constants.inc.php.

Re: Using Constants in PHP
Posted: Fri Oct 17, 2008 4:32 pm
by onion2k
tasairis wrote:Don't think so.
You would also see an error message if your settings had them configured right.
It'd be a warning message, not an error message. /pedantic
Re: Using Constants in PHP
Posted: Fri Oct 17, 2008 4:56 pm
by alex.barylski
Should a constant not be defined regardless? Isn't that the design idea behind a constant? In some languages (C++) you can initialize a constant at runtime and it effectivelys remains unchanged but in PHP define (IMHO) takes on a slightly different role.
I would use a variable (GLOBAL if neccessary)
Re: Using Constants in PHP
Posted: Fri Oct 17, 2008 5:24 pm
by requinix
onion2k wrote:tasairis wrote:Don't think so.
You would also see an error message if your settings had them configured right.
It'd be a warning message, not an error message. /pedantic
E_WARNING, E_ERROR, same difference
PCSpectra wrote:Should a constant not be defined regardless? Isn't that the design idea behind a constant? In some languages (C++) you can initialize a constant at runtime and it effectivelys remains unchanged but in PHP define (IMHO) takes on a slightly different role.
I would use a variable (GLOBAL if neccessary)
define() behaves the same, the problem you're having is that you want to use the constant before it's been defined. What happens in C++ when you try to use an undefined variable? It doesn't compile or it crashes at runtime. PHP handles it a bit more elegantly, and in a way that makes sense in 99% of the times that it happens.
If you want to leave EMAIL "undefined" then just define it using an empty string.
Code: Select all
define("EMAIL", ($condition ? "test@test.com" : ""));
Re: Using Constants in PHP
Posted: Fri Oct 17, 2008 11:48 pm
by volomike
I found a little-known API in PHP that lets you see what you can set or not set.
Code: Select all
<?php
echo "<small><pre>\n";
print_r(ini_get_all());
echo "\n</pre></small>";
I didn't see anything for constants except error_reporting, which you could use to let you know about errors of undefined constants. I'm still trying to track what's the best value there that would stop the program if you tried to use an undefined constant. On my workstation, the default is 6135.