PHP reference
PHP reference
PHP functions, classes and methods: syntax, parameters, return values and examples.
Strings
- addslashesAdds a backslash before quotes.
- bin2hexConverts the bytes of a string to hexadecimal.
- chunk_splitSplits a string into chunks, inserting a separator after each.
- crc32Calculates the CRC32 checksum of a string.
- explodeSplits a string into an array by a separator.
- hashReturns the hash of a string using a chosen algorithm.
- hash_equalsCompares two strings in constant time.
- hash_hmacCalculates a signature of data with a secret key.
- htmlentitiesConverts all applicable characters to HTML entities.
- implodeJoins array elements into a string.
- lcfirstMakes the first letter of a string lowercase.
- levenshteinCounts how many edits turn one string into another.
- ltrimRemoves whitespace or given characters from the start of a string.
- mb_convert_caseChanges the case of a string, aware of multibyte characters.
- mb_str_padPads a string to a length, counting characters rather than bytes.
- mb_str_splitSplits a string into characters for any alphabet.
- mb_strimwidthTruncates a string to a given width and appends a marker.
- mb_strlenReturns the number of characters in a string.
- mb_strposFinds the position of the first occurrence of a substring.
- mb_strtolowerConverts a string to lowercase for any alphabet.
- mb_strtoupperConverts a string to uppercase.
- mb_substrReturns part of a string.
- mb_substr_countCounts how many times a substring occurs in a string.
- mb_trimStrips whitespace from both ends of a string, including multibyte whitespace.
- mb_ucfirstMakes the first letter of a string uppercase, aware of multibyte characters.
- md5Returns the MD5 hash of a string.
- nl2brInserts a br tag before every line break.
- preg_grepReturns the array elements that match a pattern.
- preg_matchTests a string against a regular expression.
- preg_match_allFinds every match of a regular expression.
- preg_quoteEscapes regular expression special characters.
- preg_replaceReplaces everything that matches a regular expression.
- preg_replace_callbackReplaces pattern matches with the result of a function.
- preg_splitSplits a string by a regular expression.
- printfPrints a string formatted by a template.
- random_bytesReturns cryptographically secure random bytes.
- rtrimRemoves whitespace or given characters from the end of a string.
- sha1Calculates the SHA-1 hash of a string.
- similar_textCalculates how similar two strings are.
- soundexReturns the sound code of an English word.
- sprintfReturns a string built from a format template.
- str_containsChecks whether a string contains a given substring.
- str_ends_withChecks whether a string ends with a given substring.
- str_ireplaceReplaces a substring ignoring case.
- str_padPads a string to a given length.
- str_repeatRepeats a string a given number of times.
- str_replaceReplaces all occurrences of a substring with another string.
- str_splitSplits a string into pieces of a given length.
- str_starts_withChecks whether a string starts with a given substring.
- str_word_countCounts the words in a string.
- strcasecmpCompares two strings, ignoring case.
- strcmpCompares two strings.
- strip_tagsRemoves HTML tags from a string.
- striposFinds the position of a substring, ignoring case.
- strlenReturns the length of a string in bytes.
- strnatcmpCompares strings in "natural" order — numbers inside strings as numbers.
- strposFinds the position of the first occurrence of a substring.
- strrevReverses a string.
- strrposFinds the position of the last occurrence of a substring.
- strstrReturns the part of a string starting at the first occurrence of a substring.
- strtolowerConverts ASCII letters to lowercase.
- strtoupperConverts ASCII letters to uppercase.
- strtrReplaces several substrings in a single pass.
- substrReturns part of a string, counting in bytes.
- substr_countCounts how many times a substring occurs in a string.
- substr_replaceReplaces part of a string by position.
- trimRemoves whitespace and other characters from both ends of a string.
- ucfirstMakes the first letter of a string uppercase.
- ucwordsUppercases the first letter of every word.
- uniqidReturns an identifier based on the current time.
- vsprintfFormats a string by a template, taking the values as an array.
- wordwrapWraps a long string at word boundaries.
Arrays
- array_allChecks whether every element satisfies a condition.
- array_anyChecks whether at least one element satisfies a condition.
- array_chunkSplits an array into chunks of a given size.
- array_columnPulls the values of one key out of an array of arrays.
- array_combineCreates an array from two lists: keys and values.
- array_count_valuesCounts how many times each value occurs.
- array_diffReturns the values of the first array that are absent from the others.
- array_diff_assocReturns the elements of the first array missing from the others, comparing both keys and values.
- array_diff_keyReturns the elements of the first array whose keys are absent from the others.
- array_fillCreates an array of identical values.
- array_fill_keysCreates an array with given keys and the same value.
- array_filterKeeps the array elements that pass a test.
- array_findReturns the first element for which the function returned true.
- array_find_keyReturns the key of the first element that satisfies a condition.
- array_flipSwaps keys and values.
- array_intersectReturns the values present in every array.
- array_intersect_keyReturns the elements of the first array whose keys are present in the others.
- array_is_listChecks that the keys of an array run in order from zero.
- array_key_existsChecks whether an array has a key.
- array_key_firstReturns the first key of an array.
- array_key_lastReturns the last key of an array.
- array_keysReturns all the keys of an array.
- array_mapApplies a function to every element of an array.
- array_mergeMerges one or more arrays into one.
- array_merge_recursiveMerges arrays, combining nested arrays that share a key.
- array_multisortSorts several arrays at once, or a table by several columns.
- array_padPads an array with a value to a given length.
- array_popRemoves the last element of an array and returns it.
- array_productMultiplies the elements of an array.
- array_pushAppends elements to the end of an array.
- array_randReturns a random key of an array.
- array_reduceReduces an array to a single value.
- array_replaceReplaces the values of an array with values under the same keys from other arrays.
- array_reverseReturns an array with its elements in reverse order.
- array_searchSearches an array for a value and returns its key.
- array_shiftRemoves the first element of an array and returns it.
- array_sliceReturns part of an array.
- array_spliceRemoves part of an array and optionally inserts new elements in its place.
- array_sumReturns the sum of the values in an array.
- array_uniqueRemoves duplicate values from an array.
- array_unshiftPrepends elements to the beginning of an array.
- array_valuesReturns the values of an array with keys numbered from zero.
- array_walkCalls a function for every element of an array.
- arsortSorts an array by values in descending order, keeping the keys.
- asortSorts an array by value, keeping the keys.
- compactGathers variables into an array by their names.
- countReturns the number of elements in an array.
- currentReturns the element the array's internal pointer points to.
- endReturns the last element of an array.
- extractCreates variables from the keys of an array.
- in_arrayChecks whether a value exists in an array.
- iterator_to_arrayTurns an iterator or a generator into an array.
- keyReturns the key of the array's current element.
- krsortSorts an array by keys in descending order.
- ksortSorts an array by key.
- natsortSorts strings the way a person would order them.
- nextAdvances the array's internal pointer to the next element.
- rangeCreates an array holding a sequence of values.
- resetReturns the first element of an array.
- rsortSorts an array in descending order.
- shuffleShuffles the elements of an array into a random order.
- sortSorts an array in ascending order.
- uasortSorts an array by a custom rule, keeping the keys.
- uksortSorts an array by keys using your own comparison function.
- usortSorts an array by a custom comparison rule.
Numbers
- absReturns the absolute value of a number.
- base_convertConverts a number from one base to another.
- bindecConverts a binary string to a decimal number.
- ceilRounds a number up to an integer.
- decbinConverts a number to binary.
- dechexConverts a number to hexadecimal.
- expRaises the number e to a power.
- fdivDivides numbers without raising an error on division by zero.
- floorRounds a number down to the nearest integer.
- fmodReturns the remainder of a division of floats.
- hexdecConverts a hexadecimal string to a decimal number.
- intdivPerforms integer division.
- is_nanChecks whether a value is "not a number" (NAN).
- logCalculates the logarithm of a number.
- maxReturns the largest of the given values.
- minReturns the smallest of the values.
- mt_randReturns a random integer.
- number_formatFormats a number for display: decimals and a thousands separator.
- piReturns the number pi.
- powRaises a number to a power.
- randReturns a random integer.
- random_intReturns a random integer from a range.
- roundRounds a number to a given number of digits.
- sqrtReturns the square root of a number.
Types and variables
- boolvalConverts a value to a boolean.
- emptyChecks that a variable does not exist or holds an "empty" value.
- floatvalConverts a value to a float.
- get_debug_typeReturns the type name of a value the way it is written in code.
- gettypeReturns the name of a value's type.
- intvalConverts a value to an integer.
- is_arrayChecks whether a value is an array.
- is_boolChecks whether a value is a boolean.
- is_callableChecks whether a value can be called as a function.
- is_countableChecks whether a value can be passed to `count()`.
- is_floatChecks whether a value is a float.
- is_intChecks whether a value is an integer.
- is_iterableChecks whether a value can be looped over with `foreach`.
- is_nullChecks whether a value is null.
- is_numericChecks whether a value is a number or a numeric string.
- is_objectChecks whether a value is an object.
- is_stringChecks whether a value is a string.
- issetChecks that a variable exists and is not null.
- print_rPrints a value in a readable form.
- serializeTurns a value into a string for storage.
- settypeChanges the type of a variable in place.
- strvalConverts a value to a string.
- unsetDestroys a variable or an array element.
- var_dumpPrints the type and the value — for debugging.
- var_exportPrints a value as PHP code.
Date and time
- checkdateChecks whether such a date exists.
- dateFormats a date and time.
- date_createCreates a date object; returns `false` for an invalid string.
- date_default_timezone_setSets the time zone for every date function.
- date_diffCalculates the difference between two dates.
- hrtimeReturns a high-resolution timer reading in nanoseconds.
- microtimeReturns the current time with microsecond precision.
- mktimeBuilds a timestamp from separate parts of a date.
- sleepPauses the program for a number of seconds.
- strtotimeTurns a textual date description into a timestamp.
- timeReturns the current timestamp.
Files and folders
- basenameReturns the last part of a path — the file name.
- chmodChanges the permissions of a file or folder.
- copyCopies a file.
- dirnameReturns the path of the folder that holds a file.
- fcloseCloses an open file.
- feofChecks whether the end of a file has been reached.
- fgetcsvReads a line of a CSV file and splits it into fields.
- fgetsReads one line from an open file.
- fileReads a file into an array of lines.
- file_existsChecks whether a file or folder exists.
- file_get_contentsReads a whole file into a string.
- file_put_contentsWrites data to a file.
- filemtimeReturns the time a file was last modified.
- filesizeReturns the size of a file in bytes.
- flockLocks a file so that two processes do not work with it at once.
- fopenOpens a file for reading or writing piece by piece.
- fputcsvWrites an array to a file as a CSV line.
- freadReads a number of bytes from an open file.
- fwriteWrites a string to an open file.
- globFinds files by a name pattern.
- is_dirChecks whether a path is a folder.
- is_fileChecks that a path exists and is a file rather than a folder.
- is_readableChecks that a file exists and can be read.
- is_writableChecks whether a file or folder can be written to.
- mkdirCreates a folder.
- move_uploaded_fileMoves a file uploaded through a form to its permanent place.
- parse_ini_fileReads a settings file in ini format.
- pathinfoSplits a path into parts: the folder, the name and the extension.
- readfileOutputs the contents of a file.
- realpathReturns the full path without `..`, `.` or symbolic links.
- renameRenames or moves a file.
- rmdirRemoves an empty folder.
- scandirReturns the list of files and folders in a folder.
- tempnamCreates a temporary file with a unique name.
- touchCreates an empty file or updates its modification time.
- unlinkDeletes a file.
JSON and web
- base64_decodeDecodes a string from Base64.
- base64_encodeEncodes data as text made of ASCII letters and digits.
- curl_execExecutes a cURL request.
- curl_initCreates a cURL request.
- curl_setoptSets an option of a cURL request.
- filter_inputReads a value from the request and validates it at once.
- filter_varValidates or sanitises a value by a given rule.
- getenvReturns the value of an environment variable.
- headerSends an HTTP response header.
- html_entity_decodeTurns HTML entities back into characters.
- htmlspecialcharsReplaces HTML special characters with safe entities.
- htmlspecialchars_decodeConverts the `<`, `>`, `&`, `"` entities back to characters.
- http_build_queryBuilds a URL query string from an array.
- http_response_codeSets or returns the response status code.
- json_decodeParses a JSON string.
- json_encodeConverts a value to a JSON string.
- json_last_error_msgReturns the error description of the last `json_decode()` or `json_encode()` call.
- json_validateChecks whether a string is valid JSON.
- mailSends an email through the server's mail program.
- parse_strParses a query string into an array.
- parse_urlSplits a URL into its parts.
- password_hashCreates a password hash for storing in a database.
- password_verifyChecks whether a password matches a hash.
- rawurlencodeEncodes a string for use in the path of an address.
- session_destroyDeletes the session data on the server.
- session_regenerate_idGives the session a new identifier, keeping the data.
- session_startStarts a session or resumes the existing one.
- setcookieSends a cookie to the browser.
- unserializeRestores a value from a string produced by `serialize()`.
- urldecodeDecodes a string from a URL.
- urlencodeEncodes a string for use in a URL.
Classes
- ArrayAccessThe interface of objects accessed with square brackets.
- ArrayObjectAn object that behaves like an array.
- ClosureThe class of anonymous functions and closures.
- CountableThe interface of objects that can be passed to `count()`.
- DateIntervalA span of time: days, months, hours.
- DatePeriodA set of dates with an equal step to loop over.
- DateTimeA mutable date and time object.
- DateTimeImmutableA date and time object that cannot be changed: every operation returns a new object.
- ErrorThe base class of PHP's internal errors.
- ExceptionThe base class of exceptions.
- GeneratorThe object returned by a function containing `yield`.
- InvalidArgumentExceptionThe exception for an invalid function argument.
- IteratorAn interface with five methods for controlling iteration by hand.
- IteratorAggregateThe interface of objects that can be looped over with `foreach`.
- JsonSerializableAn interface defining how an object turns into JSON.
- mysqliA connection to a MySQL database.
- PDOA database connection.
- PDOStatementA prepared statement and its result.
- ReflectionClassInformation about a class: its methods, properties and attributes.
- RuntimeExceptionAn exception for errors that only show up at run time.
- stdClassAn empty class for objects with arbitrary properties.
- StringableThe interface of objects that can be turned into a string.
- ThrowableThe common interface of everything that can be thrown and caught.
- TypeErrorThe error of a mismatched argument or return type.
Functions and classes
- call_user_funcCalls a function held in a variable.
- call_user_func_arrayCalls a function, passing the arguments as an array.
- class_existsChecks whether a class is defined.
- constantReturns the value of a constant by its name in a string.
- debug_backtraceReturns the chain of calls that led to the current point.
- defineCreates a constant at run time.
- definedChecks whether a constant exists.
- error_logWrites a message to the error log.
- error_reportingSets which errors PHP should report.
- exitTerminates the program.
- extension_loadedChecks whether a PHP extension is loaded.
- func_get_argsReturns every argument passed to the function.
- function_existsChecks whether a function is defined.
- get_classReturns the class name of an object.
- get_class_methodsReturns the names of a class's methods.
- get_object_varsReturns the accessible properties of an object as an array.
- ini_setChanges a PHP setting for the duration of the program.
- is_aChecks whether an object belongs to a class or one of its children.
- is_subclass_ofChecks whether a class is a child of another class.
- memory_get_usageReturns the amount of memory the program uses.
- method_existsChecks whether a class or an object has a method.
- property_existsChecks whether a class or an object has a property.
- register_shutdown_functionRegisters a function to run when the program ends.
- set_error_handlerSets a custom function to handle PHP warnings and notices.
- set_exception_handlerSets a function for exceptions nobody caught.
- spl_autoload_registerRegisters a function that loads a class file the first time the class is used.
- trigger_errorRaises a user-level warning or notice.
- version_compareCompares two version numbers.