Core Magic Variables
$_ - Last Return Value
The $_ variable holds the return value of the most recently executed statement:
$_e - Last Exception
The $_e variable contains the most recent exception or error:
$_e is only available after an exception has been thrown. Accessing it before will result in an error.$__out - Last Standard Output
The $__out variable captures the output from the most recent statement:
When is $__out updated?
When is $__out updated?
$__out is updated whenever code prints to standard output using echo, print, var_dump(), etc. It captures all output from a single statement execution.Context Variables
$this - Bound Object
When debugging within an object context, $this refers to the bound object:
$this is only available when PsySH is started with a bound object context. See the Debugging guide for more details.Command Scope Variables
These variables provide metadata about the current execution context, similar to PHP’s magic constants:$__class
The current class name:
$__namespace
The current namespace:
$__file
The current file path:
$__line
The current line number:
$__dir
The directory of the current file:
$__function
The current function name:
$__method
The current method name:
Practical Examples
Chaining Operations
Use$_ to build up complex operations:
Debugging Exceptions
Examine exceptions in detail:Capturing Output for Testing
Use$__out to verify output:
Variable Scope
Setting Variables
Variables you define persist throughout the session:Viewing All Variables
Use thels command to see all defined variables:
Protected Variables
Magic variables cannot be overwritten:Magic variables are read-only in the sense that they’re automatically updated by PsySH. Any explicit assignments are ignored in favor of the automatic values.
Working with History
Combine magic variables with history replay:Source Code Reference
Magic variables are managed in theContext class:
$_,$_e,$__out- Defined insrc/Context.php:22-28- Return values set via
setReturnValue()-src/Context.php:148 - Exceptions set via
setLastException()-src/Context.php:168 - Output captured via
setLastStdout()-src/Context.php:192