I have been developing for years. However, I still have a feeling that I lack something. There are fundamental things in PHP that I don’t know but I definitely should.
So I decided to get a text book and learn from the basic. The book I choose is this one https://www.amazon.com/PHP-Zend-Certification-Study-Guide-ebook/dp/B077G9Q6L5
Since it is a book for test preparation, it should cover the fundamentals as well as the advanced one (IMO).
When I started working through the chapters, I really want something like the console window in Chrome/Firefox when I learn JavaScript. That is when I save the code or hit enter, I can see the result completely without manually executing the code (or reload browser window).
I tried to search for a REPL (read eval print loop) for PHP on the market but couldn’t find one so I decided to make one myself.
It wasn’t hard at all.
The concept
I think the most important thing to make a REPL work is some kind of service to watch the file changes. When changes happen, it should be update to the screen.
The components
Having work with Selenium and Python for a while, I’m sure I can reload a browser instance from python code. The next step is to find a library in Python that watches the change in my PHP source code. Without much effort, I found watchdog, an very nice library to watch changes in file/directory.
So, here is the setup:
- I’m going to setup a server. This is easy since PHP has its own capability to create a server
- Next, I’m going to open the URL that load PHP files in browser. Selenium can handle this task very well.
- Watchdog will do the rest. When the PHP source code is changed, it will signify Python code to reload the browser so I can see the changes.
The code
PHP
Next, you will need to setup a folder to host your PHP code. You can put this folder anywhere. You don’t even need to install XAMPP or WAMP.
Create a PHP file. This is the place you write your code. In your favorite terminal, cd to that folder and run the following command:
php -S localhost:9000

Python
The Python code is very simple. You can find the source code here on my PHP REPL GitHub repo
In the main.py file, you need to change three fields to match your own settings:

driver_path is the path to the geckodriver file (I use Windows so that would be geckodriver.exe, if you use Linux distros or OSX, the name should be geckodriver only). You can download the latest gecko driver https://github.com/mozilla/geckodriver/releases
Of course, if you know Selenium, you are not limited to using gecko driver only.
my_url would be the URL that you created when starting a server from PHP, which I’m going to cover next.
There is another field on line 32:

Change this to the path of your source code.
Make sure you install selenium and watchdog. Tutorials are below:
After you made the changes, open your favorite terminal program and cd to the folder that contains main.py and run:
python main.py
The program now watches changes in your PHP files.
The result
If you setup correctly, you can now see the changes instantly as you change your PHP source code. Here is what it’s like on my computer:

Conclusion
With this tool, I can learn a lot easier (without manually executing my code). Hopefully you find it useful. Please let me know your opinions.
Thanks!
I also write WordPress and WooCommerce tutorials here on my blog. Check it out!