The PHP interactive shell evaluates statements and short expressions in a CLI session for local investigation.
What It Does
Start the interactive PHP interpreter. Try an expression at the interactive prompt.
Requirements and Compatibility
Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.
Syntax, Inputs and Results
The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.
Example 1: Basic Use
Start the interactive PHP interpreter.
php -v
php -a
Example 2: Another Case
Try an expression at the interactive prompt.
php > $word = strtoupper("draft");
php > echo $word;
DRAFT
Practical Example
Explore a small calculation and exit the shell.
php > $values = [2, 3, 5];
php > echo array_sum($values);
10
php > exit;
For production, apply the compatibility and edge-case limits described below.
Important Behavior and Edge Cases
Interactive mode requires a compatible PHP CLI build and is no replacement for tests or version-controlled scripts.
Comparison with Related Functions and Tools
| Subject | Key difference |
|---|---|
| CLI vs web request | CLI receives argv and runs in a terminal; HTTP requests use a web server and request metadata. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Expecting HTTP superglobals in terminal scripts | Read command arguments from $argv or getopt and use standard output and exit codes. |
| Skipping the prerequisite of this specific workflow | Install or configure the named tool and confirm the example can actually run. |
| Ignoring edge conditions | Interactive mode requires a compatible PHP CLI build and is no replacement for tests or version-controlled scripts. |
Key Takeaways
- The PHP interactive shell evaluates statements and short expressions in a CLI session for local investigation.
- Interactive mode requires a compatible PHP CLI build and is no replacement for tests or version-controlled scripts.
- Keep application configuration, source code and validation tests under version control.
Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.