Interactive shell

suggest change

PHP includes an interactive shell (REPL) that allows developers to experiment with syntax, evaluate expressions, test functions, and inspect objects in real time from the command line without creating temporary script files.

Starting the Interactive Shell (php -a)

Launch the interactive shell by running the PHP binary with the -a option:

php -a

When GNU Readline or libedit support is compiled into PHP, you will see the interactive prompt:

Interactive shell

php > echo "Hello from PHP " . PHP_VERSION;
Hello from PHP 8.3.0
php > $numbers = [1, 2, 3, 4, 5];
php > echo array_sum($numbers);
15
php >

Multi-Line Statements

Statements can span multiple lines. The shell detects incomplete blocks and continues prompting until the statement or block is closed with a semicolon or closing brace:

php > function greet(string $name): string {
php {     return "Hello, {$name}!";
php { }
php > echo greet("World");
Hello, World!

To exit the interactive shell, type exit or quit (followed by Enter), or press Ctrl+D.

Executing Code Directly with php -r

If you want to execute a quick snippet without entering the interactive shell, use the -r (run) option:

php -r 'echo json_encode(["status" => "ok", "timestamp" => time()]);'
{"status":"ok","timestamp":1729173000}

Do not include <?php opening tags when using php -r.

Passing Input via Stdin

You can pipe PHP code directly into the PHP interpreter:

echo '<?php echo hash("sha256", "hello world");' | php

Advanced REPL: PsySH

While php -a is built into the PHP distribution, modern PHP development often uses PsySH—the interactive runtime developer console that powers tools like Laravel's artisan tinker.

Install PsySH globally via Composer:

composer global require psy/psysh

Start the console:

psysh

Key features of PsySH include:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents