3

I have created a function which randomly generates a phrase from a hardcoded list of words. I have a function get_words() which has a string of hardcoded words, which it turns into an array then shuffles and returns.

get_words() is called by generate_random_phrase(), which iterates through get_words() n times, and on every iteration concatenates the n word into the final phrase which is destined to be returned to the user.

My problem is, for some reason PHP keeps giving me inconsistent results. It does give me words which are randomized, but it gives inconsistent number of words. I specify 4 words as the default and it gives me phrases ranging from 1-4 words instead of 4. This program is so simple it is almost unbelievable I can't pinpoint the exact issue. It seems like the broken link in the chain is the $words array which is being indexed, it seems like for some reason sometimes the indexing fails. I am unfamiliar with PHP, can someone explain this to me?

<?php

function generate_random_phrase() {
  $words = get_words();
  $number_of_words = get_word_count();
  $phrase = "";
  $symbols = "!@#$%^&*()";
  echo print_r($phrase);

  for ($i = 0;$i < $number_of_words;$i++) {
  $phrase .= " ".$words[$i];
}


  if (isset($_POST['include_numbers']))
    $phrase = $phrase.rand(0, 9);

  if (isset($_POST['include_symbols']))
    $phrase = $phrase.$symbols[rand(0, 9)];

  return $phrase;
}

function get_word_count() {
  if ($_POST['word_count'] < 1 || $_POST['word_count'] > 9) 
    $word_count = 4; #default
  else
    $word_count = $_POST['word_count'];
  return $word_count;
}

function get_words() {
  $BASE_WORDS = "my sentence really hope you 
    like narwhales bacon at midnight but only 
    ferver where can paper laptops spoon door knobs 
    head phones watches barbeque not say";
  $words = explode(' ', $BASE_WORDS);
  shuffle($words);
  return $words;
}
?>

3 Answers 3

2

In $BASE_WORDS your tabs and new lines are occupying a space in the exploded array that's why. Remove the newlines and tabs and it'll generate the correct answer. Ie:

$BASE_WORDS = "my sentence really hope you like narwhales bacon at midnight but only ferver where can paper laptops spoon door knobs head phones watches barbeque not say";
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, your a genius! Thanks
2

Your function seems a bit inconsistent since you also include spaces inside the array, thats why when you included them, you include them in your loop, which seems to be 5 words (4 real words with one space index) is not really correct. You could just filter spaces also first, including whitespaces.

Here is the visual representation of what I mean:

Array
(
    [0] =>             // hello im a whitespace, i should not be in here since im not really a word

    [1] => but
    [2] => 
    [3] => bacon
    [4] => spoon
    [5] => head
    [6] => barbeque
    [7] => 
    [8] => 
    [9] => sentence
    [10] => door
    [11] => you
    [12] => 
    [13] => watches
    [14] => really
    [15] => midnight
    [16] => 

So when you loop it, you include spaces, in this case. If you got a number of words of 5, you really dont get those 5 words, index 0 - 4 it will look like you only got 3 (1 => but, 3 => bacon, 4 => spoon).

Here is a modified version:

function generate_random_phrase() {
    $words = get_words();
    $number_of_words = get_word_count();
    $phrase = "";
    $symbols = "!@#$%^&*()";
    $words = array_filter(array_map('trim', $words)); // filter empty words
    $phrase = implode(' ', array_slice($words, 0, $number_of_words)); // no need for a loop
    // this simply gets the array from the first until the desired number of words (0,5 or 0,9 whatever)
    // and then implode, just glues all the words with space
    // so this ensure its always according to how many words you want

    if (isset($_POST['include_numbers']))
    $phrase = $phrase.rand(0, 9);

    if (isset($_POST['include_symbols']))
    $phrase = $phrase.$symbols[rand(0, 9)];

    return $phrase;
}

Comments

0

Inconsistent spacing in your words list is the issue.

Here is a fix:

function get_words() {
  $BASE_WORDS = "my|sentence|really|hope|you|
|like|narwhales|bacon|at|midnight|but|only|
|ferver|where|can|paper|laptops|spoon|door|knobs|
|head|phones|watches|barbeque|not|say";
  $words = explode('|', $BASE_WORDS);
  shuffle($words);
  return $words;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.