Many, many years ago, as an open source PHP and WordPress developer, I had a system for testing arbitrary PHP as I honed my skills and learned new techniques. I had a file on my web server called test-php.php that did nothing but output the results of my code and would spend hours writing code, upload to server, switch to browser, refresh. This is how I learned the basics of regex, converted base64, tested operators, and refined my database access techniques.
On a bored and underutilized day in 2012, I decided to figure out how to write an app to simplify the process. I had similarly written two apps previously, the first of which was a rip on a crazy flub by the Mitt Romney campaign when they released a campaign app called, “I’m with Mitt!” which would overlay Mitt Romney “Oh ’12“ campaign slogans on live photos. Hilariously, their developers failed to QC it thoroughly, ending up with a silly typo on the main overlay which draped the photos, meant to be shared socially using the app’s built-in functionality, with the slogans, “A Better Amercia” and “I’m with Mitt”.
That was not typo in my post. I know how to spell America. So, being smart like I am, I built an app that did the exact same thing. Except I misspelled “Mitt” as “Milt”. I called it “I’m with Milt” and overlookably misspelled something in every overlay.
My other app converted dates to other calendar systems (it was mainly a countdown to the end of the Mayan calendar). This app I called, “ΩiStars” (Oh my stars?) I worked on it for months, but it was so dependent on icons and graphics that when I went to replace the placeholders I had relied upon during development, by December 12, 2012, I still had not worked everything out. I questioned the demand for such an app after the Mayan calendar ended.
It took the better part of the day, but I eventually found all the right resources, sample code, and Xcode tutorials to land on a solution. I posted about it on HariKari.com because I had spent so much time figuring this out when, in the end, it was so simple.
Shortly thereafter I saw apps popping up in the Mac App Store with names like “CodeRunner” and “PHP Runner” and “Test PHP”. My code was so basic and these apps were so much better that I quit using my own.
Recently, though, I’ve needed something more specific to my own dev environment. So I pulled up that old Xcode project and, surprise!, it won’t build. I can’t even figure out how to fix it, either. So I went on a Google safari once again to build the whole thing from scratch.
That’s when I found the always impressive and curious-developer-oriented, RayWenderlich.com tutorial for using NSTask in modern Swift. It’s so much simpler than the original app I cobbled together for personal use and I was able to create a new app with far fewer lines of code and a much more sensible and human readable syntax!
So, first visit RayWenderlich.com and follow the excellent tutorial. It lays out basic details and gives a solid description of how and why this works. Their tutorials are written with people like me in mind – someone who codes for a living, but whose day job does not satisfy the most exciting boundaries of his curiosity:
The results (copied from the README):
What is it?
Sample code for running PHP scripts inside a macOS app. Has a very simple and basic UI consisting of a split scroll view and a button.
Enter PHP code in the top view and see results in the bottom view.
Uses macOS default PHP
For simple protability, uses default PHP binary included with macOS. However, it is possible to embed a PHP binary, custom
php.ini file, and PHP extensions.
Compile php (or just add your existing PHP binary, usually located at
Then set the path to your custom PHP binary’s bundle resource location:
let path = Bundle.main.path(forResource: "php",ofType:nil)
From PHP documentation:
You can specify the php.ini file in command line by using the following syntax:
php -c [Path to php.ini file] [Path to .php file]
php -c /etc/php-alt/php.ini /var/www/public_html/example.php
example.php file will run with the configuration set in the
php.ini file located here:
Add your custom php.ini to your Xcode project.
-c to the
NSTask arguments list:
Then add the path to php.ini within your bundle to the arguments list (arguments are added to the command in order):
arguments.append( Bundle.main.path(forResource: "php",ofType:"ini" ))
You’ll have to figure out how to set the paths used for extenstions in php.ini separately, but send me a note if you have a solution I can include here. Would be interesting to get xdebug output.
Also, php errors go to the Xcode console log. Would be fun to display them in the app.
NSTask functionality cribbed with comments from: https://www.raywenderlich.com/125071/nstask-tutorial-os-x
Syntax highlighting from the super simple and bare bones Macaw (not PHP syntax, but C-like and very easy to understand – a few tweaks and it’s fine for PHP): https://github.com/kuyawa/Macaw
And this is a much simplified version of something I put together in Objective-C many years ago:http://www.harikari.com/technology/how-to-run-php-scripts-in-xcode-mac-os-x-application.html