Executing PHP Code from the Command Line Without Temporary Files
You want to run a single line of PHP logic, perhaps checking if a function exists or validating an environment variable, without cluttering your directory with temporary .php files. This is a common requirement for quick checks, scripting automation, and CI/CD pipelines. Fortunately, the PHP CLI interpreter supports direct execution of code strings through the eval() construct when invoked via command-line arguments.
The standard approach involves passing the code directly to the php executable using the -r flag. This flag instructs the interpreter to read the subsequent string as the entire script to be executed and then terminate immediately after completion. For your specific example, checking for function existence, the command would look like this:
php -r "if(function_exists('my_func')) echo 'function exists';"
This syntax is efficient because it avoids file system overhead. The string passed to -r is treated exactly as if it were written in a standard .php file, respecting all PHP syntax rules, including variable interpolation and escape sequences. If you need to include external libraries before running your snippet, you can prepend the require_once or include statements directly within the -r argument string. For instance:
php -r "require_once 'vendor/autoload.php'; if(function_exists('my_func')) echo 'function exists';"
While powerful, this method has specific limitations regarding how data is passed into the script. Unlike running a file where you might pipe input from stdin or use positional arguments defined in the signature, the -r flag executes the code in an isolated environment. Any variables referenced inside the string must be declared within that scope before execution. If you attempt to access global variables defined outside the command line invocation without explicitly passing them, they will be undefined. To inject data, you typically rely on argv or by defining variables directly in the string itself.
Another consideration is error handling. When running a script via -r, exceptions thrown within the code can sometimes result in an immediate termination with a generic runtime error message, depending on your CLI error reporting configuration. If you are debugging logic that might throw exceptions, it is often safer to wrap the code block in a try-catch statement directly within the command string:
php -r "try { if(function_exists('my_func')) echo 'function exists'; } catch (\Exception \$e) { echo \$e->getMessage(); }"
Note the escaping of special characters like quotes and backslashes. The shell processes the arguments before PHP sees them, so mismatched quotes often lead to confusing syntax errors rather than PHP logic errors. Using single quotes inside a double-quoted string (or vice versa) is a reliable strategy to manage this complexity.
In professional workflows, particularly when using frameworks like Laravel, there is often a need to execute custom commands or console scripts directly from the shell. The php artisan command provides a robust layer on top of the interpreter, allowing you to run specific Artisan commands or even execute raw PHP code through its CLI capabilities. For complex tasks where -r feels too restrictive due to quoting issues, leveraging the framework's built-in CLI structure can provide better error reporting and logging.
Ultimately, the -r flag remains the most direct solution for executing single statements without files. It is fast, requires no temporary disk writes, and integrates seamlessly into shell scripts and build processes. Just remember to manage your quotes carefully and ensure all necessary dependencies are loaded before your logic runs.