envec83 Posted June 19, 2011 Share Posted June 19, 2011 Hi, I code in C/C++, but I don't know PHP. I am basically trying to write some PHP code to perform split testing on my WordPress blog. That is, I want to have a PHP code that will output one ad half the times, and a second ad the other half. I think it would be possible to do this generating a random number or reading the time, and then outputting one ad if the number is odd, and the second ad if the number is even, right? So something like this in pseudo code: x = random number; if (x is odd) <img src="ad1" /> else <img src="ad2" /> Could anyone let me know how this code would be on PHP? Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/ Share on other sites More sharing options...
quelle Posted June 19, 2011 Share Posted June 19, 2011 I rly don't get what you mean, but you could try to put ur adds in an array and the shuffle the order and echo them randomly $adds = array[]; shuffle($adds); Like this Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/#findComment-1231899 Share on other sites More sharing options...
envec83 Posted June 19, 2011 Author Share Posted June 19, 2011 That could work. How would the final code look like though? Maybe something like this: <?php $adds = array["ad1"=>"<img src="ad1" />","ad2"=>"<img src="ad2" />"]; shuffle($adds); echo $adds["add1"]; ?> Or did I make any mistake there? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/#findComment-1231930 Share on other sites More sharing options...
envec83 Posted June 19, 2011 Author Share Posted June 19, 2011 Hmm, I guess calling the ad by "ad1" will eliminate the random factor. Maybe something like this instead: <?php $ads = array['<img src="ad1" />' , '<img src="ad2" />']; shuffle($ads); echo $ads[0]; ?> Will the line "echo $ads[0];" actually call the first element of the array after it was randomized? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/#findComment-1231934 Share on other sites More sharing options...
derwert Posted June 19, 2011 Share Posted June 19, 2011 envec83 I will provide two examples of what you requested to help you learn. This first one shows the ad based on a random number <?php $rand = rand(0, 1); if($rand){ // rand equals TRUE echo '<img src="ad1" />'; }else{ // rand equals FALSE echo '<img src="ad2" />'; } ?> This second one checks the current second and if it's even shows one ad and if it's odd shows another ad. <?php $seconds = date('s'); if($seconds & 1){ // Even Number echo '<img src="ad1" />'; }else{ // Odd Number echo '<img src="ad2" />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/#findComment-1231940 Share on other sites More sharing options...
envec83 Posted June 19, 2011 Author Share Posted June 19, 2011 Exactly what I needed derwert, thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/#findComment-1231942 Share on other sites More sharing options...
monkeytooth Posted June 19, 2011 Share Posted June 19, 2011 Not that this is the best concept. But to try to be "equal" if you will where is 50/50 but at random with not display count or anything to back it up stored anywhere. I would say.. maybe $adOne = 2000; $adTwo = 4000; $adSrv = rand(0, 4000); if($adSrv <= $adOne){/*show ad one*/} if(($adSrv <= $adTwo) AND ($adSrv > $adOne )){/*show ad two*/} Thats kinda quick and dirty.. for a concept would be kinda ideal for a few ad's but more elaborate concepts can be thought of I am sure. Quote Link to comment https://forums.phpfreaks.com/topic/239818-how-to-use-a-random-if-statement/#findComment-1231957 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.