Machine Learning and Neural Networks using PHP

Machine learning is a type of artificial intelligence (AI) that provides computers with the ability to learn without being explicitly programmed. Machine learning focuses on the development of computer programs that can teach themselves to grow and change when exposed to new data.

Machine learning is a method by which knowledge is gained  through learning the patterns and the method of the data. The effect of Machine learning is that the algorithm or the process of data handling changes itself based on the type of data it encounters in  real-time.

A good example of a Machine learning could be a automatic scaling of the ec2 instance in AWS which can increase/decrease the resources based on the load without any human intervention.

An Artificial Neural Network is nothing but a Machine learning to have machines behave like human beings. Just see the images below.

The images are all the same of one digit `9′ but different in shapes.  Therefore, how do we identify that its shape is 9 (nine)? This is because of the training our system has got that 9 has curve in the top followed by a curve or straight line to the bottom.

An ideal use case of a neural network algorithm could be in finding out an image in a list of thousands of images that matches one particular image that we are trying to match.

Implementation of a Neural Nework in PHP

Some people assume that PHP is majorly a Web language not capable of doing sophisticated tasks.But with the growth of technology, it can handle complex tasks and has many out-of-the-box features.

PHP provides a library FANN (Fast Artificial Neural Network). PHP binding for FANN (Fast Artificial Neural Network) includes a framework for easy handling of training data sets. It is easy to use, versatile, well documented, and fast.The PHP website claims that it can support both fully connected networks and sparse networks.

The sparse network is a network in which nodes are connected to specific nodes only, i.e. the number of vertices are less and it is difficult to create. An example of sparse network is social friendship websites where the network is not so easily created

A full connected network has all the nodes connected to each other node.A network of a family is a full connected network where each member is connected to other member.

The installation of PHP-FANN module requires the PHP 5.2.0 and above and the libfann version 2.1.0 and above.

To install this you have to first install the fann devel.

# sudo yum install fann-devel

Then download the php-fann from the github / pecl website and install it, The installation instruction is similar to the gearman installation as described in blog

It is always good to re-run your phpinfo page to see that the module is installed well and there are no errors on it.

There is a well documented example on the website to test this module.

The example shows how to train a set of data for the xor functionality. After training the four sets from the fifth set,  the inputs will be automatically defining the ouputs based on the trained data.

Another simple example of the data training would be so have the database increment a counter when a specific keywords are introduced

The FANN can be combined with Gearman so that this training of data can keep happening on the background on realtime, yet there is no literature which authorizes this.

Also there is php-ml library for implementation of Artificial intelligence and Neural Networks using php but this requires php version 7.0 as a basic requirement.

Further information on FANN and other topics can be read at


  • Anubhav Jha is experienced in web development and specializes in languages such as PHP, MySQL, Linux and Java. He specializes in geo-spatial databases, CRM designing and search algorithms. His technology interests span machine learning, NLP, Analytics and Big Data.