Re: [PHP] Constant questions

2004-05-19 Thread Don Read

On 12-May-2004 René Fournier wrote:
> Hi,
> 
> I have two questions involving Constants.
> 
> 1. I want to refer to a refer to a Constant by its value (which is 
> unique), and return its name. E.g.,:
> 
>   define ("SEND_DS","1");
>   define ("SEND_DS_ACK","2");
>   define ("RESEND_DS","3");
>   define ("STARTUP_DS","12");
> 
> For example, if I receive "3", I would like to echo "RESEND_DS"--the 
> name of the constant. Is there a simply way to do this? Or am I
> better 
> using an Associative Array (which is what I was thinking)? Then I
> could 
> such refer to an element by its key or value (both of which are 
> unique). 

Both.

get_defined_constants()
array_flip()

Regards,

-- 
Don Read [EMAIL PROTECTED]
-- It's always darkest before the dawn. So if you are going to 
   steal the neighbor's newspaper, that's the time to do it.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constant questions

2004-05-12 Thread Curt Zirzow
* Thus wrote Ren Fournier ([EMAIL PROTECTED]):
> Hi,
> 
> I have two questions involving Constants.
> 
> 1. I want to refer to a refer to a Constant by its value (which is 
> unique), and return its name. E.g.,:
> 
>   define ("SEND_DS","1");
>   define ("SEND_DS_ACK","2");
>   define ("RESEND_DS","3");
>   define ("STARTUP_DS","12");
> 
> For example, if I receive "3", I would like to echo "RESEND_DS"--the 
> name of the constant. Is there a simply way to do this? Or am I better 
> using an Associative Array (which is what I was thinking)? Then I could 
> such refer to an element by its key or value (both of which are 
> unique). I suppose this more of a performance/elegance issue, than 
> outright problem. Just curious what you think.

Constants are more for the programmer to use within the program to
avoid hard coding arbitrary numbers embeded deep inside code.

You can use a combination of both Constants and arrays to achive
your task:

define('SEND_DS', 1);
define('SEND_DS_ACK', 2);
...

$lookup = array(
  SEND_DS  => 'SEND_DS',
  SEND_DS_ACK  => 'SEND_DS_ACK',
);


Curt
-- 
"I used to think I was indecisive, but now I'm not so sure."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Constant questions

2004-05-12 Thread Richard Davey
Hello René,

Wednesday, May 12, 2004, 6:29:13 PM, you wrote:

RF> Such that the output will be MY_NAME. If I echo $val, the output will
RF> be Rene. But I want to see the constants Name, not Value. Any ideas?

No easy way I can think of, but you could do:

$array = get_defined_constants();

and then search $array for your value, returning the key if found.

-- 
Best regards,
 Richard Davey
 http://www.launchcode.co.uk / PHP Development Services
 http://www.phpcommunity.org/wiki/296.html / PHP Community

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Constant questions

2004-05-12 Thread René Fournier
Hi,

I have two questions involving Constants.

1. I want to refer to a refer to a Constant by its value (which is 
unique), and return its name. E.g.,:

define ("SEND_DS","1");
define ("SEND_DS_ACK","2");
define ("RESEND_DS","3");
define ("STARTUP_DS","12");
For example, if I receive "3", I would like to echo "RESEND_DS"--the 
name of the constant. Is there a simply way to do this? Or am I better 
using an Associative Array (which is what I was thinking)? Then I could 
such refer to an element by its key or value (both of which are 
unique). I suppose this more of a performance/elegance issue, than 
outright problem. Just curious what you think.

2. Let's say I have a Constant called MY_NAME, the value of which is 
"Rene", and I pass it to a function, such as:

function example ($val) {
echo ???;
}
example (MY_NAME);

Such that the output will be MY_NAME. If I echo $val, the output will 
be Rene. But I want to see the constants Name, not Value. Any ideas?

Thanks.

...Rene

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php