0

Without using a for/while loop or any kind of string repeat function and a single PHP file, print out the text “All work and no play makes jack a dull boy” to the screen 200 times in a list – Grade C: 15 lines of code – Grade B: 10 lines of code – Grade A: 7 lines of code – Grade A*: 4 lines of code (VERY difficult)

– n.b. a “line” counts as a statement. Each closing brace must go on its own line, function calls on their own line and function declarations on their own line. PHP tags

5
  • 3
    This isn't the place where people gives an answer to your homework. Commented Dec 13, 2017 at 16:32
  • This was an optional exercise set by our teacher and I was having problems solving it. So I posted it on here. Sorry. Commented Dec 13, 2017 at 16:37
  • 1
    @dexterb homework questions are actualy okay on SO, although I agree, it should be made clear and the question itself should be revisited and fleshed out by OP. Commented Dec 13, 2017 at 16:48
  • @WilliamPerron yeah, it's okay to ask help for homework if and only if the OP has run out of ideas on solving the problem. When asking a question OP should provide the codes/pseudocodes/things the he at least have tried just to solve it. Commented Dec 13, 2017 at 17:17
  • @user9036433 no need to apologize :) a word of advice for the future: make sure to identify homework as such and clearly show what you have tried so far (ideally in the form of code) Commented Dec 13, 2017 at 17:50

1 Answer 1

1

You can use php's array_fill() method to fill an array 200 times with your data. http://php.net/manual/en/function.array-fill.php

After that you can use implode() to print that information out to the screen without using a loop. Show array without index key

Of course, both of these methods will utilize a loop internally, but your code will be free of these.

This should work:

$a = array_fill(0, 200, 'banana');
echo implode($a, '<br />');
Sign up to request clarification or add additional context in comments.

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.