Appendix. Function Reference

This appendix describes the functions available in the built-in PHP extensions. These are the extensions that PHP is built with if you provide no --with or --enable options to configure, and they cannot be removed via configuration options.

For each function, we’ve provided the function signature, showing the data types of the various arguments and which are mandatory or optional, as well as a brief description of the side effects, errors, and returned data structures.

PHP Functions by Category

This section gives a list of functions provided by PHP’s built-in extensions, grouped by extension category.

Functions

call_user_func

call_user_func_array

create_function

forward_static_call

forward_static_call_array

func_get_arg

func_get_args

func_num_args

function_exists

get_defined_functions

register_shutdown_function

register_tick_function

unregister_tick_function

Alphabetical Listing of PHP Functions

date

string date(string format[, int timestamp])

Formats a time and date according to the format string provided in the first parameter. If the second parameter is not specified, the current time and date is used. The following characters are recognized in the format string:

a “am” or “pm”
A “AM” or “PM”
B Swatch internet time
d Day of the month as two digits, including a leading zero if necessary (e.g., “01” through “31”)
D Name of the day of the week as a three-letter abbreviation (e.g., “Mon”)
F Name of the month (e.g., “August”)
g Hour in 12-hour format (e.g., “1” through “12”)
G Hour in 24-hour format (e.g., “0” through “23”)
h Hour in 12-hour format, including a leading zero if necessary; e.g., “01” through “12”
H Hour in 24-hour format, including a leading zero if necessary (e.g., “00” through “23”)
i Minutes, including a leading zero if necessary (e.g., “00” through “59”)
I “1” if Daylight Saving Time; “0” otherwise
j Day of the month (e.g., “1” through “31”)
l Name of the day of the week (e.g., “Monday”)
L “0” if the year is not a leap year; “1” if it is
m Month, including a leading zero if necessary (e.g., “01” through “12”)
M Name of the month as a three-letter abbreviation (e.g., “Aug”)
n Month without leading zeros (e.g., “1” to “12”)
r Date formatted according to RFC 822 (e.g., “Thu, 21 Jun 2001 21:27:19 +0600”)
s Seconds, including a leading zero if necessary (e.g., “00” through “59”)
S English ordinal suffix for the day of the month; either “st”, “nd”, or “th”
t Number of days in the month, from “28” to “31”
T Time zone setting of the machine running PHP (e.g., “MST”)
u Seconds since the Unix epoch
w Numeric day of the week, starting with “0” for Sunday
W Numeric week of the year according to ISO 8601
Y Year with four digits (e.g., “1998”)
y Year with two digits (e.g., “98”)
z Day of the year, from “0” through “365”
Z Time zone offset in seconds, from “–43200” (far west of UTC) to “43200” (far east of UTC)

Any characters in the format string not matching one of the above will be kept in the resulting string as is. If a non-numeric value is provided for timestamp, then false is returned and a warning is issued.

error_reporting

int error_reporting([int level])

Sets the level of errors reported by PHP to level and returns the current level; if level is omitted, the current level of error reporting is returned. The following values are available for the function:

E_ERROR Fatal runtime errors (script execution halts)
E_WARNING Runtime warnings
E_PARSE Compile-time parse errors
E_NOTICE Runtime notices
E_CORE_ERROR Errors generated internally by PHP
E_CORE_WARNING Warnings generated internally by PHP
E_COMPILE_ERROR Errors generated internally by the Zend scripting engine
E_COMPILE_WARNING Warnings generated internally by the Zend scripting engine
E_USER_ERROR Runtime errors generated by a call to trigger_error()
E_USER_WARNING Runtime warnings generated by a call to trigger_error()
E_STRICT Direct PHP to suggest code changes to assist with forward compatibility
E_RECOVERABLE_ERROR If a potentially fatal error has occurred, was caught, and properly handled, the code can continue execution
E_DEPRECATED If enabled, warnings will be issued about deprecated code that will eventually not work properly
E_USER_DEPRECATED If enabled, any warning message triggered by deprecated code can be user-generated with the trigger_error() function
E_ALL All of the above options

Any number of these options can be ORed (bitwise OR, |) together, so that errors in each of the levels are reported. For example, the following code turns off user errors and warnings, performs some actions, and then restores the original level:

<$level = error_reporting();
 error_reporting($level & ~(E_USER_ERROR | E_USER_WARNING));
 // do some stuff
 error_reporting($level);>

fopen

resource fopen(string path, string mode[, bool include [, resource context]])

Opens the file specified by path and returns a file resource handle to the open file. If path begins with http://, an HTTP connection is opened and a file pointer to the start of the response is returned. If path begins with ftp://, an FTP connection is opened and a file pointer to the start of the file is returned; the remote server must support passive FTP.

If path is php://stdin, php://stdout, or php://stderr, a file pointer to the appropriate stream is returned.

The parameter mode specifies the permissions to open the file with. It must be one of the following:

r Open the file for reading; file pointer will be at beginning of file.
r+ Open the file for reading and writing; file pointer will be at beginning of file.
w Open the file for writing. If the file exists, it will be truncated to zero length; if the file doesn’t already exist, it will be created.
w+ Open the file for reading and writing. If the file exists, it will be truncated to zero length; if the file doesn’t already exist, it will be created. The file pointer starts at the beginning of the file.
a Open the file for writing. If the file exists, the file pointer will be at the end of the file; if the file does not exist, it is created.
a+ Open the file for reading and writing. If the file exists, the file pointer will be at the end of the file; if the file does not exist, it is created.
x Create and open file for writing only; place the file pointer at the beginning of the file.
x+ Create and open file for reading and writing.
c Open the file for writing only. If the file does not exist, it is created. If it exists, it is not truncated (as is the case with w), nor does the call to this function fail (as is the case with x). The file pointer is positioned at the beginning of the file.
c+ Open the file for reading and writing.

If include is specified and is true, fopen() tries to locate the file in the current include path.

If any error occurs while attempting to open the file, false is returned.

get_html_translation_table

array get_html_translation_table([int which[, int style[, string encoding]]])

Returns the translation table used by either htmlspecialchars() or htmlentities(). If which is HTML_ENTITIES, the table used by htmlentities() is returned; if which is HTML_SPECIALCHARS, the table used by htmlspecialchars() is returned. Optionally, you can specify which quotes style you want returned; the possible values are the same as those in the translation functions:

ENT_COMPAT (default) Converts double quotes, but not single quotes
ENT_NOQUOTES Does not convert either double quotes or single quotes
ENT_QUOTES Converts both single and double quotes
ENT_HTML401 Table for HTML 4.01 entities
ENT_XML1 Table for XML 1 entities
ENT_XHTML Table for XHTML entities
ENT_HTML5 Table for HTML 5 entities

The encoding optional parameter has the following possible selections:

ISO-8859-1 Western European, Latin-1.
ISO-8859-5 Cyrillic charset (Latin/Cyrillic), rarely used.
ISO-8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1.
UTF-8 ASCII compatible multibyte 8-bit Unicode.
cp866 DOS-specific Cyrillic charset.
cp1251 Windows-specific Cyrillic charset.
cp1252 Windows-specific charset for Western European.
KOI8-R Russian.
BIG5 Traditional Chinese, mainly used in Taiwan.
GB2312 Simplified Chinese, national standard character set.
BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JIS Japanese.
EUC-JP Japanese.
MacRoman Charset that was used by macOS.
"" An empty string activates detection from script encoding (Zend multibyte), default_charset, and current locale, in this order. Not recommended.

htmlentities

string htmlentities(string string[, int style[, string encoding [, bool double_encode]]])

Converts all characters in string that have special meaning in HTML and returns the resulting string. All entities defined in the HTML standard are converted. If supplied, style determines the manner in which quotes are translated. The possible values for style are:

ENT_COMPAT (default) Converts double quotes, but not single quotes
ENT_NOQUOTES Does not convert either double quotes or single quotes
ENT_QUOTES Converts both single and double quotes
ENT_SUBSTITUTE Replace invalid code unit sequences with a Unicode Replacement Character
ENT_DISALLOWED Replace invalid code points for the given document type with a Unicode Replacement Character
ENT_HTML401 Handle code as HTML 4.01
ENT_XML1 Handle code as XML 1
ENT_XHTML Handle code as XHTML
ENT_HTML5 Handle code as HTML 5

If supplied, encoding determines the final encoding for the characters. The possible values for encoding are:

ISO-8859-1 Western European, Latin-1
ISO-8859-5 Cyrillic charset (Latin/Cyrillic), rarely used
ISO-8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1.
UTF-8 ASCII-compatible multi-byte 8-bit Unicode
cp866 DOS-specific Cyrillic charset
cp1251 Windows-specific Cyrillic charset
cp1252 Windows-specific charset for Western European
KOI8-R Russian
BIG5 Traditional Chinese, mainly used in Taiwan
GB2312 Simplified Chinese, national standard character set
BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese
Shift_JIS Japanese
EUC-JP Japanese
MacRoman Charset that was used by Mac OS
"" An empty string activates detection from script encoding (Zend multibyte), default_charset, and current locale, in this order. Not recommended.

localeconv

array localeconv()

Returns an associative array of information about the current locale’s numeric and monetary formatting. The array contains the following elements:

decimal_point Decimal-point character
thousands_sep Separator character for thousands
grouping Array of numeric groupings; indicates where the number should be separated using the thousands separator character
int_curr_symbol International currency symbol (e.g., USD)
currency_symbol Local currency symbol (e.g., $)
mon_decimal_point Decimal-point character for monetary values
mon_thousands_sep Separator character for thousands in monetary values
positive_sign Sign for positive values
negative_sign Sign for negative values
int_frac_digits International fractional digits
frac_digits Local fractional digits
p_cs_precedes true if the local currency symbol precedes a positive value; false if it follows the value
p_sep_by_space true if a space separates the local currency symbol from a positive value
p_sign_posn 0 if parentheses surround the value and currency symbol for positive values, 1 if the sign precedes the currency symbol and value, 2 if the sign follows the currency symbol and value, 3 if the sign precedes the currency symbol, and 4 if the sign follows the currency symbol
n_cs_precedes true if the local currency symbol precedes a negative value; false if it follows the value
n_sep_by_space true if a space separates the local currency symbol from a negative value
n_sign_posn 0 if parentheses surround the value and currency symbol for negative values, 1 if the sign precedes the currency symbol and value, 2 if the sign follows the currency symbol and value, 3 if the sign precedes the currency symbol, and 4 if the sign follows the currency symbol

printf

int printf(string format[, mixed arg1 ...])

Outputs a string created by using format and the given arguments. The arguments are placed into the string in various places denoted by special markers in the format string.

Each marker starts with a percent sign (%) and consists of the following elements, in order. Except for the type specifier, the specifiers are all optional. To include a percent sign in the string, use %%.

  1. An optional sign specifier that forces a sign (– or +) to be used on a number. By default, only the – sign is used on a number if it’s negative. Additionally, this specifier forces positive numbers to have the + sign attached.

  2. A padding specifier denoting the character to use to pad the results to the appropriate string size (given below). Either 0, a space, or any character prefixed with a single quote may be specified; padding with spaces is the default.

  3. An alignment specifier. By default, the string is padded to make it right-justified. To make it left-justified, specify a dash () here.

  4. The minimum number of characters this element should contain. If the result would be less than this number of characters, the preceding specifiers determine the behavior to pad to the appropriate width.

  5. For floating-point numbers, a precision specifier consisting of a period and a number; this dictates how many decimal digits will be displayed. For types other than float, this specifier is ignored.

  6. Finally, a type specifier. This specifier tells printf() what type of data is being handed to the function for this marker. There are eight possible types:

b The argument is an integer and is displayed as a binary number
c The argument is an integer and is displayed as the character with that value
d The argument is an integer and is displayed as a decimal number
f The argument is a float and is displayed as a floating-point number
o The argument is an integer and is displayed as an octal (base-8) number
s The argument is treated and displayed as a string
x The argument is an integer and is displayed as a hexadecimal (base-16) number; lowercase letters are used
X Same as x, except uppercase letters are used

strftime

string strftime(string format[, int timestamp])

Formats a time and date according to the format string provided in the first parameter and the current locale. If the second parameter is not specified, the current time and date is used. The following characters are recognized in the format string:

%a Name of the day of the week as a three-letter abbreviation (e.g., Mon)
%A Name of the day of the week (e.g., Monday)
%b Name of the month as a three-letter abbreviation (e.g., Aug)
%B Name of the month (e.g., August)
%c Date and time in the preferred format for the current locale
%C The last two digits of the century
%d Day of the month as two digits, including a leading zero if necessary (e.g., 01 through 31)
%D Same as %m/%d/%y
%e Day of the month as two digits, including a leading space if necessary (e.g., 1 through 31)
%h Same as %b
%H Hour in 24-hour format, including a leading zero if necessary (e.g., 00 through 23)
%I Hour in 12-hour format (e.g., 1 through 12)
%j Day of the year, including leading zeros as necessary (e.g., 001 through 366)
%m Month, including a leading zero if necessary (e.g., 01 through 12)
%M Minutes
%n The newline character (\n)
%p am or pm
%r Same as %I:%M:%S %p
%R Same as %H:%M:%S
%S Seconds
%t The tab character (\t)
%T Same as %H:%M:%S
%u Numeric day of the week, starting with 1 for Monday
%U Numeric week of the year, starting with the first Sunday
%V ISO 8601:1998 numeric week of the year—Week 1 starts on the Monday of the first week that has at least four days
%W Numeric week of the year, starting with the first Monday
%w Numeric day of the week, starting with 0 for Sunday
%x The preferred date format for the current locale
%X The preferred time format for the current locale
%y Year with two digits (e.g., 98)
%Y Year with four digits (e.g., 1998)
%Z Time zone or name or abbreviation
%% The percent sign (%)

uasort

bool uasort(array array, callable function)

Sorts an array using a user-defined function, maintaining the keys for the values. See Chapter 5 and usort() for more information on using this function. Returns true if the array was successfully sorted, and false otherwise.

uksort

bool uksort(array array, callable function)

Sorts an array by keys using a user-defined function, maintaining the keys for the values. See Chapter 5 and usort() for more information on using this function. Returns true if the array was successfully sorted, and false otherwise.