Page 1 of 1
Stumped with DEFINES
Posted: Thu May 17, 2007 2:45 pm
by thunderace
Hi
I'm totally stumped with this but I'm sure there's an easy answer.
Code: Select all
define('THIS_IS_DEFINED', 'my_value');
$a = 'IS';
My question is how to access the value of the define as
Thanks
Rob
Posted: Thu May 17, 2007 2:47 pm
by guitarlvr
in
Code: Select all
define('THIS_IS_DEFINED', 'my_value');
THIS_IS_DEFINED would be the variable name and 'my_value' would be the value. So when accessing the variable you would use $THIS_IS_DEFINED.
Wayne
Posted: Thu May 17, 2007 2:48 pm
by jayshields
Code: Select all
echo constant("THIS_".$a."_DEFINED");
Edit: In regards to the above post, you do not reference constants by prefixing their name with a $, just reference them by their name alone.
Posted: Thu May 17, 2007 2:51 pm
by guitarlvr
I did not know that, thanks Jay.
Wayne
Posted: Thu May 17, 2007 2:52 pm
by thunderace
Thanks
but all I have is
THIS_
$a
_DEFINED
I don't know that $a == 'IS';
from that how do I access the variable?
Am I being thick lol

Posted: Thu May 17, 2007 2:53 pm
by thiscatis
Indeed.
Basic example from php.net
Code: Select all
<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // print "Hello world."
?>
no point in prefixing them with $,
that's like going to the supermarket, buy some food, go back home, spend 1hour cooking a dish and throw it away in the garbage.
Posted: Thu May 17, 2007 3:04 pm
by thunderace
I don't think I explained myself very well .. sorry
Example:
I know that certain defines exist e.g.
THIS_DOG_DEFINE = 'barks a lot';
THIS_CAT_DEFINE = 'meows a lot';
THIS_BUNNY_DEFINE = 'hops a lot';
However all I have is a variable which I have been given, in this case we'll say $pet == 'CAT';
Results in THIS_CAT_DEFINE not the variable value which is what I need.
Hope that clarifies.
Posted: Thu May 17, 2007 3:06 pm
by thiscatis
Why are you trying to make a constant variable?
(if you read the above sentence out loud 2 times you might get the point..)
If you could give some more info about what you are trying to accomplish we might find a solution

Posted: Thu May 17, 2007 3:14 pm
by thunderace
Thanks for the sarcasm
I am modding oscommerce
There are various shipping types that are DEFINED.
Like
MODULE_SHIPPING_ROYALMAIL_ZONE
MODULE_SHIPPING_UPS_ZONE
MODULE_SHIPPING_DHL_ZONE
I am supplied the name of the courier company e.g $var = 'UPS';
But rather than query the database I want to find a way of accessing the defined variable when all I have is ..
MODULE_SHIPPING_
$VAR
_ZONE
Does that make it clearer or should I read the above sentence out loud 2 times so I might get the point?
Posted: Thu May 17, 2007 3:19 pm
by thiscatis
thanks for your counter-sarcasm! It makes life so much more fun.
I haven't seen anything like that before.
You can try to get the constant name, break it down and compare it with the values. possibly as array items.
// I think the problem here is that the name of your constant isn't variable..
// If you have $item, you can't use the .te. for anything, but the value where it's set to.
Posted: Thu May 17, 2007 3:47 pm
by thunderace
Well I found a way but I doubt it's very efficient as there are hundreds of defines ..
Code: Select all
$constantslist = get_defined_constants();
$varstring = 'MODULE_SHIPPING_' . $curr_ship . '_STATUS';
foreach ($constantslist as $key => $value)
if ($key == $varstring) $result = $value;
Posted: Thu May 17, 2007 3:55 pm
by jayshields
I've already told you how to do it once...
jayshields wrote:Code: Select all
echo constant("THIS_".$a."_DEFINED");
Posted: Thu May 17, 2007 4:04 pm
by thunderace
jayshields
Thanks very much I somehow missed it.
Posted: Thu May 17, 2007 4:25 pm
by RobertGonzalez
I don't think you are going to be able to do what you want no matter how many times you read it.