Simple A/B randomization script with PHP

江枫思渺然 提交于 2020-01-07 02:53:05

问题


I have two ad options in my theme, Adsense and "Custom". If the user enters code into the "Custom" field, it trumps whatever they have in the "Adsense" code field, so the theme places their custom ad atop the content area. If they have adsense enabled, and nothing in the custom ad spot, the theme places their adsense code there.

I'd like to add an option to allow the user to opt to have my theme automatically rotate ads between adsense and their custom ads. The custom ads may be Ebay, Amazon, ClickBank, etc.

How would you set up a PHP script that will randomly choose between two options?

Alternately, I'd like to be able to give the user the option to choose a percentage value so that the adsense ads are rotated based on their chosen percentage.

Just looking for some suggested approaches. Thanks in advance.


回答1:


<?php

   $option[0] = 'html option 1';
   $option[1] = 'html option 2';

   echo $option[rand()%count($option)];
?>



回答2:


1) I think mt_rand(); it's better than rand(); (source:http://php.net/manual/en/function.mt-rand.php)

2) $option[0] = 'html option 1';
$option[1] = 'html option 1';
$option[2] = 'html option 1';
$option[3] = 'html option 2';

echo $option[rand()%count($option)];

It's not a better solution, but it works :)



来源:https://stackoverflow.com/questions/6215380/simple-a-b-randomization-script-with-php

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