String Functions:
Function | Description | Example |
---|---|---|
strlen() | Get the length of a string. | strlen("Hello World"); → 11 |
strtoupper() | Convert a string to uppercase. | strtoupper("hello"); → "HELLO" |
strtolower() | Convert a string to lowercase. | strtolower("HELLO"); → "hello" |
substr() | Extract a part of a string. | substr("Hello World", 0, 5); → "Hello" |
strpos() | Find the position of the first occurrence of a substring. | strpos("Hello World", "World"); → 6 |
str_replace() | Replace all occurrences of a substring. | str_replace("World", "PHP", "Hello World"); → "Hello PHP" |
trim() | Remove whitespace from the beginning and end of a string. | trim(" Hello World "); → "Hello World" |
explode() | Split a string into an array by a delimiter. | explode(" ", "Hello World"); → ["Hello", "World"] |
implode() | Join array elements into a string. | implode(" ", ["Hello", "World"]); → "Hello World" |
sprintf() | Return a formatted string. | sprintf("Hello %s", "World"); → "Hello World" |
Array Functions:
Function | Description | Example |
---|---|---|
array_push() | Push one or more elements onto the end of an array. | array_push($arr, "new"); |
array_pop() | Pop the last element off an array. | array_pop($arr); |
array_shift() | Remove the first element of an array. | array_shift($arr); |
array_unshift() | Prepend one or more elements to the beginning of an array. | array_unshift($arr, "new"); |
array_merge() | Merge one or more arrays. | array_merge($arr1, $arr2); |
array_slice() | Extract a portion of an array. | array_slice($arr, 1, 2); → ["B", "C"] |
in_array() | Check if a value exists in an array. | in_array("apple", $arr); → true |
array_search() | Search for a value in an array and return its key. | array_search("apple", $arr); → 2 |
array_map() | Apply a callback to the elements of an array. | array_map("strtoupper", $arr); |
array_filter() | Filter elements of an array using a callback function. | array_filter($arr, "is_numeric"); |
Date and Time Functions:
Function | Description | Example |
---|---|---|
date() | Format a local time/date. | date("Y-m-d H:i:s"); → "2024-12-22 10:30:00" |
time() | Return current Unix timestamp. | time(); → 1640956931 |
strtotime() | Parse an English textual datetime description into a Unix timestamp. | strtotime("now"); → 1640956931 |
mktime() | Get Unix timestamp for a date. | mktime(0, 0, 0, 1, 1, 2025); → 1735680000 |
getdate() | Get the date/time information of a timestamp. | getdate(); → array(0 => "2024", 1 => "December", ...) |
date_diff() | Calculate the difference between two DateTime objects. | $date1 = new DateTime("2024-12-22"); $date2 = new DateTime("2025-01-01"); date_diff($date1, $date2); |
Mathematical Functions:
Function | Description | Example |
---|---|---|
abs() | Get the absolute value of a number. | abs(-5); → 5 |
round() | Round a floating-point number. | round(3.14159, 2); → 3.14 |
floor() | Round a number down to the nearest integer. | floor(3.9); → 3 |
ceil() | Round a number up to the nearest integer. | ceil(3.1); → 4 |
max() | Get the maximum value from a list of values. | max(3, 5, 2); → 5 |
min() | Get the minimum value from a list of values. | min(3, 5, 2); → 2 |
rand() | Generate a random integer. | rand(1, 10); → 7 |
File Handling Functions:
Function | Description | Example |
---|---|---|
fopen() | Open a file or URL. | $file = fopen("file.txt", "r"); |
fread() | Read from a file. | fread($file, 10); |
fwrite() | Write to a file. | fwrite($file, "Hello, world!"); |
file_get_contents() | Read an entire file into a string. | file_get_contents("file.txt"); |
file_put_contents() | Write data to a file. | file_put_contents("file.txt", "Hello, world!"); |
unlink() | Delete a file. | unlink("file.txt"); |
file_exists() | Check if a file or directory exists. | file_exists("file.txt"); → true |
Session and Cookie Functions:
Function | Description | Example |
---|---|---|
session_start() | Start a new session or resume an existing session. | session_start(); |
session_destroy() | Destroy all data registered to a session. | session_destroy(); |
setcookie() | Send a cookie to the browser. | setcookie("user", "John", time() + 3600); |
$_SESSION | Superglobal array to store session variables. | $_SESSION['name'] = "John"; |
$_COOKIE | Superglobal array to access cookie values. | $_COOKIE['user']; |
Utility Functions:
Function | Description | Example |
---|---|---|
isset() | Determine if a variable is set and not null. | isset($var); → true |
empty() | Check if a variable is empty. | empty($var); → true |
die() | Output a message and terminate the current script. | die("Error!"); |
exit() | Output a message and terminate the script (alias of die() ). | exit("Error!"); |
var_dump() | Dump information about a variable. | var_dump($arr); |
print_r() | Print human-readable information about a variable. | print_r($arr); |
gettype() | Get the type of a variable. | gettype(123); → "integer" |