The most used built-in functions in php
PHP常用
Variables
- $_COOKIE - $HTTP_COOKIE_VARS [deprecated] — HTTP Cookies
- $_ENV - $HTTP_ENV_VARS [deprecated] — Environment variables
- $_SERVER - $HTTP_SERVER_VARS [removed] — Server and execution environment information
- $GLOBALS — References all variables available in global scope
- $_SESSION - $HTTP_SESSION_VARS [deprecated] — Session variables
- $_FILES - $HTTP_POST_FILES [deprecated] — HTTP File Upload variables
- $argc — The number of arguments passed to script
- $argv — Array of arguments passed to script
Function
- explode() — Split a string by string
- implode() - Join array elements with a string
- trim() - Strip whitespace (or other characters) from the beginning and end of a string
- basename() - Returns trailing name component of path
- dirname() - Returns a parent directory’s path
- set_time_limit() - Limits the maximum execution time
- time() - Return current Unix timestamp
- is_bool() - Finds out whether a variable is a boolean
- is_int() - Find whether the type of a variable is integer
- is_float() - Finds whether the type of a variable is float
- strtotime() - Parse about any English textual datetime description into a Unix timestamp
- fopen() - Opens file or URL
- fgets() - Gets line from file pointer
- feof() - Tests for end-of-file on a file pointer
- strlen() - Get string length
- str_replace() - Replace all occurrences of the search string with the replacement string
- isset() - Determine if a variable is set and is not NULL
- strpo() - Find the position of the first occurrence of a substring in a string
- DIRECTORY_SEPARATOR - windows(\ or /), unix(/)
- PATH_SEPARATOR - windows(;), unix(:)
- count() - Count all elements in an array,
or something in an object
- substr() - Return part of string,
string substr ( string $string , int $start [, int $length ] )
- memory_get_usage() - Returns the amount of memory allocated to PHP
- ceil() - Round fractions up,
ceil(3.1) => 4
ceil(-3.14) => -3
- floor() - Round fractions down
- round() - Rounds a float
round(3.4) => 3
round(3.5) => 4
- preg_match() - Perform a regular expression match
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
- preg_replace()
- php_strip_whitespace() - Return source with stripped comments and whitespace
- array_chunk() - Split an array into chunks
- array_column() - Return the values from a single column in the input array
- array_combine() - Creates an array by using one array for keys and another for its values
- array_count_values() — Counts all the values of an array
- array_keys() — Return all the keys or a subset of the keys of an array
- array_map() — Applies the callback to the elements of the given arrays
- array_shift() — Shift an element off the beginning of array
- array_pop() — Pop the element off the end of array
- array_intersect() — Computes the intersection of arrays
- array_key_exists() — Checks if the given key or index exists in the array
- array_unique() — Removes duplicate values from an array
- array_unshift() — Prepend one or more elements to the beginning of an array
- array_values() — Return all the values of an array
- zlib:// – bzip2:// – zip:// — Compression Streams
$_SERVER
1 | Just a PHP file to put on your local server (as I don't have enough memory) <?php $indicesServer = array('PHP_SELF', 'argv', 'argc', 'GATEWAY_INTERFACE', 'SERVER_ADDR', 'SERVER_NAME', 'SERVER_SOFTWARE', 'SERVER_PROTOCOL', 'REQUEST_METHOD', 'REQUEST_TIME', 'REQUEST_TIME_FLOAT', 'QUERY_STRING', 'DOCUMENT_ROOT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_ENCODING', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_CONNECTION', 'HTTP_HOST', 'HTTP_REFERER', 'HTTP_USER_AGENT', 'HTTPS', 'REMOTE_ADDR', 'REMOTE_HOST', 'REMOTE_PORT', 'REMOTE_USER', 'REDIRECT_REMOTE_USER', 'SCRIPT_FILENAME', 'SERVER_ADMIN', 'SERVER_PORT', 'SERVER_SIGNATURE', 'PATH_TRANSLATED', 'SCRIPT_NAME', 'REQUEST_URI', 'PHP_AUTH_DIGEST', 'PHP_AUTH_USER', 'PHP_AUTH_PW', 'AUTH_TYPE', 'PATH_INFO', 'ORIG_PATH_INFO') ; echo '<table cellpadding="10">' ; foreach ($indicesServer as $arg) { if (isset($_SERVER[$arg])) { echo '<tr><td>'.$arg.'</td><td>' . $_SERVER[$arg] . '</td></tr>' ; } else { echo '<tr><td>'.$arg.'</td><td>-</td></tr>' ; } } echo '</table>' ; /* That will give you the result of each variable like (if the file is server_indices.php at the root and Apache Web directory is in E:\web) : PHP_SELF /server_indices.php argv - argc - GATEWAY_INTERFACE CGI/1.1 SERVER_ADDR 127.0.0.1 SERVER_NAME localhost SERVER_SOFTWARE Apache/2.2.22 (Win64) PHP/5.3.13 SERVER_PROTOCOL HTTP/1.1 REQUEST_METHOD GET REQUEST_TIME 1361542579 REQUEST_TIME_FLOAT - QUERY_STRING DOCUMENT_ROOT E:/web/ HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8 HTTP_ACCEPT_CHARSET ISO-8859-1,utf-8;q=0.7,*;q=0.3 HTTP_ACCEPT_ENCODING gzip,deflate,sdch HTTP_ACCEPT_LANGUAGE fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 HTTP_CONNECTION keep-alive HTTP_HOST localhost HTTP_REFERER http://localhost/ HTTP_USER_AGENT Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 HTTPS - REMOTE_ADDR 127.0.0.1 REMOTE_HOST - REMOTE_PORT 65037 REMOTE_USER - REDIRECT_REMOTE_USER - SCRIPT_FILENAME E:/web/server_indices.php SERVER_ADMIN myemail@personal.us SERVER_PORT 80 SERVER_SIGNATURE PATH_TRANSLATED - SCRIPT_NAME /server_indices.php REQUEST_URI /server_indices.php PHP_AUTH_DIGEST - PHP_AUTH_USER - PHP_AUTH_PW - AUTH_TYPE - PATH_INFO - ORIG_PATH_INFO - */ ?> |
fopen()
1 | fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) <?php $handle = fopen("/home/rasmus/file.txt", "r"); $handle = fopen("/home/rasmus/file.gif", "wb"); $handle = fopen("http://www.example.com/", "r"); $handle = fopen("ftp://user:password@example.com/somefile.txt", "w"); ?> |
mode | Description |
---|---|
‘r’ | Open for reading only; place the file pointer at the beginning of the file. |
‘r+’ | Open for reading and writing; place the file pointer at the beginning of the file. |
‘w’ | Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
‘w+’ | Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
‘a’ | Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended. |
‘a+’ | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() only affects the reading position, writes are always appended. |
‘x’ | Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. |
‘x+’ | Create and open for reading and writing; otherwise it has the same behavior as ‘x’. |
‘c’ | Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to ‘w’), nor the call to this function fails (as is the case with ‘x’). The file pointer is positioned on the beginning of the file. This may be useful if it’s desired to get an advisory lock (see flock()) before attempting to modify the file, as using ‘w’ could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested). |
‘c+’ | Open the file for reading and writing; otherwise it has the same behavior as ‘c’. |
###p hp_strip_whitespace()
usage:
1 | <?php // PHP comment here /* * Another PHP comment */ echo php_strip_whitespace(__FILE__); // Newlines are considered whitespace, and are removed too: do_nothing(); ?> ###### The output: <?php echo php_strip_whitespace(__FILE__); do_nothing(); ?> |
###array_map()1
<?php
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>
//output:
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
zip
1 | Example on how to read an entry from a ZIP archive (file "bar.txt" inside "./foo.zip"): <?php $fp = fopen('zip://./foo.zip#bar.txt', 'r'); if( $fp ){ while( !feof($fp) ){ echo fread($fp, 8192); } fclose($fp); } ?> Also, apparently, the "zip:" wrapper does not allow writing as of PHP/5.3.6. You can read http://php.net/ziparchive-getstream for further reference since the underlying code is probably the same. |
A few “magical” PHP constants
Name | Description |
---|---|
LINE | The current line number of the file. |
FILE | The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned. |
DIR | The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory. |
FUNCTION | The function name. |
CLASS | The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 CLASS works also in traits. When used in a trait method, CLASS is the name of the class the trait is used in. |
TRAIT | The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar). |
METHOD | The class method name. |
NAMESPACE | The name of the current namespace. |