-1

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?

serezha93
  • 125
  • 1
  • 1
  • 10
  • 1
    why do you think it can not be expression? – skyboyer Aug 10 '18 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 '18 at 15:30
  • 1
    Constants are meant to be constant for a single invocation of a script, not every invocation ever made of it. – Alex Howansky Aug 10 '18 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 '18 at 15:31
  • Possible duplicate of [define() vs const](https://stackoverflow.com/questions/2447791/define-vs-const) – iainn Aug 10 '18 at 15:47

3 Answers3

1

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.

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

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.

Due to language design/specifications

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

Paolo
  • 15,233
  • 27
  • 70
  • 91
0

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.

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.

gbalduzzi
  • 9,356
  • 28
  • 58
0

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.

Yann
  • 317
  • 2
  • 8