PHP Standard Library: A Complete Overview of Features

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

PHP Standard Library: Complete Overview



PHP is one of the most popular languages for web development, and its standard library provides developers with a powerful set of ready-made solutions. In this overview, we will examine in detail the main components of the PHP standard library, which allow solving tasks from simple string processing to complex work with databases and network protocols.



1. Working with Arrays (array_*)

Functions for working with arrays are among the most in-demand in PHP. The standard library contains over 80 functions for array manipulation.



  • array_map() — applies a callback to all elements of an array
  • array_filter() — filters array elements using a callback
  • array_reduce() — iteratively reduces an array to a single value
  • array_merge() — merges one or more arrays
  • array_search() — searches for a value in an array and returns the key


<?php$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Filtering even numbers$even = array_filter($numbers, function($n) { return $n % 2 === 0;});

// Doubling each element$doubled = array_map(function($n) { return $n * 2;}, $numbers);

// Sum of all elements$sum = array_reduce($numbers, function($carry, $item) { return $carry + $item;}, 0);

echo "Even: " . implode(', ', $even) . "<br>";echo "Doubled: " . implode(', ', $doubled) . "<br>";echo "Sum: $sum";?>


2. String Functions

PHP offers an extensive set of functions for string processing, including work with multibyte encodings via the mbstring extension.



  • strlen() / mb_strlen() — string length
  • strpos() / mb_strpos() — substring search
  • substr() / mb_substr() — substring extraction
  • str_replace() — substring replacement
  • explode() / implode() — string splitting/joining
  • trim() — removing whitespace from beginning and end


<?php$text = "  Hello, world! PHP is great.  ";

// Removing extra whitespace$clean = trim($text);

// Substring replacement$replaced = str_replace("great", "awesome", $clean);

// Splitting string into words$words = explode(" ", $replaced);

// Getting first 10 characters$first10 = mb_substr($clean, 0, 10, 'UTF-8');

echo "Cleaned text: '$clean'<br>";echo "With replacement: '$replaced'<br>";echo "First 10 characters: '$first10'<br>";echo "Number of words: " . count($words);?>


3. Working with Dates and Time (DateTime)

The DateTime class and its derivatives provide an object-oriented interface for working with dates.



<?php// Creating a date object$date = new DateTime('2024-12-25 10:30:00');

// Formattingecho $date->format('Y-m-d H:i:s') . "<br>";

// Adding an interval$date->add(new DateInterval('P10D')); // +10 daysecho "In 10 days: " . $date->format('Y-m-d') . "<br>";

// Difference between dates$now = new DateTime();$diff = $now->diff($date);echo "Days remaining until the date: {$diff->days} days";

// Working with time zones$date->setTimezone(new DateTimeZone('America/New_York'));echo "<br>In New York: " . $date->format('Y-m-d H:i:s');?>


4. File Input-Output

The standard library includes many functions for working with the file system.



  • file_get_contents() — reads the entire file into a string
  • file_put_contents() — writes a string to a file
  • fopen(), fread(), fwrite()

Recommendations