UltimatePOPCORNS UltimatePOPCORNS - 6 months ago 21
PHP Question

Cloning only the value of a variable

Im trying to make some kind of ambience randomizer, which picks a random ambience at every page load, but I also want it to avoid picking the same one from the previous page.

$amblst = array(1,2,3,4,5,6,7,8,9,10);

if (($key = array_search($_SESSION['ambprev'], $amblst)) !== false) {

$ambnum = $ambnum + 1;
$ambnum = array_rand($amblst);

$_SESSION['ambprev'] = clone $ambnum;

However using clone breaks the page for some reason and I don't have access to the error logs, so I can't provide that right now.

I just want to clone the variable's value, nothing more.

Answer Source

clone keyword is used to create an object copy, not variable.

An object copy is created by using the clone keyword (which calls the object's __clone() method if possible)


In your case it's enough to assign a copy of the $ambnum variable value:

$_SESSION['ambprev'] = $ambnum;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download