Running PHP with -r instead of code tags

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.

• 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. – Peter Taylor Oct 30 '14 at 8:10

1 Answer

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.

• 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! – hmatt1 Oct 30 '14 at 13:59
• @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. – Dennis Oct 30 '14 at 17:42
• 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. – primo Nov 20 '15 at 9:39
• @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. – Titus Jul 7 '16 at 12:28
• @Titus Happy new year <?=date(Y)?>!. – primo Jul 7 '16 at 13:03
• @primo This is php mixed with plain text; I would consider that a disqualifying loophole. – Titus Jul 7 '16 at 13:27
• @Titus "This is php mixed with plain text" The default behavior is to dump everything to stdout. This is php. – primo Jul 7 '16 at 17:23
• @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? – Titus Jul 7 '16 at 18:18
• @Titus That's completely irrelevant. The language does what it does, and PHP prints everything outside of PHP tags verbatim to STDOUT. – Dennis Jul 7 '16 at 18:19
• ... unless you call it with -r, where any php tags are forbidden. – Titus Jul 7 '16 at 18:28