I’m working on a PHP function called cascade that accepts a string and an integer representing the gap between words. The function should return the text arranged in a cascading representation.
For example, if the function is called like this:
cascade("The codings bug", 2);
t should return:
T__c__b___
_h__o__u__
__e__d__g_
______i___
_______n__
________g_
_________s
Another example:
cascade("The coding bug", 3);
Should return:
T___c___b__
_h___o___u_
__e___d___g
_______i___
________n__
_________g_
Here is the current code I have written
function cascade(string $s, int $gap): string {
$lines = [];
$words = explode(' ', $s);
foreach ($words as $word) {
$line = "";
for ($i = 0; $i < strlen($word); $i++) {
$line .= str_repeat('_', $gap * $i) . $word[$i] . PHP_EOL;
}
$lines[] = trim($line); // Remove the trailing newline character
}
return implode(PHP_EOL, $lines);
}
When I ran cascade(‘The codings bug’, 2), I got the following result:
T __h ____e c __o ____d ______i ________n __________g ____________s b __u ____g