views:

125

answers:

2

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 ?

+6  A: 

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;
?>
Karsten
+1, basics of probability and statistics
dfilkovi
can you please explain more on how to apply 50% to php random function .. thanks
NetCaster
I think what he wants is toggle between variables each time a new request comes
marvin
@NetCaster: sample code added.
Karsten
@Netcaster: The law of big numbers says that in the long run it WILL be 50/50.
ryeguy
A: 
  • 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)

marvin