Execute a string of PHP code on the command line
Developers coming from other dynamic languages often expect PHP to behave like Perl or Python when it comes to executing arbitrary strings from the command line. In those environments, you can seamlessly run perl -e 'print "hi";' or python -c 'print("hi")'. It is natural to assume that PHP offers a similar -r flag to execute code directly. However, this expectation runs into immediate friction when interacting with standard PHP installations.
If you attempt to run php -r 'echo "hi";', the CLI interpreter will immediately reject it with an error indicating that the option is not found. This behavior was consistent across PHP 4.4.9 and PHP 5.2.13, as evidenced by their respective help outputs. The -h flag for these versions lists options like -a for interactive mode, -f to parse a specific file, and -d to define INI entries, but the -r option is conspicuously absent.
The absence of this flag is not an oversight or a configuration error; it is a deliberate design decision rooted in security and architecture. Unlike Perl or Python, which were often designed with quick-and-dirty scripting tasks in mind from their inception, PHP was built primarily for server-side web development. The architecture assumed that code would always reside within files to be parsed by the server. Allowing arbitrary strings of code to be executed directly via the command line introduces significant security risks, particularly when dealing with user input or untrusted environments.
For those who require this functionality without writing a custom intermediary script, there is no native built-in switch in standard PHP distributions. However, you can still achieve the desired effect by leveraging the -r flag through a small wrapper script. This approach validates your need for dynamic execution while keeping the core logic separate from the environment setup.
Consider the following shell script, run_php.sh, which acts as a bridge:
#!/bin/bash
# Usage: ./run_php.sh 'echo "hi";'
/usr/bin/php -r "$1"
Here, the shell passes your string of code to PHP via standard input redirection logic inherent in how arguments are passed. Alternatively, if you prefer a pure PHP solution without an external script, you can use a wrapper file that accepts the code as an argument and evaluates it:
#!/usr/bin/php -q
<?php
if ($argc < 2) {
echo "Usage: run_php.php 'code here'\n";
exit(1);
}
$code = $argv[1];
// Avoid executing arbitrary code from untrusted sources
eval($code);
?>
While this method works, it is crucial to understand the trade-offs. Using eval() on command-line arguments means you are trusting the input provided to the script. If the goal is to run trusted snippets, a wrapper is acceptable. However, if you need to execute untrusted code or handle complex logic, relying on a string passed directly to eval can be dangerous and should be avoided in production environments.
The lack of a native -r flag forces developers to weigh the convenience of quick execution against the security implications of dynamic code evaluation. In many cases, creating a small helper script is the most pragmatic solution that balances ease of use with maintainability.