Why DEFINE in PHP can accept random values?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why DEFINE in PHP can accept random values?



Constants can't accept expressions that make calculations. Why can I declare DEFINE with random_int(1,4) and not get a fatal error? In this case, the value of the constant will be different every time the page loads. Is this correct for the ideology of constants?


define('RANDOM_NUMBER', random_int(2,4));



It's ideology question. Why is this correct in PHP? And why can use the expression in DEFINE but not in constants?





why do you think it can not be expression?
– skyboyer
Aug 10 at 15:29





Because define is executed at runtime. First, the expression is evaluated, then assigned to the constant. Where’s the problem?
– lxg
Aug 10 at 15:30


define





Constants are meant to be constant for a single invocation of a script, not every invocation ever made of it.
– Alex Howansky
Aug 10 at 15:31





You define it as the number that is generated. Not different than define('RANDOM_NUMBER', 3). The random number is calculated before it gets defined
– Chad K
Aug 10 at 15:31





Possible duplicate of define() vs const
– iainn
Aug 10 at 15:47




3 Answers
3



Constants can't accept expressions that make calculations.



This is not true.



define may accept as second parameter expressions, included values returned from a called function.


define



The restrictions for the value parameter of define are (from the manual)


value



value



The value of the constant. In PHP 5, value must be a scalar value (integer, float, string, boolean, or NULL). In PHP 7, array values are also accepted.



Of course a constant cannot be defined twice.



And -if I understand your question- this is the "ideology" of constants.



As they are defined the value cannot be modified in another part of the script as it's constant.



Of course if the script is run a second time the constant can get a different value like in your case.



Worth mentioning this is different for Class Constants - constants you declare inside a class definition with the keyword const.


const



Due to language design/specifications



The value must be a constant expression, not (for example) a variable, a property, or a function call.





I meant the const in the class when said that they can't accept expressions.
– serezha93
Aug 10 at 15:36



Because the define function only get the result of random_int(2,4) as parameter and has no way to understand if it was created in a random or in a deterministic way.


define


random_int(2,4)



What the program really does is:


$temp = random_int(2,4);
define("RANDOM_NUMBER", $x);



That said, the define is used to define a constant for a request script, not necessarily a constant for all requests, so it does make sense that every different request has a different value for the defined constant.



The official php documentation states:



While it is possible to define resource constants, it is not
recommended and may cause unpredictable behavior.



The same will apply to any non scalar values assigned to a DEFINE constant. The PHP documentation states that:



The value of the constant. In PHP 5, value must be a scalar value
(integer, float, string, boolean, or NULL). In PHP 7, array values are
also accepted.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard