I oftentimes forget the syntax of some of the most commonly used PHP functions so I created this simple PHP functions cheat sheet which you can have open in your web browser for quick reference.
explode($separator, $string, [$number_of_array_parts]);
↗
Break apart [$string] into an array of parts, splitting [$string] at each occurrence of [$separator].
|
implode([$join_character], $array);
↗
Takes the parts of an [$array] and joins them into a string.
In the examples below we will assume [$array] = Array([0]=>PHP [1]=>Programming [2]=>Makes me Explode in my Pants)
|
nl2br($string, [$xhtml_compatible]);
↗
Function to insert proper HTML break tags to ensure a new line is output everwhere
|
str_contains($string, $strLookingFor);
↗
// case sensitive
Check to see if $string has or contains $strLookingFor.
|
str_pad($string, $output_length, [$padding_string], [$padding_type]);
↗
Make a string a specific length so even if the actual string is shorter than [$output_length] [$string] gets padded so the final length is [$output_length]. Generally used for visual formatting purposes.
|
str_replace($find, $replacement, $string, [$return_number_replacements]);
↗
// case sensitive
Find all instances of [$find] in [$string] and replace each of them with [$replacement]. [$return_number_replacements] is an optional variable that returns to you the number of replacements found.
|
str_split($string, [$each_arr_len]);
↗
Split [$string] into an array, each element of the array being the length of [$each_arr_len].
|
strlen($string);
↗
Returns the length number of characters of [$string].
|
strpos($string, $find, [$start]);
↗
// case sensitive
Returns position of first occurrence of [$find] within [$string].
|
stripos($string, $find, [$start]);
↗
// case ignored
Returns position of first occurrence of [$find] within [$string].
|
strripos($string, $find, [$start]);
↗
// case ignored
Returns position of last occurrence of [$find] within [$string].
|
strstr($string, $find, [$return_before]);
↗
// case sensitive
Finds first occurrence of [$find] within [$string] and returns everything from the start of [$find] to the end.
|
stristr($string, $find, [$return_before]);
↗
// case ignored
Finds first occurrence of [$find] within [$string] and returns everything from the start of [$find] to the end.
|
strrev($string);
↗
Takes [$string] and reverses the characters so the string read backwards.
|
strtolower($string);
↗
Converts [$string] to all lowercase letters.
|
strtoupper($string);
↗
Converts [$string] to all UPPER CASE letters.
|
ucfirst($string);
↗
Converts first character of [$string] to upper case and leaves the rest of [$string] alone. To make an entire [$string] lower case with first letter capitalized use strtolower() to lowercase the entire [$string] first.
|
ucwords($string);
↗
Converts first letter of each word of [$string] to upper case. To capitialize just the first letter of the first word of [$string] use function [ucfirst].
|
lcfirst($string);
↗
Converts first character of [$string] to lower case and leaves the rest of [$string] alone.
|
substr($string, $startpos, [$length]);
↗
Returns part of [$string] starting at [$startpos] and is the length of [$length].
|
substr_count($string, $substring, [$start], [$length]);
↗
// case sensitive
Returns how many times [$substring] can be found within [$string] with [$start] being the start position to start searching for [$substring] within [$string] and [$length] being the endpoint of the search.
|
substr_replace($string, $replacement, $start, [$length]);
↗
Replace [$string] or part of [$string] with [$replacement]. If [$start] = 0 and [$length] argument omitted, entire [$string] replaced with [$replacement].
|
array_column($array, $columnKey, [$indexKey]);
↗
Returns the values from a single column in the input $array.
|
array_fill($startIndex, $numberElements, $fillValues);
↗
Create an array on the fly.
|
array_filter($array, [$filteringFunction], [$flag]);
↗
Filter out certain elements of an array.
|
array_intersect($mainArray, $arrToCheck1, [$arrToCheck1], ...);
↗
Find and return as an array the values of any array elements of the first argument of array_intersect() $mainArray exist in ALL remaining arguments ie $arrToCheck1, $arrToCheck2, etc.
|
array_intersect_assoc($mainArray, $arrToCheck1, [$arrToCheck1], ...);
↗
Find and return as an associative array the keys and the values of any array elements of the first argument of array_intersect_assoc() $mainArray exist in ALL remaining arguments ie $arrToCheck1, $arrToCheck2, etc. Both the keys and the values have to match to be returned.
|
array_key_exists($key, $array);
↗
Search $array to see if the array key of $key exists.
|
array_map($functionToMap, $arrToMap1, [$arrToMap2], ...);
↗
Create an array by modifying the elements of an existing array $arrToMap1 via function $functionToMap.
|
array_merge($array1, $array2, [$array3], [$array4] ...);
↗
Combine/merge 2 or more arrays as a single new array.
|
array_search($whatToSearchFor, $arrayToSearch, [$strictMatch]);
↗
Search an array $arrayToSearch for a value $whatToSearchFor. Returns the key of $arrayToSearch of the matching $whatToSearchFor, if found or returns false.
To perform a case insensitive search using array_search ...
|
array_shift($array);
↗
Remove and return the first element of $array.
|
array_unique($array, [$sortType]);
↗
Removes any duplicates in array to ensure each element of $array is unique.
|
array_unshift($array, $itemToAdd1, [$itemToAdd2, $itemToAdd3, ...]);
↗
Insert new element(s) to the beginning of an array.
|
in_array($searchFor, $arr, [$type]);
↗
See if $searchFor can be found as an element in $arr. Returns TRUE if found or FALSE.
|
file_exists($path_to_check);
↗
Returns [true/false] whether file or path [$path_to_check] exists.
|
mkdir($path, $permissions, [$recursive], [$context]);
↗
Creates a directory you specify via [$path]. Returns [true/false]. For [$path] you should use server path ie [$permissions] is a 4 digit number which refers to read/write permissions of the newly created directory.
If [$recursive] set to false, only the last directory item after the last
|
move_uploaded_file($from, $to);
↗
// if file at [$to] already exists it will be overwritten
Takes a file uploaded via POST ie $file["tmp_name"] and places in the location specified by [$to].
|
error_log($err_message, [$err_dest_type], [$err_sent_to], [$email_headers]);
↗
Saves php errors so they can be analyzed/debugged. These error messages can be sent to a log, file, or email address. The return is either true on success $err_message = the text of the error message you want reported/printed. [$err_dest_type] = where the error message will be saved or printed or output.
[$err_sent_to] = where the error message will saved/sent/printed.
[$email_headers] is only used if [$err_dest_type] = 1 sent to email. Specifies email headers, like From, Cc, and Bcc. Multiple headers should be separated with a line break \r\n
|