20
\$\begingroup\$

Based on this manual, there is a -r option that allows you to run PHP code on the command line without specifying the code tags, <? ?>.

I recommended it on the polyquine question, where this answer code be considered a PHP quine if we used -r or -R. I'm pretty sure -R is used like php -R scriptname.php or something and then runs the script like it is in between code tags.

Coming from a Perl background, one-liners are often used to shorten the code. My answer here is an example. The way we have been counting it for Perl one-liners are:

  • Each characters between the single quotes counts as a character.
  • Each additional flag counts as a character.

I put ^ under each character we count for the following one-liner just as an example:

echo "test" | perl -nle'print'
                    ^^  ^^^^^

which would count as 7 characters.

For PHP, can we do the same thing? For example in this program from that manual:

$ php -r '$foo = get_defined_constants(); var_dump($foo);'

We would count the characters in between '.

I think you'd be able to shorten this answer with the trick.

There was a small discussion in the comments here about it.

  • Would the -r count as 0, 1, or 2 characters?
  • Is this allowed?
  • Should I recommend this when I see answers with <? ?>?

I am not the best at PHP so I'm looking for some guidance.

\$\endgroup\$
  • \$\begingroup\$ By the way, the ?> isn't necessary at the end of the file, and it's recommended not to use it because whitespace after it could start output (and prevent setting headers) before you want to. \$\endgroup\$ – Peter Taylor Oct 30 '14 at 8:10
13
\$\begingroup\$

perl -e seems to be accepted without character penalty, so php -r should be OK as well.

php -R is the equivalent to perl -ne, so php -R scriptname.php is a syntax error.

php -f file, php -F file and plain php file all execute file, but none of them removes the need for PHP tags.

In php -R code or php -F file, the flag should be counted as one byte, just as in perl -ne.

\$\endgroup\$
  • \$\begingroup\$ I think that if php -r does not have any character penalties, than php -R should not have any character penalties. In Perl, you can do perl -E instead of -e to get the features for say for free, which lets you save 2 bytes over using print. Thanks for the explanation of -R too! \$\endgroup\$ – hmatt1 Oct 30 '14 at 13:59
  • \$\begingroup\$ @chilemagic: Frankly, I'm not a fan of not counting the -E switch. For PHP, in my opinion, you can't get -r and -R for free. That basically allows yoo to have a single program performing two different tasks without any penalty in the score. php -r is the default for running code as a command-line argument, so any other switch should count. \$\endgroup\$ – Dennis Oct 30 '14 at 17:42
  • \$\begingroup\$ Perhaps I'm a bit of a traditionalist, but a particular code cannot be saved to saved to a file, and executed thusly, then in my opinion it is not a program, but rather a code snippet. \$\endgroup\$ – primo Nov 20 '15 at 9:39
  • \$\begingroup\$ @primo <? is rather a shebang than part of the code. Would you accept a shell script that does not start with #/bin/sh? @chilemagic: I agree partially: -B and -E add addional code, so at least the code should not be free. (@ all: Not sure about the flag itself. Can´t I just add that code to my program?) -r however only allows you to get rid of the shebang (like using csh <someshellscript instead of just using the default shell) and should not count. \$\endgroup\$ – Titus Jul 7 '16 at 12:28
  • \$\begingroup\$ @Titus Happy new year <?=date(Y)?>!. \$\endgroup\$ – primo Jul 7 '16 at 13:03
  • \$\begingroup\$ @primo This is php mixed with plain text; I would consider that a disqualifying loophole. \$\endgroup\$ – Titus Jul 7 '16 at 13:27
  • 2
    \$\begingroup\$ @Titus "This is php mixed with plain text" The default behavior is to dump everything to stdout. This is php. \$\endgroup\$ – primo Jul 7 '16 at 17:23
  • \$\begingroup\$ @primo: It may be (historical) php, but I wouldn´t call it coding. Haven´t seen a php script in years that does not start off with <?php. Have You? \$\endgroup\$ – Titus Jul 7 '16 at 18:18
  • \$\begingroup\$ @Titus That's completely irrelevant. The language does what it does, and PHP prints everything outside of PHP tags verbatim to STDOUT. \$\endgroup\$ – Dennis Jul 7 '16 at 18:19
  • \$\begingroup\$ ... unless you call it with -r, where any php tags are forbidden. \$\endgroup\$ – Titus Jul 7 '16 at 18:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .