Introduction to Algorithms and Data Structures in PHP
PHP is one of the most popular languages for web development, but many developers underestimate the importance of understanding algorithms and data structures. Knowledge of these fundamental concepts allows you to write more efficient, faster, and scalable code. In this article, we will cover key algorithms and data structures in PHP with practical examples that will help you optimize your projects.
Whether you work with CMS, frameworks, or create APIs, algorithms and data structures in PHP are tools that directly affect application performance. Let's start with the basics.
Basic Data Structures in PHP
Arrays: A Universal Tool
In PHP, arrays are the most flexible data structure. They can work as lists, stacks, queues, and even hash tables. Let's look at standard operations:
<?php// Creating an array$numbers = [5, 3, 8, 1, 9];
// Adding an element to the end$numbers[] = 7;
// Removing the last element (like a stack)$last = array_pop($numbers); // 7
// Removing the first element (like a queue)$first = array_shift($numbers); // 5
// Searching for an element$index = array_search(8, $numbers); // 1
// Sortingsort($numbers);print_r($numbers); // [1, 3, 8, 9]?>It's important to remember: PHP's built-in array functions are optimized at the C level, so for basic operations use them rather than writing your own implementations.
Stack in PHP
A stack operates on the LIFO (Last In, First Out) principle. Implementation in PHP using SplStack:
<?php$stack = new SplStack();$stack->push('first');$stack->push('second');$stack->push('third');
echo $stack->pop(); // 'third'echo $stack->pop(); // 'second'?>Stacks are useful for checking brackets in code, implementing an undo function, or depth-first search (DFS) in graphs.
Queue in PHP
A queue operates on the FIFO (First In, First Out) principle. Use SplQueue:
<?php$queue = new SplQueue();$queue->enqueue('task 1');$queue->enqueue('task 2');$queue->enqueue('task 3');
echo $queue->dequeue(); // 'task 1'echo $queue->dequeue(); // 'task 2'?>Queues are indispensable for processing background tasks, working with message queues (RabbitMQ, Redis), and implementing BFS (breadth-first search).
Sorting Algorithms in PHP
Quick Sort
One of the most efficient algorithms. Implementation in PHP:
<?phpfunction quickSort(array $arr): array { if (count($arr) <= 1) { return $arr; } $pivot = $arr[0]; $left = $right = []; for ($i = 1; $i < count($arr); $i++) { if ($arr[$i] < $pivot) { $left[] = $arr[$i]; } else { $right[] = $arr[$i]; } } return array_merge( quickSort($left), [$pivot], quickSort($right) );}
$data = [3, 7, 1, 9, 2, 5];print_r(quickSort($data)); // [1, 2, 3, 5, 7, 9]?>Complexity: O(n log n) on average, O(n²) in the worst case. For large arrays, use the built-in sort() — it's faster.
Bubble Sort
A simple but slow algorithm. Useful for educational purposes and small arrays:
<?phpfunction bubbleSort(array $arr): array { $n = count($arr); for ($i = 0; $i < $n - 1; $i++) { for ($j = 0; $j < $n - $i - 1; $j++) { if ($arr[$j] > $arr[$j + 1]) { [$arr[$j], $arr[$j + 1]] = [$arr[$j + 1], $arr[$j]]; } } } return $arr;}?>Complexity: O(n²). Do not use for production — t