Error calling the rand () function when defining a property

-1

I have a "Forgot Password" code. And at the time of generating the random number to send to email, you are giving me this error:

  

Parse error: syntax error, unexpected '(', expecting ',' or ';' on   line 9

Can anyone help me with this?

//Criar um código aleátorio
var $min    = 000001;
var $max    = 100000;
var $codigo = rand($min,$max);
//fim criar código aleátorio
    
asked by anonymous 23.11.2016 / 17:51

2 answers

3

You can not call functions (such as rand ) in the definition of class properties, only in the constructor. The constructor is a special function executed whenever an instance of the class is created, and in PHP it has the name __construct . For example:

class MinhaClasse {

    private $min = 1;
    private $max = 10;
    private $valor;

    function __construct() {
        $this->valor = rand($this->min, $this->max);
    }
}
    
23.11.2016 / 18:06
-1

In PHP the function of generating random numbers is as follows:

mt_rand(inicio, fim)

So, test with your code like this:

//Criar um código aleátorio
var $min    = 000001;
var $max    = 100000;
var $codigo = mt_rand($min,$max);
//fim criar código aleátorio

The mt_rand() function generates an integer using the "Mersenne Twister" algorithm. Also, it is 4 times faster than the% w_that you were trying to use.

Source: W3 Schools website.

    
23.11.2016 / 17:59