constant definition doesn't return value? 
Author Message
 constant definition doesn't return value?

I was hoping that define() returns a value. Consider the following:

echo define('HELLO', 3);

Which outputs "1". Thats a pitty becouse otherwise one could use it as
follows:

$max_message_id = 0;
$error_array[ define(E_SOME_ERROR_MESSAGE, $max_message_id++)] = "You
should not do that, it is illegal";
$error_array[ define(E_SOME_OTHER_ERROR_MESSAGE, $max_message_id++)] =
"Dont do that again";

Is my view correct on the define() language construct?



Mon, 19 Sep 2005 17:50:52 GMT  
 constant definition doesn't return value?

Quote:

> I was hoping that define() returns a value. Consider the following:

> echo define('HELLO', 3);

> Which outputs "1". Thats a pitty becouse otherwise one could use it as
> follows:

> $max_message_id = 0;
> $error_array[ define(E_SOME_ERROR_MESSAGE, $max_message_id++)] = "You
> should not do that, it is illegal";
> $error_array[ define(E_SOME_OTHER_ERROR_MESSAGE, $max_message_id++)] =
> "Dont do that again";

> Is my view correct on the define() language construct?

Define is meant to define constants. Redefinition should be illegal but
might not be in php.

define === constant

if you want an incrementing value the just use a variable.



Mon, 19 Sep 2005 19:34:39 GMT  
 constant definition doesn't return value?

Quote:

> I was hoping that define() returns a value. Consider the following:

> echo define('HELLO', 3);

> Which outputs "1". Thats a pitty becouse otherwise one could use it as
> follows:

Read the manual, define returns true or false (eq. 1 or 0)
http://www.php.net/manual/en/function.define.php

HTH;
JOn



Mon, 19 Sep 2005 19:20:45 GMT  
 constant definition doesn't return value?

Quote:
> I was hoping that define() returns a value. Consider the following:

> echo define('HELLO', 3);

> Which outputs "1". Thats a pitty becouse otherwise one could use it as
> follows:

> $max_message_id = 0;
> $error_array[ define(E_SOME_ERROR_MESSAGE, $max_message_id++)] = "You
> should not do that, it is illegal";
> $error_array[ define(E_SOME_OTHER_ERROR_MESSAGE, $max_message_id++)] =
> "Dont do that again";

> Is my view correct on the define() language construct?

I don't understand why you would think that define() would return the value
that you set a constant to, or even the name of the constant.  The reason it
returns 1 is because define is returning the answer to the question:

Did the function define('YOUR_CONSTANT', 5) execute properly? TRUE or 1 :
FALSE or 0;

In other words, there are only 2 values that define() will return.  On the
other hand, if you define your constants first, you can use the following:

define('E_SOME_ERROR_MESSAGE', $max_message_id);
$error_array[E_SOME_ERROR_MESSAGE] = "You should not do that, it is
illegal";
$max_message_id++;
define('E_SOME_OTHER_ERROR_MESSAGE', $max_message_id);
$error_array[E_SOME_OTHER_ERROR_MESSAGE] = "Dont do that again";

You can use variables to set constants, but you cannot redefine a constant.



Tue, 20 Sep 2005 07:30:59 GMT  
 constant definition doesn't return value?

Quote:
> Did the function define('YOUR_CONSTANT', 5) execute properly? TRUE or 1 :
> FALSE or 0;

That was my understanding from the manual. (Contrary to what Kevin and
Jon were thinking, id _did_ read the manual.)

Quote:
> define('E_SOME_ERROR_MESSAGE', $max_message_id);
> $error_array[E_SOME_ERROR_MESSAGE] = "You should not do that, it is
> illegal";
> $max_message_id++;
> define('E_SOME_OTHER_ERROR_MESSAGE', $max_message_id);
> $error_array[E_SOME_OTHER_ERROR_MESSAGE] = "Dont do that again";

> You can use variables to set constants, but you cannot redefine a constant.

Thats how i'm using it now but it requires me two lines (and the need
to keep them in sync). It would have been nice to have a language
construct that would return the value or the name of the constant
itself. (Such a costruct could have the limitation of not being able
to set a constant too null, such that one would be able to check
wether the 'call' was succesfull.)

Ah well... back to my coffee :-) Thanks for your reactions!



Tue, 20 Sep 2005 14:59:43 GMT  
 constant definition doesn't return value?
Jilles Oldenbeuving:

Quote:
>> define('E_SOME_ERROR_MESSAGE', $max_message_id);
>> $error_array[E_SOME_ERROR_MESSAGE] = "You should not do that, it is
>> illegal";
>> $max_message_id++;
>> define('E_SOME_OTHER_ERROR_MESSAGE', $max_message_id);
>> $error_array[E_SOME_OTHER_ERROR_MESSAGE] = "Dont do that again";

>> You can use variables to set constants, but you cannot redefine a
>> constant.

> Thats how i'm using it now but it requires me two lines (and the need
> to keep them in sync).

You could of course use get_defined_constants() and perform a loop over this
array. You'd probably have to use some sort of naming convention to
separate "your" constants from other constants, but that shouldn't cause
too much problems (be careful not to use a prefix which is in use by some
extension though).

Andr N?ss



Tue, 20 Sep 2005 15:05:17 GMT  
 constant definition doesn't return value?

Quote:
> That was my understanding from the manual. (Contrary to what Kevin and
> Jon were thinking, id _did_ read the manual.)

Taken from the manual:
bool define ( string name, mixed value [, bool case_insensitive])
http://www.php.net/manual/en/function.define.php

bool means it returns a boolean value.

Quote:

> > define('E_SOME_ERROR_MESSAGE', $max_message_id);
> > $error_array[E_SOME_ERROR_MESSAGE] = "You should not do that, it is
> > illegal";
> > $max_message_id++;
> > define('E_SOME_OTHER_ERROR_MESSAGE', $max_message_id);
> > $error_array[E_SOME_OTHER_ERROR_MESSAGE] = "Dont do that again";

> > You can use variables to set constants, but you cannot redefine a
constant.

> Thats how i'm using it now but it requires me two lines (and the need
> to keep them in sync). It would have been nice to have a language
> construct that would return the value or the name of the constant
> itself. (Such a costruct could have the limitation of not being able
> to set a constant too null, such that one would be able to check
> wether the 'call' was succesfull.)

> Ah well... back to my coffee :-) Thanks for your reactions!



Tue, 20 Sep 2005 17:49:12 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. return -code return doesn't play nice with set

2. Initialising a constant with a return value from a function

3. Constants for catch return values?

4. Clearing Error()'s and Errorcode()'s return values

5. Change request: return value from 'trap'

6. Returning a variable doesn't work

7. RECORDS( MFL:Key0 ) doesn't return correct record number

8. Deallocate doesn't return memory

9. close $pipe doesn't return non-zero exit status

10. Non-blocking close doesn't return immediately on command pipeline

11. kill doesn't return error?

12. exec of daemons doesn't return

 

 
Powered by phpBB® Forum Software