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.
?>
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\$