etrader Posted April 11, 2011 Share Posted April 11, 2011 I need a simple code like $test1="1"; $test2="2"; $test=$test1|$test2; echo $test; to randomly catch the value of strings $test1 or $test2; as each time running the code, echo shows randomly one of the strings. Could you please guide what the third line should be ? Link to comment https://forums.phpfreaks.com/topic/233320-a-simple-way-to-randomly-choose-between-from-two-strings/ Share on other sites More sharing options...
dcro2 Posted April 11, 2011 Share Posted April 11, 2011 Here's one way to do it, put the strings in an array: $strings = array("1", "2", "3", "4"); $test = $strings[rand(0, count($strings)-1)]; Link to comment https://forums.phpfreaks.com/topic/233320-a-simple-way-to-randomly-choose-between-from-two-strings/#findComment-1199891 Share on other sites More sharing options...
etrader Posted April 11, 2011 Author Share Posted April 11, 2011 oh thanks, I did not think of the powerfulness of array Thanks again! Link to comment https://forums.phpfreaks.com/topic/233320-a-simple-way-to-randomly-choose-between-from-two-strings/#findComment-1199892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.