How to generate words instead of characters?

I’m using a script which generates words, but instead of generating words, it generates characters instead and mixes them up.

This is the code

<?php 
function FirstNames($numchar) 
{ 
    $word = "Jason, Abdul, Aria, Demi, Paul"; 
    $array=explode(",",$word); 
    shuffle($array); 
    $newstring = implode($array,""); 
    return substr($newstring, 0, $numchar); 
} 


?>

And this is the code which i use to echo the results, it’s like a name generator basically.

<?php

$num_letters = rand(5, 20);
echo FirstNames($num_letters);
?>

Someone gave me the script, hence i didn’t write it myself, but the use was originally intended to generate a random string such as “abs783” not actual words.


function RandFirstName () {
  $words = array( 'Jason', 'Abdul', 'Aria', 'Demi', 'Paul' ); // Fill this in.
  return $words[ array_rand( $words ) ];
}

echo RandFirstName();

Thanks for the reply, so do i replace the top half of the code with the one you posted? and keep the bottom half the same? (ie the part where i want the string to display)?

Edit: I see now, i’ve done it. Works just fine. Thanks for that. Say i wanted to generate two words, how would i go about that?
Say i wanted to generate “Abdul and Peter”

Don’t keep the bottom half, and can change most if not all of the code in the top half to what I provided.

I see now, i’ve done it. Works just fine. Thanks for that. Say i wanted to generate two words, how would i go about that?
Say i wanted to generate “Abdul and Peter”

P.S i’m working on a name generator, and i will be sure to credit you on my site for your help.

Well the easiest way would be to run the function twice…but there is that chance of repeat names. But if you have a large data set of names in, it would be very rare.