php

How to get random value from an array in PHP

In this tutorial, we are going to see how to get random value from an array in PHP. You can use the PHP shuffle() function to randomly shuffle the order of elements or values in an array. The shuffle() function returns FALSE on failure.
 

How to get random value from an array in PHP
<?php
	$nbrs = array(1, 2, 3, 4, 5, 6);
	 
	// Randomize the order of elements in the array 
	shuffle($nbrs);
	foreach ($nbrs as $value){
		echo "$value" . "<br>";
	}
?>

Output:

6
2
5
3
4
1
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *