As PHP developers, we often find ourselves testing small snippets of code, inspecting variables, or debugging issues directly from the terminal. In a Laravel project, we can use Tinker to do this.
But what if you want to interact with PHP code outside of Laravel? Enter PsySH — an interactive shell for PHP that works anywhere.
In this post, we’ll explore how to use PsySH in your PHP projects, how to leverage it for quick testing and debugging, and why it’s a must-have tool for PHP developers.
What is PsySH?
PsySH is an interactive shell for PHP, similar to the REPL (Read-Eval-Print Loop) environments you might have used with other languages (like Python’s python shell or Ruby’s irb). It allows you to execute PHP code interactively, making it easier to quickly try out functions, test snippets of code, or explore your project’s behavior without needing to write and execute full scripts.
Originally, PsySH is the underlying engine behind Laravel’s Tinker command, but you can also use it outside of Laravel to test and experiment with any PHP code.
Installing PsySH
If you’re working on a standalone PHP project, you can install PsySH using Composer, the PHP package manager.
Step 1: Install PsySH via Composer
composer require psy/psysh
This command will install PsySH in your project’s vendor directory.
Step 2: Launch PsySH
Once PsySH is installed, you can launch the interactive shell with:
vendor/bin/psysh
This command will start a REPL session where you can execute PHP code directly.
Using PsySH
Now that you’re inside the shell, you can start typing PHP commands. Here are a few examples:
Basic PHP Operations:
>>> 2 + 3
=> 5
>>> $name = "John Doe";
>>> strtoupper($name)
=> "JOHN DOE"
You can use all the basic features of PHP here, just like you would in a script.
Working with Variables and Functions:
>>> $x = 15;
>>> function square($n) { return $n * $n; }
>>> square($x);
=> 225
Advanced Features in PsySH
PsySH isn’t just a basic shell — it offers powerful features that make it extremely useful for debugging and testing.
Listing Variables and Functions
To see all the available variables, functions, and objects in your current session, use the ls command:
>>> ls
This will list all the variables currently available in the PsySH session.
Getting Help
If you’re unsure about a function or class, you can use the doc command to get detailed documentation. For example, to see documentation for strlen:
>>> doc strlen
Inspecting Classes and Methods
PsySH allows you to inspect PHP classes, including their methods and properties. For example, if you want to inspect the ArrayObject class:
>>> show ArrayObject
This will display the source code of the class if it’s available, allowing you to easily dive into its implementation.
PsySH with Laravel (Tinker)
If you’re using Laravel, PsySH is integrated as part of the Tinker command. You can launch the interactive shell within Laravel with:
php artisan tinker
Inside Tinker, you have access to your Laravel models, database, and application environment, which makes it incredibly powerful for testing Eloquent queries and interacting with your app’s data.
For example:
>>> use App\Models\User;
>>> User::find(1);
This allows you to interact with your Laravel application without writing any additional code. And do not require a browser to check the result. Instant code & output in terminal!
Using PsySH for Debugging
PsySH is perfect for debugging issues in your PHP code. Instead of writing temporary echo statements or var_dump() calls, you can quickly test how a function behaves or inspect the contents of a variable directly in the shell.
For example:
>>> $data = file_get_contents('file.txt');
>>> var_dump($data);
You can run this and directly see the output, which helps speed up the debugging process.
Conclusion
PsySH is a game-changer for PHP developers. Whether you’re building a small script or working with a complex Laravel application, having an interactive PHP shell can dramatically increase your productivity.
- Test snippets of code quickly.
- Inspect variables, classes, and functions with ease.
- Improve debugging without needing to modify your codebase.
By using PsySH in your workflow, you can avoid the hassle of constantly writing temporary debug statements and streamline the process of exploring and testing your PHP code.
