Quantcast
Channel: Rob Erskine - Graphic Designer
Viewing all articles
Browse latest Browse all 12

Selecting a Random String from a PHP Array

$
0
0

I thought I’d give a short little tutorial about selecting a random string from a php array. When I was rebuilding my portfolio, I wanted to give my search bar some spunk, where it would select a random string to display each time the page was loaded. Lost, I scoured the depths of the internet to find a solution. I wasn’t able to find a solid solution, so I started spaghetti-ing together some solutions.


$randomString = array ("This is string one!", "this is string two!", "this is string three!");
shuffle($randomString);
$randomResult = ($randomString[0], '');

What this does is adds a whole bunch of strings to an array. shuffle then randomizes their order within the array. We then call the random string by selecting the first object in the array after they’ve been shuffled.

Have any questions, or feel like you have a better way to obtain a random string? Let me know in the comments!


Viewing all articles
Browse latest Browse all 12

Trending Articles