PHP Switching between two variables equally without using database

浪尽此生 提交于 2019-12-23 02:06:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!