Using Constants in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Using Constants in PHP

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using Constants in PHP

Post by requinix »

Don't think so.
You would also see an error message if your settings had them configured right.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Using Constants in PHP

Post 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. :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Using Constants in PHP

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Using Constants in PHP

Post 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)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using Constants in PHP

Post 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" : ""));
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Using Constants in PHP

Post 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.
Post Reply