Standard PHP Library: Complete Overview
PHP is one of the most popular languages for web development, and its strength is largely due to its rich standard library (Standard PHP Library, SPL). Unlike many languages where the standard library is just a set of basic tools, PHP includes hundreds of ready-made functions and classes covering almost all backend developer needs. In this article, we will provide a complete overview of the key components of the standard PHP library, with code examples and practical recommendations.
1. Working with Arrays (Array Functions)
Arrays are the foundation of PHP. The standard library provides dozens of functions for processing them:
array_map()— applies a callback to all array elementsarray_filter()— filters elements by conditionarray_reduce()— reduces an array to a single valuearray_merge()— merges multiple arraysarray_diff()andarray_intersect()— array comparison
<?php$numbers = [1, 2, 3, 4, 5];$squared = array_map(fn($n) => $n * $n, $numbers);$even = array_filter($numbers, fn($n) => $n % 2 === 0);$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);print_r($squared); // [1, 4, 9, 16, 25]print_r($even); // [2, 4]echo $sum; // 15?>2. String Functions
PHP offers a powerful set for working with strings:
strlen(),strpos(),substr()— basic operationsstr_replace(),preg_replace()— substring replacementexplode()andimplode()— splitting/joiningtrim(),htmlspecialchars()— data sanitization
<?php$text = " Hello, PHP World! ";$clean = trim($text);$parts = explode(", ", $clean);echo $parts[0]; // Helloecho str_replace("World", "Universe", $clean); // Hello, PHP Universe!?>3. Working with Dates and Time (DateTime)
The DateTime class and its derivatives have completely replaced the outdated date() functions:
DateTime::createFromFormat()— string parsingDateInterval— working with intervalsDatePeriod— iterating over a date range
<?php$now = new DateTime('now');$birthday = new DateTime('2025-05-15');$interval = $now->diff($birthday);echo $interval->days . " days until birthday";?>4. Filesystem Functions
The standard PHP library provides a complete set for working with files and directories:
file_get_contents(),file_put_contents()— fast reading/writingfopen(),fwrite(),fclose()— low-level operationsscandir(),mkdir(),unlink()— directory managementis_file(),is_dir(),file_exists()— checks
<?php$content = file_get_contents('data.txt');file_put_contents('backup.txt', $content);$files = array_diff(scandir('./uploads'), ['.', '..']);?>5. Working with Databases (PDO - PHP Data Objects)
PDO is a universal interface for working with various DBMS (MySQL, PostgreSQL, SQLite):
- Prepared statements — protection against SQL injection
- Transactions with commit/rollback support
- Unified API for different databases
<?php$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');$stmt = $pdo->