Algorithms and Data Structures in PHP: A Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Algorithms and Data Structures in PHP: In Practice

\n\n

In the world of web development, PHP remains one of the most popular languages. However, many developers familiar with the syntax often overlook fundamental concepts — algorithms and data structures. Understanding these topics not only improves code quality but also makes you a more competitive specialist in interviews. In this article, we will cover key algorithms and data structures in PHP with real code examples that you can immediately apply in your projects.

\n\n

1. Basic Data Structures: Stack and Queue

\n\n

Stack and queue are linear data structures often used in real-world applications. A stack operates on the LIFO (Last In, First Out) principle, while a queue operates on FIFO (First In, First Out). In PHP, they can be implemented using built-in array functions.

\n\n

Stack Implementation

\n
<?php\nclass Stack {\n    private $stack = [];\n\n    public function push($item) {\n        array_push($this->stack, $item);\n    }\n\n    public function pop() {\n        if ($this->isEmpty()) {\n            return null;\n        }\n        return array_pop($this->stack);\n    }\n\n    public function peek() {\n        return end($this->stack);\n    }\n\n    public function isEmpty() {\n        return empty($this->stack);\n    }\n}\n\n// Usage example\n$stack = new Stack();\n$stack->push("First");\n$stack->push("Second");\n$stack->push("Third");\necho $stack->pop(); // Third\necho $stack->peek(); // Second\n?>
\n\n

Queue Implementation

\n
<?php\nclass Queue {\n    private $queue = [];\n\n    public function enqueue($item) {\n        array_push($this->queue, $item);\n    }\n\n    public function dequeue() {\n        if ($this->isEmpty()) {\n            return null;\n        }\n        return array_shift($this->queue);\n    }\n\n    public function front() {\n        return reset($this->queue);\n    }\n\n    public function isEmpty() {\n        return empty($this->queue);\n    }\n}\n\n// Usage example\n$queue = new Queue();\n$queue->enqueue("First");\n$queue->enqueue("Second");\n$queue->enqueue("Third");\necho $queue->dequeue(); // First\necho $queue->front();    // Second\n?>
\n\n

These data structures are indispensable for implementing buffers, action history (undo/redo), and processing tasks in order of arrival.

\n\n

2. Sorting Algorithms in PHP

\n\n

Sorting is one of the most common operations in programming. Let's look at two classic implementations: bubble sort (for learning) and quick sort (for real-world use).

\n\n

Bubble Sort

\n
<?php\nfunction bubbleSort(&$arr) {\n    $n = count($arr);\n    for ($i = 0; $i < $n - 1; $i++) {\n        for ($j = 0; $j < $n - $i - 1; $j++) {\n            if ($arr[$j] > $arr[$j + 1]) {\n                // Swap values\n                $temp = $arr[$j];\n                $arr[$j] = $arr[$j + 1];\n                $arr[$j + 1] = $temp;\n            }\n        }\n    }\n}\n\n$numbers = [64, 34, 25, 12, 22, 11, 90];\nbubbleSort($numbers);\nprint_r($numbers); // [11, 12, 22, 25, 34, 64, 90]\n?>
\n\n

Complexity: O(n²) — not suitable for large arrays, but excellent for demonstrating the principle.

\n\n

Quick Sort

\n
<?php\nfunction quickSort($arr) {\n    if (count($arr) < 2) {\n        return $arr;\n    }\n\n    $pivot = $arr[0];\n    $left = $right = [];\n\n    for ($i = 1; $i < count($arr); $i++) {\n        if ($arr[$i] < $pivot) {\n            $left[] = $arr[$i];\n        } else {\n            $right[] = $arr[$i];\n        }\n    }\n\n    return array_merge(quickSort($left), [$pivot], quickSort($right));\n}\n\n$numbers = [3, 6, 8, 10, 1, 2, 1];\n$sorted = quickSort($numbers);\nprint_r($sorted); // [1, 1, 2, 3, 6, 8, 10]\n?>                

Blogs

Book Recommendations