Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- PHP Challenge
- by Jefrey S. Santos <jefrey[at]jefrey.ml>
- The purpose is to write a PHP code that produces the following output:
- * *
- * *
- * *
- * *
- *
- * *
- * *
- * *
- * *
- Simple, huh? But there are some rules...
- - You can't use more than one loop (for, while...)
- - You can't use more than one echo (or print, printf...)
- - You can't cause any PHP error of any kind
- - You can't use more than one LINE 3:)
- */
- /*
- An approach to draw that figure in standard PHP,
- without following the rules,
- could be like this.
- */
- for($i = 0; $i <= 9; $i++) {
- if($i<5) {
- if($i) echo str_repeat(" ", $i);
- echo "*";
- if(9-2-$i*2>=0) echo str_repeat(" ", 9-2-$i*2);
- if($i!=4) echo "*\n";
- else echo "\n";
- } else {
- if($i>5) {
- if(9-$i) echo str_repeat(" ", 9-$i);
- echo "*";
- if(9-2-((9-$i)*2)>=0) echo str_repeat(" ", 9-2-((9-$i)*2));
- if(9-$i!=5) echo "*\n";
- else echo "\n";
- }
- }
- }
- echo "\n\n\n\n\n\n\n"; // this is not part of the answer code
- /*
- However, we've put plenty of echoes,
- and we are limited just to 1 echo.
- We could tokenize our script using ternary operators
- instead of if and elses, and use only one main echo.
- */
- for($i = 0; $i <= 9; $i++) echo (
- $i < 5 ? (
- ($i ? str_repeat(" ", $i) : null).
- "*".
- (9-2-$i*2>=0 ? str_repeat(" ", 9-2-$i*2) : null).
- ($i!=4 ? "*\n"
- : "\n")
- ) : (
- ($i>5 ?
- (9-$i ? str_repeat(" ", 9-$i) : null).
- "*".
- (9-2-((9-$i)*2)>=0 ? str_repeat(" ", 9-2-((9-$i)*2)) : null).
- (9-$i!=5 ? "*\n"
- : "\n")
- : null)
- )
- );
- echo "\n\n\n\n\n\n\n"; // this is not part of the answer code
- /*
- I know the code above seems to be some different programming
- language rather than PHP (almost a brainf*ck), but it's valid PHP.
- As we just used one command inside the for, and it's just an echo,
- it's valid if we remove line breaks and tabs without looking like
- minifying or forcing the code to be in one line.
- In other words, it looks like this:
- if($foo) echo "hey";
- And not like this:
- if($foo) { echo "hey"; }
- */
- for($i = 0; $i <= 9; $i++) echo ($i < 5 ? (($i ? str_repeat(" ", $i) : null)."*".(9-2-$i*2>=0 ? str_repeat(" ", 9-2-$i*2) : null).($i!=4 ? "*\n": "\n")) : (($i>5 ? (9-$i ? str_repeat(" ", 9-$i) : null)."*".(9-2-((9-$i)*2)>=0 ? str_repeat(" ", 9-2-((9-$i)*2)) : null).(9-$i!=5 ? "*\n": "\n"): null)));
Add Comment
Please, Sign In to add comment