Stumped with DEFINES

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
thunderace
Forum Newbie
Posts: 6
Joined: Thu Jul 28, 2005 1:15 am

Stumped with DEFINES

Post 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

Code: Select all

THIS_' . $a . '_DEFINED
Thanks

Rob
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

I did not know that, thanks Jay.

Wayne
thunderace
Forum Newbie
Posts: 6
Joined: Thu Jul 28, 2005 1:15 am

Post 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 :lol:
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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.
thunderace
Forum Newbie
Posts: 6
Joined: Thu Jul 28, 2005 1:15 am

Post 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';

Code: Select all

echo 'THIS_' . $pet . '_DEFINE';
Results in THIS_CAT_DEFINE not the variable value which is what I need.

Hope that clarifies.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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 :)
thunderace
Forum Newbie
Posts: 6
Joined: Thu Jul 28, 2005 1:15 am

Post 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?
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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.
thunderace
Forum Newbie
Posts: 6
Joined: Thu Jul 28, 2005 1:15 am

Post 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;
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I've already told you how to do it once...
jayshields wrote:

Code: Select all

echo constant("THIS_".$a."_DEFINED");
thunderace
Forum Newbie
Posts: 6
Joined: Thu Jul 28, 2005 1:15 am

Post by thunderace »

jayshields

Thanks very much I somehow missed it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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