Reference · 133 entries

PHP Reference

The PHP 8 reference: string, multibyte, array and sorting functions with syntax, parameters, the PHP version that added or changed each, and examples you can run in your browser.

PHP 8 Version history

Every page covers one built-in function for PHP 8.x, with the version that added it and what changed since, linked to the PHP version history. Examples run in your browser on PHP 8.3 compiled to WebAssembly; functions from PHP 8.4 and 8.5 show output from those versions.

String Functions

PHP string functions from strlen() and str_replace() to sprintf() and htmlspecialchars(), with syntax, PHP 8 changes and examples you can run.

NameDescription
strlen()Return the length of a string in bytes. Multibyte characters such as é count as several bytes; use mb_strlen() to count characters.
str_word_count()Count the words in a string, or return them as an array. Words are runs of letters plus ' and -; digits split words.
strtolower()Convert every ASCII letter in a string to lowercase. Since PHP 8.2 it ignores the locale; use mb_strtolower() for é, Ü and others.
strtoupper()Convert every ASCII letter in a string to uppercase. Letters such as é stay unchanged; use mb_strtoupper() for those.
ucfirst()Make the first character of a string uppercase and leave the rest as it is. Only ASCII letters change.
lcfirst()Make the first character of a string lowercase, leaving the rest unchanged. Handy for turning PascalCase into camelCase.
ucwords()Uppercase the first letter of each word in a string. By default words are separated by spaces, tabs and new lines.
trim()Remove whitespace (or other characters you choose) from both ends of a string. Spaces inside the string are kept.
ltrim()Remove whitespace or other chosen characters from the start (left side) of a string only.
rtrim()Remove whitespace or other chosen characters from the end (right side) of a string. chop() is an alias.
str_pad()Pad a string to a given length with spaces or another string, on the right, left or both sides.
str_repeat()Repeat a string a given number of times and return the result. A count of 0 returns an empty string.
strrev()Reverse a string byte by byte. Fine for ASCII, but it breaks multibyte characters such as é.
str_replace()Replace every occurrence of a search string with a replacement. Accepts arrays to make several replacements at once.
str_ireplace()Replace every occurrence of a search string, ignoring upper and lower case. Otherwise works like str_replace().
substr_replace()Replace part of a string, given a start position and length, with another string. Also inserts text or masks digits.
strtr()Translate characters, or replace substrings in one pass using an array of from => to pairs. Longest keys match first.
substr()Return part of a string, given a start position and an optional length. Negative values count from the end.
strpos()Find the position of the first occurrence of a substring. Returns false if it's not found, so compare with === false.
stripos()Find the position of the first occurrence of a substring, ignoring case. Returns false when there's no match.
strrpos()Find the position of the last occurrence of a substring in a string. Returns false if it isn't found.
strstr()Return the part of a string from the first occurrence of a substring to the end, or the part before it.
strrchr()Return the part of a string from the last occurrence of a character to the end. Only the first character of the needle is used.
str_contains()Check whether a string contains a substring and return true or false. Case-sensitive; added in PHP 8.0.
str_starts_with()Check whether a string begins with a given substring. Case-sensitive and returns true or false; added in PHP 8.0.
str_ends_with()Check whether a string ends with a given substring, such as a file extension. Case-sensitive; added in PHP 8.0.
substr_count()Count how many times a substring occurs in a string. Matches don't overlap, and the search is case-sensitive.
explode()Split a string into an array by a separator, like turning "a,b,c" into ["a", "b", "c"]. A limit caps the pieces.
implode()Join array elements into one string with a separator between them. join() is an alias of implode().
str_split()Split a string into an array of chunks of a given length, one byte each by default. Use mb_str_split() for UTF-8.

All 61 string functions →

Multibyte String Functions

The mb_* functions for UTF-8 text: mb_strlen(), mb_substr(), mb_strtolower(), mb_str_pad(), mb_trim() and more, counting characters instead of bytes.

NameDescription
mb_strlen()Return the number of characters in a string, counting é or 日 as one character. The UTF-8-safe version of strlen().
mb_substr()Return part of a string by character position, so multibyte characters are never cut in half. The UTF-8 version of substr().
mb_strtolower()Convert a string to lowercase, including letters outside ASCII such as É, Ü and Cyrillic. The Unicode-aware strtolower().
mb_strtoupper()Convert a string to uppercase, including letters outside ASCII such as é, ß and Cyrillic. The Unicode-aware strtoupper().
mb_convert_case()Change the case of a string with Unicode rules: upper, lower, title case or case folding for comparisons.
mb_strpos()Find the character position of the first occurrence of a substring in a multibyte string. Returns false if not found.
mb_str_split()Split a multibyte string into an array of characters, or of chunks of n characters. The UTF-8-safe str_split().
mb_str_pad()Pad a multibyte string to a given length in characters, on the left, right or both sides. Added in PHP 8.3.
mb_trim()Strip whitespace from both ends of a string, including Unicode spaces such as the non-breaking space. Added in PHP 8.4.
mb_ucfirst()Make the first character of a string title case, including letters such as é or ı. Added in PHP 8.4.

Array Functions

PHP array functions: count(), array_map(), array_filter(), in_array(), array_merge(), array_column(), array_find() and more, with runnable examples.

NameDescription
count()Return the number of elements in an array or Countable object. With COUNT_RECURSIVE it also counts nested elements.
array_push()Add one or more elements to the end of an array and return its new length. $array[] = $value does the same for one value.
array_pop()Remove the last element of an array and return it. Returns null if the array is empty.
array_shift()Remove the first element of an array and return it. Numeric keys are renumbered from 0; string keys stay.
array_unshift()Add one or more elements to the start of an array and return its new length. Numeric keys are renumbered.
array_merge()Merge one or more arrays into a new array. Numeric keys are renumbered; for string keys the later value wins.
array_merge_recursive()Merge arrays and, when string keys clash, combine their values into arrays instead of overwriting.
array_combine()Create an array by using one array for keys and another for values. Both arrays must have the same number of elements.
array_slice()Return a portion of an array without changing the original, given an offset and an optional length.
array_splice()Remove a portion of an array and optionally replace it with other elements, changing the array in place.
array_map()Apply a callback to every element of one or more arrays and return a new array of the results.
array_filter()Keep only the elements for which a callback returns true. Without a callback it removes empty values; keys are kept.
array_reduce()Reduce an array to a single value by passing a running total and each element to a callback.
array_walk()Run a callback on every element of an array, with the value and key. Take the value by reference to change it in place.
array_keys()Return all the keys of an array, or only the keys whose value matches a search value.
array_values()Return all the values of an array as a list re-indexed from 0. Often used after array_filter() or unset().
array_key_exists()Check whether an array has a given key, even if its value is null. isset() returns false for null values.
in_array()Check whether a value exists in an array. Pass true as the third argument for a strict (===) comparison.
array_search()Search an array for a value and return the first matching key, or false if it isn't there.
array_key_first()Return the first key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.
array_key_last()Return the last key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.
array_is_list()Check whether an array's keys are 0, 1, 2… in order, so it would encode as a JSON array. Added in PHP 8.1.
array_find()Return the first element for which a callback returns true, or null if none match. Added in PHP 8.4.
array_find_key()Return the key of the first element for which a callback returns true, or null if none match. Added in PHP 8.4.
array_any()Return true if at least one element passes a callback test, stopping at the first match. Added in PHP 8.4.
array_all()Return true if every element passes a callback test, stopping at the first failure. Added in PHP 8.4.
array_first()Return the first value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.
array_last()Return the last value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.
array_unique()Remove duplicate values from an array, keeping the first occurrence of each and its key.
array_reverse()Return a new array with the elements in reverse order. String keys are kept; numeric keys are renumbered by default.

All 49 array functions →

Array Sorting Functions

Every PHP array sorting function compared: sort(), rsort(), usort(), asort(), ksort(), natsort() and more, with which ones keep keys.

NameDescription
sort()Sort an array's values in ascending order, in place. Keys are discarded and the result is renumbered from 0.
rsort()Sort an array's values in descending order, in place. Keys are discarded and the result is renumbered from 0.
usort()Sort an array's values with your own comparison function, in place. Keys are discarded. Use <=> for easy comparisons.
asort()Sort an array by value in ascending order, keeping each key attached to its value. Ideal for name => score maps.
arsort()Sort an array by value in descending order, keeping each key attached to its value. Great for leaderboards.
uasort()Sort an array by value with your own comparison function, keeping keys attached to their values.
ksort()Sort an array by key in ascending order, keeping each value with its key.
krsort()Sort an array by key in descending order, keeping each value with its key.
uksort()Sort an array by key with your own comparison function, keeping values with their keys.
natsort()Sort an array in natural order, so "img2" comes before "img10", keeping keys. Case-sensitive.
natcasesort()Sort an array in natural order while ignoring case, keeping keys. "Item2" comes before "item10".
shuffle()Shuffle an array into a random order, in place. Keys are discarded. Not suitable for security-sensitive randomness.
array_multisort()Sort several arrays at once, or a table by several columns, keeping rows lined up. Changes the arrays in place.

Was this page helpful?