Jump to content

How To Use a Random If Statement


envec83

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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" />';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.