Hello, let's say there is a constant array:

define('constant', array('one', 'two', 'three')); 

I'm trying to do this:

 shuffle(costant); 

And I get:

 Fatal error: Only variables can be passed by reference 

Is it possible not to mix such an array?

  • 3
    then it is a constant that does not change. make a copy and mix it up. $tmp = constant; shuffle($tmp); - teran

1 answer 1

Think about what you are trying to do.

A constant is an identifier (name) for a simple value. As the name implies, their value can not change during the execution of the script

In principle, the constant is not even a variable that cannot be changed. Typically, in various programming languages, the values ​​of constants are substituted into the code at the compilation stage.

Changing the order of the array keys in principle changes it. Two arrays are considered equivalent when

if $ a and $ b contain the same key / value pairs in the same order and the same type.

Therefore, you get an error

Fatal error: Only variables can be passed by reference.

If you want to change the value of a constant during the execution of a code, then obviously you should use a variable rather than a constant.

But with all this, it seems to me that it will be quite enough for you to create a copy of this array, and to work with it already, without touching the original array

 $data = constant; shuffle($data); 

so the wolves are fed and the sheep are whole.