PHP Cheatsheet
55 essential methods for coding interviews
Showing 55 of 55 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
array_map | array_map(callable $callback, array $array, array ...$arrays): array | Apply a callback to each element of an array | O(n) | essential |
array_filter | array_filter(array $array, ?callable $callback = null, int $mode = 0): array | Filter elements using a callback function | O(n) | essential |
array_reduce | array_reduce(array $array, callable $callback, mixed $initial = null): mixed | Reduce array to a single value using callback | O(n) | essential |
array_merge | array_merge(array ...$arrays): array | Merge one or more arrays | O(n + m) | essential |
in_array | in_array(mixed $needle, array $haystack, bool $strict = false): bool | Check if a value exists in an array | O(n) | essential |
array_search | array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false | Search for a value and return its key | O(n) | essential |
array_keys | array_keys(array $array, mixed $filter_value = null, bool $strict = false): array | Return all keys or keys for a specific value | O(n) | essential |
array_values | array_values(array $array): array | Return all values with numeric keys | O(n) | essential |
array_push | array_push(array &$array, mixed ...$values): int | Push one or more elements onto end of array | O(1) amortized | essential |
array_pop | array_pop(array &$array): mixed | Pop element from end of array | O(1) | essential |
array_shift | array_shift(array &$array): mixed | Shift element from beginning of array | O(n) | essential |
array_slice | array_slice(array $array, int $offset, ?int $length = null, bool $preserve_keys = false): array | Extract a portion of an array | O(n) | essential |
array_unique | array_unique(array $array, int $flags = SORT_STRING): array | Remove duplicate values from array | O(n log n) | essential |
array_count_values | array_count_values(array $array): array | Count occurrences of each value | O(n) | essential |
array_key_exists | array_key_exists(string|int $key, array $array): bool | Check if key exists in array | O(1) | essential |
count | count(Countable|array $value, int $mode = COUNT_NORMAL): int | Count elements in array or object | O(1) | essential |
sort | sort(array &$array, int $flags = SORT_REGULAR): bool | Sort array in ascending order | O(n log n) | essential |
usort | usort(array &$array, callable $callback): bool | Sort array using user-defined comparison function | O(n log n) | essential |
explode | explode(string $separator, string $string, int $limit = PHP_INT_MAX): array | Split string by delimiter into array | O(n) | essential |
implode | implode(string $separator, array $array): string | Join array elements with separator | O(n) | essential |
substr | substr(string $string, int $offset, ?int $length = null): string | Return part of a string | O(n) | essential |
str_replace | str_replace(array|string $search, array|string $replace, string|array $subject, int &$count = null): string|array | Replace occurrences of search with replace | O(n * m) | essential |
strlen | strlen(string $string): int | Get string length in bytes | O(1) | essential |
strpos | strpos(string $haystack, string $needle, int $offset = 0): int|false | Find position of first occurrence of substring | O(n * m) | essential |
strtolower | strtolower(string $string): string | Convert string to lowercase | O(n) | essential |
trim | trim(string $string, string $characters = " \n\r\t\v\x00"): string | Strip whitespace from beginning and end | O(n) | essential |
str_split | str_split(string $string, int $length = 1): array | Convert string to array of characters | O(n) | essential |
preg_match | preg_match(string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0): int|false | Perform regular expression match | O(n) | essential |
max | max(mixed $value, mixed ...$values): mixed | Find highest value | O(n) | essential |
min | min(mixed $value, mixed ...$values): mixed | Find lowest value | O(n) | essential |
abs | abs(int|float $num): int|float | Get absolute value | O(1) | essential |
array_column | array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array | Return values from single column | O(n) | essential |
array_sum | array_sum(array $array): int|float | Calculate sum of array values | O(n) | essential |
array_unshift | array_unshift(array &$array, mixed ...$values): int | Prepend elements to beginning of array | O(n) | common |
array_splice | array_splice(array &$array, int $offset, ?int $length = null, mixed $replacement = []): array | Remove and/or replace portion of array | O(n) | common |
array_reverse | array_reverse(array $array, bool $preserve_keys = false): array | Return array in reverse order | O(n) | common |
array_flip | array_flip(array $array): array | Exchange keys and values | O(n) | common |
array_diff | array_diff(array $array, array ...$arrays): array | Compute difference of arrays | O(n * m) | common |
array_intersect | array_intersect(array $array, array ...$arrays): array | Compute intersection of arrays | O(n * m) | common |
rsort | rsort(array &$array, int $flags = SORT_REGULAR): bool | Sort array in descending order | O(n log n) | common |
ksort | ksort(array &$array, int $flags = SORT_REGULAR): bool | Sort array by keys in ascending order | O(n log n) | common |
strtoupper | strtoupper(string $string): string | Convert string to uppercase | O(n) | common |
sprintf | sprintf(string $format, mixed ...$values): string | Return formatted string | O(n) | common |
preg_replace | preg_replace(string|array $pattern, string|array $replacement, string|array $subject, int $limit = -1, int &$count = null): string|array|null | Perform regular expression search and replace | O(n) | common |
floor | floor(int|float $num): float | Round down to nearest integer | O(1) | common |
ceil | ceil(int|float $num): float | Round up to nearest integer | O(1) | common |
round | round(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float | Round a number | O(1) | common |
intval | intval(mixed $value, int $base = 10): int | Get integer value of variable | O(n) | common |
strval | strval(mixed $value): string | Get string value of variable | O(1) | common |
is_array | is_array(mixed $value): bool | Check if variable is an array | O(1) | common |
is_numeric | is_numeric(mixed $value): bool | Check if variable is numeric | O(1) | common |
array_walk | array_walk(array|object &$array, callable $callback, mixed $arg = null): bool | Apply callback to each element in place | O(n) | common |
array_combine | array_combine(array $keys, array $values): array | Create array using keys and values arrays | O(n) | common |
array_fill | array_fill(int $start_index, int $count, mixed $value): array | Fill array with values | O(n) | common |
range | range(string|int|float $start, string|int|float $end, int|float $step = 1): array | Create array of sequential values | O(n) | common |