Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Created May 21, 2012 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefrosty/2763961 to your computer and use it in GitHub Desktop.
Save thefrosty/2763961 to your computer and use it in GitHub Desktop.
Random array
$ads = array('<a href="#">Sponsored link 1</a>', '<a href="#">Sponsored link 2</a>', '<a href="#">Sponsored link 3</a>');
echo $ads[array_rand($ads, 1)];
OR
$numb = rand( 1, 3 );
if ( $numb == 1 )
echo '<a href="#">Sponsored link 1</a>';
@jkudish
Copy link

jkudish commented May 21, 2012

why not this?

<?php
$numb  = rand( 1, 3 );
echo "<a href="#">Sponsored link $numb</a>";

@thefrosty
Copy link
Author

Because each array is a different message..

@jkudish
Copy link

jkudish commented May 21, 2012

Ah, your initial example showed them as the same except for the number, in that case scenario 1 is probably my preferred approach, but I'd list each element in the array on each line since they're long strings, it's easier to read/modify later

@sdenike
Copy link

sdenike commented May 21, 2012

The first method will probably look cleaner than the other depending on how many different links you end up with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment