Built-in Functions

Explore powerful built-in functions for strings, arrays, dates, and more.

Built-in Functions

PHP comes with thousands of built-in functions. Master the most useful ones to write efficient code.

String Functions

PHP Strings
<?php
$text = "Hello World";

// Length and count
strlen($text);           // 11
str_word_count($text);    // 2

// Case conversion
strtolower($text);       // "hello world"
strtoupper($text);       // "HELLO WORLD"
ucwords($text);          // "Hello World"
ucfirst($text);          // "Hello world"

// Finding
strpos($text, "World");   // 6 (position)
strrpos($text, "o");     // 7 (last position)

// Replace
str_replace("World", "PHP", $text);  // "Hello PHP"
str_contains($text, "Hello");        // true (PHP 8+)

// Split and join
explode(" ", $text);     // ["Hello", "World"]
implode("-", ["a", "b"]); // "a-b"
join(", ", $arr);

// Extract
substr($text, 0, 5);     // "Hello"
substr($text, 6);        // "World"

// Trim whitespace
$trimmed = "  hello  ";
trim($trimmed);           // "hello"

// Format
sprintf("Hello %s", "World");
printf("Value: %.2f", 3.14159);  // Value: 3.14
?>

Array Functions

PHP Arrays
<?php
// Creating arrays
$numbers = range(1, 10);         // [1,2,3,4,5,6,7,8,9,10]
$filled = array_fill(0, 5, "X");  // ["X","X","X","X","X"]
$combined = array_merge($a, $b);

// Searching
in_array("red", $colors);              // true/false
array_search("red", $colors);          // index or false
array_key_exists("name", $arr);        // true/false

// Filtering and mapping
$numbers = [1, 2, 3, 4, 5];
$evens = array_filter($numbers, fn($n) => $n % 2 == 0);
$squared = array_map(fn($n) => $n ** 2, $numbers);
$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n);

// Sorting
sort($array);      // Sort values ascending
rsort($array);     // Sort values descending
ksort($array);     // Sort by keys ascending
asort($array);     // Sort by values, keep keys

// Transform
array_chunk($arr, 2);    // Split into chunks
array_slice($arr, 2, 3); // Get portion
array_reverse($arr);    // Reverse
array_unique($arr);    // Remove duplicates
array_flip($arr);      // Swap keys/values
?>

Date & Time Functions

PHP Dates
<?php
// Current time
date("Y-m-d");         // 2026-04-27
date("H:i:s");         // 15:30:45
date("l, F j, Y");     // Monday, April 27, 2026
date("d/m/Y H:i");     // 27/04/2026 15:30

// Timestamp
time();                // Current timestamp
mktime(0, 0, 0, 4, 27, 2026);  // Specific date

// Relative dates
strtotime("+1 week");          // Next week
strtotime("-1 month");         // Last month
strtotime("next Monday");     // Next Monday

// Date comparison
$date1 = strtotime("2026-01-01");
$date2 = strtotime("2026-12-31");
$diff = ($date2 - $date1) / (60*60*24);  // Days between

// DateTime object (more powerful)
$date = new DateTime("2026-04-27");
$date->modify("+1 week");
echo $date->format("Y-m-d");
?>

Useful Utility Functions

PHP Utilities
<?php
// Type checking
is_string($var); is_int($var);
is_array($var); is_bool($var);
is_null($var); is_numeric($var);

// Type casting
(int) "42";        // 42
(float) "3.14";    // 3.14
(string) 42;       // "42"

// Empty checking
empty($var);       // true if null, "", 0, [], "0"
isset($var);       // true if variable exists

// Output
var_dump($var);    // Detailed debug output
print_r($var);     // Human-readable

// JSON
json_encode($arr);      // Array to JSON string
json_decode($json, true);  // JSON to array
?>

Hands-on Exercises

Exercise 1: Use strpos() to find "PHP" in "I love PHP programming"
Exercise 2: Create an array and use array_filter() to get values > 10
Exercise 3: Display the current date in format: "Today is Monday, 27th April 2026"
Exercise 4: Use str_replace() to replace all vowels with "*"
Exercise 5: Create a JSON from an array and decode it back