3

I need some guidance with a exercise I was set as part of my PHP class. I've tried many different methods but none that satisfy the requirements. The task is to do the following:

Prerequisite :

  1. Without using any variation of a loop
  2. Without any variation of string repeat
  3. Without string join function
  4. Without the require statement
  5. Using only 4 lines of code

    • It's worth noting that each statement must be on it's own line and the <?php, ?> tags count as one line each.

Output: print the following text 200 times

“All work and no play makes jack a dull boy.”

I'm assuming it requires the use of a while loop but I've exhausted my knowledge of PHP and simply can't come up with a solution. I'm not necessarily looking for the answer just to be pointed in the right direction.

Thank you.

4
  • Are you allowed to use arrays? Are you not allowed to use str_repeat()? Commented Nov 5, 2015 at 3:40
  • Arrays can be used, but not str_repeat(). Commented Nov 6, 2015 at 1:21
  • 1
    So what does your teacher say about labels and goto? :) Commented Nov 7, 2015 at 22:29
  • You can save 1 line of code because you should not put the ?> in place unless you're writing other (non-PHP) code after the PHP code. So all you need is the <?php opener, thus you can use three lines.... Commented Nov 7, 2015 at 22:36

4 Answers 4

2

Not sure if I got it or not, but heres my try...

function printsth ($count) {
    echo $count++, "All work and no play makes jack a dull boy.<br>", $count <= 200 ? printsth($count++) : "";
}
printsth(1);

cleaner version

function printsth ($count) {
    echo "All work and no play makes jack a dull boy.<br>", $count++ < 200 ? printsth($count++) : "";
}
printsth(1);
Sign up to request clarification or add additional context in comments.

2 Comments

updated the function name as it collapses with php reserved function name
This was actually very close to the solution I came up with in the end, which was indeed correct, however instead of the ternary operator I used an if statement to check the count and to also call the print function. I should note it was valid to use an if statement and an echo on the same line and finally omitting the closing PHP tag brings it to a total of 5 lines :)
1

Here you go:

$i = 0;
while ($i++ < 200){
    echo("All work and no play makes jack a dull boy.");
}

2 Comments

I would add that the brackets are optional and the whole while()&echo statement can be set to one line
This is actually the solution that I thought was correct, but I had forgotten to mention that it must be done without using any variation of a loop so I'm assuming it requires a recursive function. Since the closing PHP tag can be omitted, it leaves 4 lines for the code and 1 line for the opening PHP tag which is 5 lines in total. It's a very tricky question.
1

Here's my solution, using recursion and anonymous function (just for fun).

$rec = function($iteration, $text) use (&$rec) {
    return $iteration == 200 ? '' : $text .= $rec(++$iteration,"All work and no play makes jack a dull boy.\n");
};
echo $rec(1,'');

1 Comment

I don't have much experience using anonymous functions, however I was informed by my professor that it is possible to shorten the code to 4 lines (including the <?php tag) using anonymous functions so perhaps I'll look into them at some point, thank you.
1

Ok, since you can use arrays this makes it relatively simple:

I recommend browsing just this page and seeing what functions are available to you : https://secure.php.net/manual/en/ref.array.php

If the idea is to use recursive functions then this solution defeats the purpose and maybe the teacher will want future restrictions on using any built-in php functions aside from echo/print.

First solution if array_fill() is acceptable:

<?php
echo implode( "<br/>\n", array_fill(0, 200, "All work and no play makes jack a dull boy.") );

If you can't use array_fill() and it seems <?php is considered one of the four line requirement then here's this spaghetto:

<?php
a: (isset($i)?++$i:$i=0);
echo "<!-- $i --> All work and no play makes jack a dull boy.<br/>\n";
if ( $i < 199 ) goto a;

... And if line 2 is disallowed then I say this:

<?php
a:
echo "<!-- ".(isset($i)?++$i:$i=0)." --> All work and no play makes jack a dull boy.<br/>\n";
if ( $i < 199 ) goto a;

4 Comments

this is actually a pretty cool way to get it done. but doesn't array_fill has the conflict with the rule of any variation of string repeat ?
@Andrew I wondered about this also. Not sure if the answer would be accepted by the teacher. I was looking at the other solutions for ideas to reduce them to 3 lines.
this is an interesting question...I also wonder what the answer would be given by the professor
The topic we we're covering was using functions and recursion so I think my professor was looking for a solution that involved those two. However the solutions you provided are very good nonetheless, thank you :)

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.