/    Sign up×
Community /Pin to ProfileBookmark

Problem handling constants

I need your help to solve a problem I’ve bumped into.

The best way to explain it is with a simple example (how bad sounds this!!), better than to put much more complex code:

[code=php]<?php
define(“myConstant1”, 3);
define(“myConstant2”, 5);
define(“myConstant3”, 7);
define(“myConstant4”, 9);

echo “<p>My Constants: “.myConstant1.”, “.myConstant2.”, “.myConstant3.”, “.myConstant4.”</p>”;

$constants_list = array(‘myConstant1’, ‘myConstant2’, ‘myConstant3’, ‘myConstant4’);

echo “<p>My Constants: “;
for ($i=0; $i < 4; $i++) {
echo $constants_list[$i].” “;
}
echo “</p>”;

?>[/code]

The “for” loop outputs the name of the constants, and what I want is to output their value. How could I do this? I tried with eval() but I got an error syntax.

Any help please?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@JazztronikauthorFeb 18.2008 — oops! got it!

just had to do this:

[code=php]$constants_list = array(myConstant1, myConstant2, myConstant3, myConstant4);[/code]

But what if I had to store the string names of the constants in the array?? Some suggestion?
Copy linkTweet thisAlerts:
@bokehFeb 18.2008 — what if I had to store the string names of the constants in the array?[/QUOTE]There is no way for a script to be self aware in that respect.
Copy linkTweet thisAlerts:
@JazztronikauthorFeb 18.2008 — You mean there's absolutely no way to parse the strings of the array as names of previously declared constants?
Copy linkTweet thisAlerts:
@bokehFeb 18.2008 — What you are asking doesn't make any sense. The whole point of a constant is that it is constant (unchanging for the duration of the script). If you could access a constant via a variable that would no longer be the case. Also there is no way for a script to be self aware with respect to a script being able to discover the name of a particular constant.
Copy linkTweet thisAlerts:
@NogDogFeb 19.2008 — Yes, there is a way: the [url=http://us2.php.net/manual/en/function.constant.php]constant[/url]() function:
[code=php]
define("TEST", "This is a test.");
$var = "TEST";
echo "The value of constant '$var' is '" . constant($var) . "'.";
[/code]
Copy linkTweet thisAlerts:
@bokehFeb 19.2008 — Which defeats the point of a constant as pointed out above and is not self aware.
Copy linkTweet thisAlerts:
@NogDogFeb 19.2008 — Which defeats the point of a constant as pointed out above and is not self aware.[/QUOTE]
I do not understand what you mean. It does exactly what is requested: it allows the dynamic selection via a variable as to which constant value is used. What does self-awareness have to do with it? What am I failing to understand? Why did the builders of PHP decide to create the constant() function if it "defeats the point of a constant...and is not self aware"? It does not change the value of the constant, and it will only return a value if the specified constant exists, else null. What's wrong with that?
Copy linkTweet thisAlerts:
@JazztronikauthorFeb 19.2008 — Thank you NogDog, you hit the nail. ?

I think there must be a good way to use this function. My problem came up when using OSCommerce, and this application uses a lot of constants all through the PHP files. There's an included file for each language where all of these constants are defined (they store texts of sections, error messages, etc). There must be some certain way to benefit from the constant() function.
×

Success!

Help @Jazztronik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...