Standard PHP Library: Complete Overview of Built-in Functions and Classes

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

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 elements
  • array_filter() — filters elements by condition
  • array_reduce() — reduces an array to a single value
  • array_merge() — merges multiple arrays
  • array_diff() and array_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 operations
  • str_replace(), preg_replace() — substring replacement
  • explode() and implode() — splitting/joining
  • trim(), 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 parsing
  • DateInterval — working with intervals
  • DatePeriod — 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/writing
  • fopen(), fwrite(), fclose() — low-level operations
  • scandir(), mkdir(), unlink() — directory management
  • is_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->                    

Recommendations