0

I have a function with string output that outputs a sign in line 5. How to using a generator to add a function that will output line-by-line data from the first variable, where a new line is defined by sign; and the function should also have a limit - stop execution after output of 25 lines

<?php
function getRandomWord($len = 5) {
    $word = array_merge(range('a', 'z'), range('A', 'Z'));
    shuffle($word);
    return substr(implode($word), 0, $len);
}

$sign = ';';

for ($i = 0; $i < 100; $i++) {
    echo getRandomWord();
    if (($i + 1) % 5 == 0) {
        echo $sign;
    }
    echo "\n";
}
?>
3
  • 1
    Your description is quite wordy and not the easiest to follow. Some worked examples would really help. Also where exactly are you stuck? Have you tried anything? See also How to Ask. Thanks Commented Nov 24, 2023 at 10:28
  • @ADyson In short, I wanted to make a function that would output strings with only $sign using yield Commented Nov 24, 2023 at 10:49
  • 1
    If you have any clarifications, please edit your question to make a coherent post, without important bits hidden away down here. Have you tried anything so far? Commented Nov 24, 2023 at 11:05

0

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.