php -r: run PHP code from the command line without a file
- tool
- PHP
- versions
- PHP 5.x – 8.x
- os
- linux, macos, windows
php -r 'echo PHP_VERSION, PHP_EOL;' runs PHP code given on the command line, without a file and without the <?php tag. It is the quickest way to test a function, check a setting or compute a value in a shell script. Wrap the code in single quotes in bash and zsh, so the shell does not expand the $ of PHP variables.
Examples
The example uses PHP 8.4 compiled to WebAssembly (php-wasm) through a small php wrapper that passes its arguments to the PHP CLI unchanged:
npm init -y >/dev/null
npm install --silent --no-audit --no-fund @php-wasm/node@3.1.56 @php-wasm/universal@3.1.56 >/dev/null 2>&1
cat > php.mjs <<'EOF'
import { PHP } from "@php-wasm/universal";
import { loadNodeRuntime } from "@php-wasm/node";
const php = new PHP(await loadNodeRuntime("8.4", { emscriptenOptions: { processId: 1 } }));
const r = await php.cli(["php", ...process.argv.slice(2)]);
process.stdout.write(await r.stdoutText);
process.stdout.write(await r.stderrText);
process.exit(await r.exitCode);
EOF
node php.mjs -r 'echo PHP_VERSION, PHP_EOL;'
node php.mjs -r 'var_dump(str_contains("errcodex", "code"));'
node php.mjs -r 'echo json_encode(["ok" => true, "n" => 3]), "\n";'
node php.mjs -r 'echo $argv[1] * 2, "\n";' 21
node php.mjs -r 'echo date("Y-m-d", 0), "\n";'Output
8.4.25
bool(true)
{"ok":true,"n":3}
42
1970-01-01Arguments after the code land in $argv (with $argv[0] set to Standard input code). date() with timestamp 0 shows the start of the Unix epoch in the default time zone, which is UTC unless date.timezone is set in php.ini.
Related options
| Command | What it does |
|---|---|
| php -r 'code' | Run code given as an argument |
| php -r 'code' -- -x arg | Pass arguments that start with a dash to the code |
| echo '<?php echo 1;' | php | Read a script from standard input (needs the <?php tag) |
| php -a | Interactive shell: type statements and see the result |
| php -i | grep memory_limit | Show configuration values (phpinfo as text) |
| php -m | List the compiled and loaded extensions |
| php --ini | Show which php.ini files are loaded |
| php -d display_errors=1 -r 'code' | Override an ini setting for this run |
Pitfalls
- Double quotes in bash.
php -r "echo $x;"lets the shell replace$x(usually with nothing) before PHP sees it, which produces a parse error. Use single quotes in Unix shells. - No opening tag.
php -r '<?php echo 1;'fails with a syntax error; the tag is only for files and standard input. - Semicolons. The last statement still needs a
;, otherwise PHP reportsunexpected end of file. - cmd.exe and PowerShell. In
cmd.exe, use double quotes outside and single quotes inside the code. In PowerShell, single quotes outside work, but a"inside the code may need escaping depending on the PowerShell version. - Undefined functions from extensions that are not loaded fail with Call to undefined function; check
php -m.
Confirm it worked
The output of the code appears in the terminal, and echo $? after the command is 0 unless the code failed or called exit(N).
FAQ
Do I need <?php in php -r code?
No, and you must not add it. The code is taken as pure PHP; a leading <?php tag is a syntax error with -r.
How do I pass arguments to php -r?
Put them after the code; they are available in $argv, starting at $argv[1]. Use -- before arguments that start with a dash, so php does not read them as its own options.
Why does php -r work in bash but not in cmd.exe?
cmd.exe does not treat single quotes as quotes. Wrap the code in double quotes there and use single quotes inside the PHP code, for example php -r "echo 'hi';".