问题
i need to switch between two variables so i am able to serve two variables equally
for example i have
$ad1
$ad2
i want to serve both ads equally using a light method with no database
using random method won't serve both equally
can you please guide me how to achieve this ?
回答1:
But the random method (50/50) should serve both equally, given enough requests.
And it is the simplest solution imho.
<?php
$ad1 = '<img src... >';
$ad2 = '<img src...2 >';
echo mt_rand(0, 1) ? $ad1 : $ad2;
?>
回答2:
- You can use memcache (recommended)
- You can keep track of the last used variable in a file on the server (not recommended)
- You can use time() and use its mod by 2 as your decision maker (still not an exactly equality as you wanted)
(I think, on the long run, rand will serve you great)
来源:https://stackoverflow.com/questions/1993312/php-switching-between-two-variables-equally-without-using-database