PHP development on an Android device
5/13/16
A new PHP project for a web application starts with some simple dependencies: a webserver, maybe a database and of course PHP. For that, we need a good workstation, Apache or Nginx and as a database like (usually) MySQL. Surely some choose MariaDB or PostgreSQL, others a non relational database like MongoDB. Most of us started with LAMP or WAMP for the first time and switched later to a development environment on a VM or Docker. I can go on and on throwing names of possible solutions and buzz words at you, but you will rarely hear Android and its apps as a tool for development.
A mobile for PHP development
Not only the code snippets, but also the project itself was completely written on my Android smartphone. With a lot of time at hand during my way to work by tram, I did some research if there is a way to run a PHP project on Android, so I could write simple code as proof of concept, checking out PHP's behaviour or test libraries/framework components while no Internet connection was on. It was important to me that everything had to work on a
non-rooted Android version and that the projects aren't handled by one big app.
Over the past year my smartphone became an important dev machine running different projects, mainly
PHPSnippets. It may look a bit nerdy to work on a mobile, but it is a great help for spontaneous ideas, proof of concepts or just for learning purposes. And you can work on it from everywhere.
PHP
Let's take a look at the most important part we need: PHP. For that I found the app
Server for PHP, that is available starting support from PHP version 5.4 to 5.6 and has even PHP 7. The name isn't quite right because it is PHP that is running its build-in webserver.
The app is kept simple and offers to change the path, port and network ip. Especially the last option can be quite handy if you want to test your project from remote systems.
It also comes with a huge list of extensions which can be easily disabled if you don't need them or in case you want to test your project on missing dependencies. Unfortunately, you can't add more PHP extensions to it, but the most important ones are included. It even offers
Yaf and
Phalcon if you like one of these frameworks. It can be configured with a custom
php.ini that you have to put into the webserver's root directory. Frameworks like
Symfony and it's little sibling
Silex can easily be used with this app in case you like to build a bigger web application on your mobile.
A drawback is the missing CLI, so PHP build tools or Composer package management can't be used the regular way. You'll have to build your vendor files on your regular workstation and copy them to your mobile, or you have to write your own installer that can be used with your browser.
There are also issues with PHP functions and classes handling with the temporary directory. While Android has no
/tmp path for temporary files, the function
sys_get_temp_dir()
still returns that path which result in strange behaviours and errors. Also
tmpfile()
will return "false" which is the return value in case an error happened. While the previous functions' errors happen on all invocations, the class
SplTempFileObject
will only write into the temporary directory, if it exceeds a given amount of bytes for a file. As a result, not every execution of the same code may go wrong.
Update: I got feedback from the author Tautvydas Andrikys that in the latest version system temp directory settings are in the default php.ini. The default files can be reinstalled by the menu item "Reinstall files". Also, the default index.php file contains scripts to use Composer with your browser. I haven't tested it yet, but will try it out next time when I have to update dependencies of a project.
Nevertheless, Server for PHP works great most of the time and you might enjoy to run code on your device with it.
Databases
SQLite
The simplest way for a database would be SQLite because it is already a part of Android. Many apps are using this small yet powerful storage for their needs, so why not using it for your project? There are many SQLite Clients in the Google Play app-store and one of my first choices was
aSQLiteManager. It offers a simple user interface and the possibility for executing SQL queries while being a small app not taking much space on your device. Probably a good choice when there isn't much memory left or on older Android mobiles. But if you consider MySQL on your production, this can lead to differences between productive systems and development.
MariaDB
If you haven't heard of
MariaDB before, it was created by former MySQL developers and has so many in common with it's sibling, that most of the projects using MySQL can be easily switched to MariaDB without any changes. On some occasions a performance boost was observed on a few SQL queries with default settings. Having MariaDB on Android is pretty easy with the app
MariaDB Server.
The app has a lot of similarities to
Server for PHP, mainly because it is written by the same author. Path, port and network ip can be changed to your needs. If a new version is available, MariaDB Server shows you the next version you can install. The only db-user to access the databases is root that comes already with an empty password, but it doesn't provide an interface to change the existing user's password or add new users in case your project needs more than one. This root user is allowed to access the data from a remote client if the network ip is set to be allowed for external clients, so you have to be careful how you configure MariaDB.
At first, it didn't want to work with
Server for PHP from the start and I had to set the mysql.sock path in the php.ini to make my web project run without errors. In case you get the same problems, I added the following lines into my php.ini:
pdo_mysql.default_socket="/data/data/com.esminis.server.mariadb/files/mysql.sock"
mysql.default_socket="/data/data/com.esminis.server.mariadb/files/mysql.sock"
mysqli.default_socket="/data/data/com.esminis.server.mariadb/files/mysql.sock"
The app offers only server functionality so you have to download a MySQL client of your choice or you can administrate MariaDB remotely. Of course there are also many admin tools written in PHP out there that can handle your databases.
Editor
We haven't any entire PHP IDE for Android (yet), but we all know the benefits of simple text editors: small, easy to work with and not overloaded. At first, there is a huge amount of text editors for Android, but not every app is useful for writing PHP code on your mobile. Next to code highlighting we know that a mobile isn't quite good when it comes to writing text compared to a regular keyboard. Code has more than just alphanumeric characters and with the default touchscreen keyboard typing operators or brackets can be tiresome. There are other keyboards in app stores like the
Hacker's Keyboard, but all those keys on a small smartphone is not really user-friendly, so my editor of choice had to include a help for usually inconvenient reachable characters.
I like it very much to work with
920 Text Editor, but this editor isn't listed anymore in Google play store. The development on that project was stopped a while ago, but the APK files of the latest version are
still available on Github. There's also a new repository where the development of
920 Text Editor will be continued.
Some of its features are common ones: tabs, undo, redo, text highlighting support for many coding languages, copy - paste, file search and content search. A real help opens when you tab on the Ω-icon in the upper toolbar. It opens a semi-transparent layer that shows all the code-relevant characters in addition to the Android keyboard for faster code-writing. That layer can be moved inside the app in case it overlays important lines of code. Together with the configurable indentation you can write small code snippets much faster than with the default keyboard and get faster results.
Room for improvement
Of course, this will never be able to replace your preferred dev environment but if you ever have the urge to test some PHP code or library on the move, you can do it from everywhere you are. Without a build process and PHP on CLI, not every kind of project can run from your mobile, so it seems you have to go without Sass or other transpiler. For now, the initialization of the project needs to be done on a PC/Mac and later you can move all files to your Android device. If you already have experience with other apps or solutions, I would like to hear your suggestions and ideas on this topic. For everyone else who got interested in developing PHP on a mobile: Have fun and build awesome stuff.
About Claudio Zizza
I'm working as a software developer for more than 15 years and used
many coding languages and frameworks during that time, like
PHP (of course), ASP, .NET, Javascript, Symfony, Laminas
and more. I also contribute to the open source communities
and am a part of the PHP Usergroup in Karlsruhe, Germany.
Follow me on
Twitter (@SenseException)