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?
Using Constants in PHP
Moderator: General Moderators
Re: Using Constants in PHP
Don't think so.
You would also see an error message if your settings had them configured right.
You would also see an error message if your settings had them configured right.
Re: Using Constants in PHP
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
It'd be a warning message, not an error message. /pedantictasairis wrote:Don't think so.
You would also see an error message if your settings had them configured right.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Using Constants in PHP
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)
I would use a variable (GLOBAL if neccessary)
Re: Using Constants in PHP
E_WARNING, E_ERROR, same differenceonion2k wrote:It'd be a warning message, not an error message. /pedantictasairis wrote:Don't think so.
You would also see an error message if your settings had them configured right.
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.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)
If you want to leave EMAIL "undefined" then just define it using an empty string.
Code: Select all
define("EMAIL", ($condition ? "test@test.com" : ""));- volomike
- Forum Regular
- Posts: 633
- Joined: Wed Jan 16, 2008 9:04 am
- Location: Myrtle Beach, South Carolina, USA
Re: Using Constants in PHP
I found a little-known API in PHP that lets you see what you can set or not set.
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.
Code: Select all
<?php
echo "<small><pre>\n";
print_r(ini_get_all());
echo "\n</pre></small>";