Alim Qureshi Alim Qureshi - 2 months ago 6
PHP Question

How to display an array at random?

I was wondering how I display a array at random here's my code

<?php
$names=file('name.txt');
echo count($names).'<br>';
foreach($names as $name)
?>

<h1>Random Names Test, Name: <?php echo $name.'<br>'; ?>


I know the code isn't 100% right to do the array at random :/ sorry I'm not that well at php but I want it to display random like if they reload the page another string from the text file will appear

Here I'll show you what I mean

<h1>Random Names Test, Name: Alim</h1>


When they reload the page another name will appear

<h1>Random Names Test, Name: Tiara</h1>


And also I don't know if they can see the array names in inspect element, if they can how can I prevent them from seeing them?

Answer

Your text file should be

nn.txt

   Test2 
   Test3 
   Test4 
   Test5 
   Test6 
   Test7 
   Test8 

PHP:

    <?php

    $names=file('nn.txt');
    echo count($names).'<br>';

    ?>
    <h1>Random Names Test, Name: <?php echo $names[array_rand($names)]; ?></h1>
Comments