PHP for Machine Learning: Reality or Myth?
Many developers accustomed to the Python ecosystem for machine learning (ML) tasks are skeptical about using PHP in this field. And indeed, historically, Python with its libraries (NumPy, Pandas, Scikit-learn, TensorFlow) has become the industry standard. However, PHP is not just a language for website layout. Modern PHP (versions 7.4+ and 8.x) has a number of features that make it suitable for certain ML tasks, especially in the context of web applications.
In this article, we will analyze what tools exist for machine learning in PHP, where their use is justified, and where it is better to turn to more specialized languages.
Main PHP Libraries for Machine Learning
The PHP ecosystem is not as rich as Python's, but it has several mature libraries that allow you to implement basic and even advanced ML algorithms.
1. PHP-ML (PHP Machine Learning)
This is the most popular and well-documented library. It provides a wide range of algorithms: classification, regression, clustering, dimensionality reduction, and data preparation.
Example: Classification using the k-nearest neighbors (k-NN) method
require_once __DIR__ . '/vendor/autoload.php';
use Phpml\\Classification\\KNearestNeighbors;use Phpml\\Dataset\\CsvDataset;
// Loading data from CSV$dataset = new CsvDataset('iris.csv', 4, true);
// Splitting into training and test sets$randomSplit = new \\Phpml\\CrossValidation\\RandomSplit($dataset, 0.3);
// Training the model$classifier = new KNearestNeighbors(3);$classifier->train($randomSplit->getTrainSamples(), $randomSplit->getTrainLabels());
// Prediction$predicted = $classifier->predict([5.1, 3.5, 1.4, 0.2]);echo "Predicted class: " . $predicted; // Will output, for example, 'Iris-setosa'
// Accuracy evaluation$testSamples = $randomSplit->getTestSamples();$testLabels = $randomSplit->getTestLabels();$accuracy = \\Phpml\\Metric\\Accuracy::score($testLabels, $classifier->predict($testSamples));echo "Accuracy: " . $accuracy;The library also supports neural networks (Multilayer Perceptron), decision trees, SVM, naive Bayes classifier, and ensemble methods (Random Forest, AdaBoost).
2. Rubix ML
A more modern and performant alternative to PHP-ML. Rubix ML leverages the capabilities of PHP 7.4+ (typed properties, performance) and offers advanced tools: pipelines, cross-validation, hyperparameter tuning via GridSearch.
Example: Regression using Gradient Boost
require_once __DIR__ . '/vendor/autoload.php';
use Rubix\\ML\\Datasets\\Labeled;use Rubix\\ML\\Regressors\\GradientBoost;use Rubix\\ML\\CrossValidation\\Reports\\RSquared;
// Creating synthetic data$samples = [[1], [2], [3], [4], [5]];$labels = [2, 4, 6, 8, 10];
$dataset = Labeled::fromIterator($samples, $labels);
// Training$estimator = new GradientBoost();$estimator->train($dataset);
// Prediction$prediction = $estimator->predict([[6]]);echo "Prediction for 6: " . $prediction[0]; // Should be close to 123. TensorFlow PHP (FFI)
There is an experimental possibility to work with TensorFlow via PHP FFI (Foreign Function Interface). This allows calling the native TensorFlow library directly from PHP. However, the project is in an early stage and requires compiling C extensions.
Real-World Use Cases for PHP in ML
Despite the limitations, PHP can be effective in the following cases:
- Embedding ML into an existing web application — if you already have a project on Laravel or Symfony, and you need to add a simple model (for example, predicting customer churn probability or m