Programming PHP

abs

int abs(int number) float abs(float number)

Returns the absolute value of number in the same type (float or integer) as the argument.

acos

double acos(double value)

Returns the arc cosine of value in radians.

addcslashes

string addcslashes(string string, string characters)

Escapes instances of characters in string by adding a backslash before them. You can specify ranges of characters by separating them by two periods; for example, to escape characters between a and q, use "a..q". Multiple characters and ranges can be specified in characters. The addcslashes( ) function is the inverse of stripcslashes( ).

addslashes

string addslashes(string string)

Escapes characters in string that have special meaning in SQL database queries. Single quotes (''), double quotes (""), backslashes (\), and the NUL-byte ("\0") are escaped. The stripslashes( ) function is the inverse for this function.

array

array array([mixed ...])

Creates an array using the parameters as elements in the array. By using the => operator, you can specify specific indexes for any elements; if no indexes are given, the elements are assigned indexes starting from 0 and incrementing by one. The internal pointer (see current, reset, and next) is set to the first element.

$array = array("first", 3 => "second", "third", "fourth" => 4);

Note: array is actually a language construct, used to denote literal arrays, but its usage is similar to that of a function, so it's included here.

array_count_values

array array_count_values(array array)

Returns an array whose elements' keys are the input array's values. The value of each key is the number of times that key appears in the input array as a value.

array_diff

array array_diff(array array1, array array2[, ... array arrayN])

Returns an array containing all of the values from the first array that are not present in any of the other arrays. The keys of the values are preserved.

array_filter

array array_filter(array array, mixed callback)

Creates an array containing all values from the original array for which the given callback function returns true. If the input array is an associative array, the keys are preserved. For example:

function isBig($inValue) { return($inValue > 10); } $array = array(7, 8, 9, 10, 11, 12, 13, 14); $new_array = array_filter($array, "isBig"); // contains (11, 12, 13, 14)

array_flip

array array_flip(array array)

Returns an array in which the elements' keys are the original array's values, and vice versa. If multiple values are found, the last one encountered is retained. If any of the values in the original array are any type except strings and integers, array_flip( ) returns false.

array_intersect

array array_intersect(array array1, array array2[, ... array arrayN])

Returns an array whose elements are those from the first array that also exist in every other array.

array_keys

array array_keys(array array[, mixed value])

Returns an array containing all of the keys in the given array. If the second parameter is provided, only keys whose values match value are returned in the array.

array_map

array array_map(mixed callback, array array1[, ... array arrayN])

Creates an array by applying the callback function referenced in the first parameter to the remaining parameters; the callback function should take as parameters a number of values equal to the number of arrays passed into array_map( ). For example:

function multiply($inOne, $inTwo) { return $inOne * $inTwo; } $first = (1, 2, 3, 4); $second = (10, 9, 8, 7); $array = array_map("multiply", $first, $second); // contains (10, 18, 24, 28)

array_merge

array array_merge(array array1, array array2[, ... array arrayN])

Returns an array created by appending the elements of every array to the previous. If any array has a value with the same string key, the last value encountered for the key is returned in the array; any elements with identical numeric keys are inserted into the resulting array.

array_merge_recursive

array array_merge_recursive(array array1, array array2[, ... array arrayN])

Like array_merge( ), creates and returns an array by appending each input array to the previous. Unlike that function, when multiple elements have the same string key, an array containing each value is inserted into the resulting array.

array_multisort

bool array_multisort(array array1[, SORT_ASC|SORT_DESC [, SORT_REGULAR|SORT_NUMERIC|SORT_STRING]] [, array array2[, SORT_ASC|SORT_DESC [, SORT_REGULAR|SORT_NUMERIC|SORT_STRING]], ...])

Used to sort several arrays simultaneously, or to sort a multidimensional array in one or more dimensions. The input arrays are treated as columns in a table to be sorted by rows the first array is the primary sort. Any values that compare the same according to that sort are sorted by the next input array, and so on.

The first argument is an array; following that, each argument may be an array or one of the following order flags (the order flags are used to change the default order of the sort):

SORT_ASC (default)

Sort in ascending order

SORT_DESC

Sort in descending order

After that, a sorting type from the following list can be specified:

SORT_REGULAR (default)

Compare items normally

SORT_NUMERIC

Compare items numerically

SORT_STRING

Compare items as strings

The sorting flags apply only to the immediately preceding array, and they revert to SORT_ASC and SORT_REGULAR before each new array argument.

This function returns true if the operation was successful and false if not.

array_pad

array array_pad(array input, int size[, mixed padding])

Returns a copy of the input array padded to the length specified by size. Any new elements added to the array have the value of the optional third value. You can add elements to the beginning of the array by specifying a negative size in this case, the new size of the array is the absolute value of the size.

If the array already has the specified number of elements or more, no padding takes place and an exact copy of the original array is returned.

array_pop

mixed array_pop(array stack)

Removes the last value from the given array and returns it. If the array is empty (or the argument is not an array), returns NULL.

array_push

int array_push(array array, mixed value1[, ... mixed valueN])

Adds the given values to the end of the array specified in the first argument and returns the new size of the array. Performs the same function as calling $array[] = $value for each of the values in the list.

array_rand

mixed array_rand(array array[, int count])

Picks a random element from the given array. The second, optional, parameter can be given to specify a number of elements to pick and return. If more than one element is returned, an array of keys is returned, rather than the element's value.

Before you call array_rand( ), be sure to seed the random-number generator using srand( ).

array_reduce

mixed array_reduce(array array, mixed callback[, int initial])

Returns a value derived by iteratively calling the given callback function with pairs of values from the array. If the third parameter is supplied, it, along with the first element in the array, is passed to the callback function for the initial call.

array_reverse

array array_reverse(array array[, bool preserve_keys])

Returns an array containing the same elements as the input array, but whose order is reversed. If the second parameter is given and is true, the keys for the elements are preserved; if not, the keys are lost.

array_search

mixed array_search(mixed value, array array[, bool strict])

Performs a search for a value in an array, as with in_array( ). If the value is found, the key of the matching element is returned; NULL is returned if the value is not found. If strict is specified and is true, a matched element is returned only when it is of the same type and value as value.

array_shift

mixed array_shift(array stack)

Similar to array_pop( ), but instead of removing and returning the last element in the array, it removes and returns the first element in the array. If the array is empty, or if the argument is not an array, returns NULL.

array_slice

array array_slice(array array, int offset[, int length])

Returns an array containing a set of elements pulled from the given array. If offset is a positive number, elements starting from that index onward are used; if offset is a negative number, elements starting that many elements from the end of the array are used. If the third argument is provided and is a positive number, that many elements are returned; if negative, the sequence stops that many elements from the end of the array. If the third argument is omitted, the sequence returned contains all elements from the offset to the end of the array.

array_splice

array array_splice(array array, int offset[, int length[, array replacement]])

Selects a sequence of elements using the same rules as array_slice( ), but instead of being returned, those elements are either removed or, if the fourth argument is provided, replaced with that array. An array containing the removed (or replaced) elements is returned.

array_sum

mixed array_sum(array array)

Returns the sum of every element in the array. If all of the values are integers, an integer is returned. If any of the values are doubles, a double is returned.

array_unique

array array_unique(array array)

Creates and returns an array containing each element in the given array. If any values are duplicated, the later values are ignored. Keys from the original array are preserved.

array_unshift

int array_unshift(array stack, mixed value1[, ... mixed valueN])

Returns a copy of the given array, with the additional arguments added to the front of the array; the added elements are added as a whole, so the elements as they appear in the array are in the same order as they appear in the argument list. Returns the number of elements in the new array.

array_values

array array_values(array array)

Returns an array containing all of the values from the input array. The keys for those values are not retained.

array_walk

int array_walk(array input, string callback[, mixed user_data])

Calls the named function for each element in the array. The function is called with the element's value, key, and optional user data as arguments. To ensure that the function works directly on the values of the array, define the first parameter of the function by reference.

arsort

void arsort(array array[, int flags])

Sorts an array in reverse order, maintaining the keys for the array values. The optional second parameter contains additional sorting flags. See Chapter 5 and sort for more information on using this function.

asin

double asin(double value)

Returns the arc sine of value in radians.

asort

void asort(array array[, int flags])

Sorts an array, maintaining the keys for the array values. The optional second parameter contains additional sorting flags. See Chapter 5 and sort for more information on using this function.

assert

int assert(string|bool assertion)

If assertion is true, generates a warning in executing the code. If assertion is a string, assert( ) evaluates that string as PHP code.

assert_options

mixed assert_options(int option[, mixed value])

If value is specified, sets the assert control option option to value and returns the previous setting. If value is not specified, returns the current value of option. The following values for option are allowed:

ASSERT_ACTIVE

Enable assertions.

ASSERT_WARNING

Have assertions generate warnings.

ASSERT_BAIL

Have execution of the script halt on an assertion.

ASSERT_QUIET_EVAL

Disable error reporting while evaluating assertion code given to the assert( ) function.

ASSERT_CALLBACK

Call the specified user function to handle an assertion. Assertion callbacks are called with three arguments: the file, the line, and the expression where the assertion failed.

atan

double atan(double value)

Returns the arc tangent of value in radians.

atan2

double atan2(double y, double x)

Using the signs of both parameters to determine the quadrant the value is in, returns the arc tangent of x and y in radians.

base64_decode

string base64_decode(string data)

Decodes data, which is base 64-encoded data, into a string (which may contain binary data). For more information on base-64 encoding, see RFC 2045.

base64_encode

string base64_encode(string data)

Returns a base 64-encoded version of data. MIME base-64 encoding is designed to allow binary or other 8-bit data to survive transition through protocols that may not be 8-bit safe, such as email messages.

base_convert

string base_convert(string number, int from, int to)

Converts number from one base to another. The base the number is currently in is from, and the base to convert to is to. The bases to convert from and to must be between 2 and 36. Digits in a base higher than 10 are represented with the letters a (10) through z (35). Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.

basename

string basename(string path[, string suffix])

Returns the filename component from the full path path. If the file's name ends in suffix, that string is removed from the name. For example:

$path = "/usr/local/httpd/index.html"; echo(basename($path)); // index.html echo(basename($path, '.html')); // index

bin2hex

string bin2hex(string binary)

Converts binary to a hexadecimal (base-16) value. Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.

bindec

int bindec(string binary)

Converts binary to a decimal value. Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.

call_user_func

mixed call_user_func(string function[, mixed parameter1[, ... mixed parameterN]])

Calls the function given in the first parameter. Additional parameters are used as parameters when calling the function. The comparison to check for a matching function is case-insensitive. Returns the value returned by the function.

call_user_func_array

mixed call_user_func_array(string function, array parameters)

Similar to call_user_func( ), this function calls the function named function with the parameters in the array parameters. The comparison to check for a matching function is case-insensitive. Returns the value returned by the function.

call_user_method

mixed call_user_method(string function, mixed object[, mixed parameter1 [, ... mixed parameterN]])

Calls the method given in the first parameter on the object in the second parameter. Additional parameters are used as parameters when calling the method. The comparison to check for a matching method name is case-insensitive. Returns the value returned by the function.

call_user_method_array

mixed call_user_method_array(string function, mixed object[, array parameters])

Similar to call_user_method( ), this function calls the method named by the first parameter on the object in the second parameter. If given, the third parameter is an array of values used as parameters for the call to the object method. The comparison to check for a matching method name is case-insensitive. Returns the value returned by the function.

ceil

double ceil(double number)

Returns the smallest integer value greater than or equal to number.

chdir

bool chdir(string path)

Sets the current working directory to path; returns true if the operation was successful and false if not.

checkdate

bool checkdate(int month, int day, int year)

Returns true if the month, date, and year as given in the parameters are valid, and false if not. A date is considered valid if the year falls between 1 and 32767 inclusive, the month is between 1 and 12 inclusive, and the day is within the number of days the specified month has.

checkdnsrr

int checkdnsrr(string host[, string type])

Searches DNS records for a host having the given type. Returns true if any records are found, and false if none are found. The host type can take any of the following values (if no value is specified, MX is the default):

A

IP address

MX (default)

Mail exchanger

NS

Name server

SOA

Start of authority

PTR

Pointer to information

CNAME

Canonical name

ANY

Any of the above

chgrp

bool chgrp(string path, mixed group)

Changes the group for the file path to group; PHP must have appropriate privileges for this function to work. Returns true if the change was successful and false if not.

chmod

bool chmod(string path, int mode)

Attempts to change the permissions of path to mode. mode is expected to be an octal number, such as 0755. An integer value such as 755 or a string value such as "u+x" will not work as expected. Returns true if the operation was successful and false if not.

chop

string chop(string string[, string characters])

This is an alias for ltrim( ).

chown

bool chown(string path, mixed user)

Changes ownership for the file path to the user named user. PHP must have appropriate privileges (generally, root for this function) for the function to operate. Returns true if the change was successful and false if not.

chr

string chr(int char)

Returns a string consisting of the single ASCII character char.

chroot

bool chroot(string path)

Changes the root directory of the current process to path. You cannot use chroot( ) to restore the root directory to / when running PHP in a web server environment. Returns true if the change was successful and false if not.

chunk_split

string chunk_split(string string[, int size[, string postfix]])

Inserts postfix into string every size characters and at the end of the string; returns the resulting string. If not specified, postfix defaults to \r\n and size defaults to 76. This function is most useful for encoding data to the RPF 2045 standard. For example:

$data = "...some long data..."; $converted = chunk_split(base64_encode($data));

class_exists

bool class_exists(string name)

Returns true if a class with the same name as the string has been defined; if not, it returns false. The comparison for class names is case-insensitive.

clearstatcache

void clearstatcache( )

Clears the file status functions cache. The next call to any of the file status functions will retrieve the information from the disk.

closedir

void closedir([int handle])

Closes the directory stream referenced by handle. See opendir for more information on directory streams. If handle is not specified, the most recently opened directory stream is closed.

closelog

int closelog( )

Closes the file descriptor used to write to the system logger after an openlog( ) call; returns true.

compact

array compact(mixed variable1[, ... mixed variableN])

Creates an array by retrieving the values of the variables named in the parameters. If any of the parameters are arrays, the values of variables named in the arrays are also retrieved. The array returned is an associative array, with the keys being the arguments provided to the function and the values being the values of the named variables. This function is the opposite of extract( ).

convert_cyr_string

string convert_cyr_string(string value, string from, string to)

Converts value from one Cyrillic set to another. The from and to parameters are single-character strings representing the set and have the following valid values:

k

koi8-r

w

Windows-1251

i

ISO 8859-5

a or d

x-cp866

m

x-mac-cyrillic

copy

int copy(string path, string destination)

Copies the file at path to destination. If the operation succeeds, the function returns true; otherwise, it returns false.

cos

double cos(double value)

Returns the cosine of value in radians.

count

int count(mixed value)

Returns the number of elements in the value; for arrays, this is the number of elements in the array; for any other value, this is 1. If the parameter is a variable and the variable is not set, 0 is returned.

count_chars

mixed count_chars(string string[, int mode])

Returns the number of occurrences of each byte value from 0-255 in string; mode determines the form of the result. The possible values of mode are:

0 (default)

Returns an associative array with each byte-value as a key and the frequency of that byte-value as the value

1

Same as above, except that only byte-values with a nonzero frequency are listed

2

Same as above, except that only byte-values with a frequency of zero are listed

3

Returns a string containing all byte-values with a nonzero frequency

4

Returns a string containing all byte-values with a frequency of zero

crc32

int crc32(string value)

Calculates and returns the cyclic redundancy checksum (CRC) for value.

create_function

string create_function(string arguments, string code)

Creates an anonymous function with the given arguments and code; returns a generated name for the function. Such anonymous functions (also called lambda functions) are useful for short-term callback functions, such as when using usort( ).

crypt

string crypt(string string[, string salt])

Encrypts string using the DES encryption algorithm seeded with the two-character salt value salt. If salt is not supplied, a random salt value is generated the first time crypt( ) is called in a script; this value is used on subsequent calls to crypt( ). Returns the encrypted string.

current

mixed current(array array)

Returns the value of the element to which the internal pointer is set. The first time current( ) is called, or when current( ) is called after reset, the pointer is set to the first element in the array.

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 Savings 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

Timezone 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 "1" 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.

decbin

string decbin(int decimal)

Converts decimal to a binary representation of it. Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.

dechex

string dechex(int decimal)

Converts decimal to a hexadecimal (base-16) representation of it. Up to a 32-bit number, or 2,147,483,647 decimal (0x7FFFFFFF hexadecimal), can be converted.

decoct

string decoct(int decimal)

Converts decimal to an octal (base-8) representation of it. Up to a 32-bit number, or 2,147,483,647 decimal (017777777777 octal), can be converted.

define_syslog_variables

void define_syslog_variables( )

Initializes all variables and constants used by the syslog functions openlog( ), syslog( ), and closelog( ). This function should be called before using any of the syslog functions.

deg2rad

double deg2rad(double number)

Converts number from degrees to radians and returns the result.

dirname

string dirname(string path)

Returns the directory component of path. This includes everything up to the filename portion (see basename) and doesn't include the trailing path separator.

disk_free_space

double disk_free_space(string path)

Returns the number of bytes of free space available on the disk partition or filesystem at path.

disk_total_space

double disk_total_space(string path)

Returns the number of bytes of total space available (including both used and free) on the disk partition or filesystem at path.

dl

int dl(string filename)

Dynamically loads the PHP extension given in filename.

doubleval

double doubleval(mixed value)

Returns the floating-point value for value. If value is a nonscalar value (object or array), the function returns 0.

each

array each(array array)

Creates an array containing the keys and values of the element currently pointed at by the array's internal pointer. The array contains four elements: elements with the keys 0 and key from the element contain the key of the element, and elements with the keys 1 and value contain the value of the element.

If the internal pointer of the array points beyond the end of the array, each( ) returns false.

echo

void echo string string[, string string2[, string stringN ...]]

Outputs the given strings. echo is a language construct, and enclosing the parameters in parentheses is optional, unless multiple parameters are given in this case, you cannot use parentheses.

empty

bool empty(mixed value)

Returns true if value is either 0 or not set, and false otherwise.

end

mixed end(array array)

Advances the array's internal pointer to the last element and returns the element's value.

ereg

int ereg(string pattern,string string[, array matches])

Searches string for the regular expression pattern. If given, the array matches is filled with the subpattern matches. Returns true if the pattern matched in string and false if not. See Chapter 4 for more information on using regular expressions.

ereg_replace

string ereg_replace(string pattern,string replace, string string)

Searches for all occurrences of the regular expression pattern in string, replaces them with replace, and returns the result.

eregi

int eregi(string pattern,string string[, array matches])

Searches string for the regular expression pattern (the pattern matching is case-insensitive). If given, the array matches is filled with the subpattern matches. Returns true if the pattern matched in string and false if not. See Chapter 4 for more information on using regular expressions. This is a case-insensitive version of ereg( ).

eregi_replace

string ereg_replace(string pattern, string replace, string string)

Searches for all occurrences of the regular expression pattern in string, replaces them with replace, and returns the result. The pattern matching is case-insensitive. This is a case-insensitive version of ereg_replace( ).

error_log

int error_log(string message, int type[, string destination[, string headers]])

Records an error message to the web server's error log, to an email address, or to a file. The first parameter is the message to log. The type is one of the following:

0

message is sent to the PHP system log; the message is put into the file pointed at by the error_log configuration directive.

1

message is sent to the email address destination. If specified, headers provides optional headers to use when creating the message (see mail for more information on the optional headers).

3

Appends message to the file destination.

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

Runtime warnings

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_ALL

All of the above options

Any number of these options can be ORed 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, then restores the original level:

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

escapeshellarg

string escapeshellarg(string argument)

Properly escapes argument so it can be used as a safe argument to a shell function. When directly passing user input (such as from forms) to a shell command, you should use this function to escape the data to ensure that the argument isn't a security risk.

escapeshellcmd

string escapeshellcmd(string command)

Escapes any characters in command that could cause a shell command to run additional commands. When directly passing user input (such as from forms) to the exec( ) or system( ) functions, you should use this function to escape the data to ensure that the argument isn't a security risk.

exec

string exec(string command[, array output[, int return]])

Executes command via the shell and returns the last line of output from the command's result. If output is specified, it is filled with the lines returned by the command. If return is specified, it is set to the return status of the command.

If you want to have the results of the command output into the PHP page, use passthru( ).

exp

double exp(double number)

Returns e raised to the number power.

explode

array explode(string separator, string string[, int limit])

Returns an array of substrings created by splitting string wherever separator is found. If supplied, a maximum of limit substrings will be returned, with the last substring returned containing the remainder of the string. If separator is not found, returns the original string.

extension_loaded

bool extension_loaded(string name)

Returns true if the named extension is loaded or false if it is not.

extract

int extract(array array[, int type[, string prefix]])

Sets the value of variables to the values of elements from an array. For each element in the array, the key is used to determine the variable name to set, and that variable is set to the value of the element.

The second argument, if given, takes one of the following values to determine behavior if the values in the array have the same name as variables already existing in the local scope:

EXTR_OVERWRITE (default)

Overwrite the existing variable

EXTR_SKIP

Don't overwrite the existing variable (ignore the value provided in the array)

EXTR_PREFIX_SAME

Prefix the variable name with the string given as the third argument

EXTR_PREFIX_ALL

Prefix all variable names with the string given as the third argument

EXTR_PREFIX_INVALID

Prefix any invalid or numeric variable names with the string given as the third argument

The function returns the number of successfully set variables.

Категории