Common PHP functions and constants

Source: Internet
Author: User
The complete path and FILE name of the common PHP system constant _ FILE. If it is used in a file to be included, the file name to be included is returned. Since PHP4.0.2, it always contains an absolute path (if it is a symbolic connection, it is the absolute path after resolution), and the previous version sometimes contains a relative path. The directory where the _ DIR _ file is located. If

The complete path and FILE name of the common PHP system constant _ FILE. If it is used in a file to be included, the file name to be included is returned. Since PHP4.0.2, it always contains an absolute path (if it is a symbolic connection, it is the absolute path after resolution), and the previous version sometimes contains a relative path. The directory where the _ DIR _ file is located. If

Common PHP system Constants

_ FILE __

The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Since PHP 4.0.2, it always contains an absolute path (if it is a symbolic connection, it is the absolute path after resolution), and the previous version sometimes contains a relative path.

_ DIR __

The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)

_ FUNCTION __

Function Name (New PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.

_ CLASS __

Class Name (New PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters.

_ METHOD __

Class Method Name (New PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).
Mathematical operation functions

Abs (x)

Returns the absolute value of x.

Ceil (x)

Rounds up to an integer closest to x.

Floor (x)

Round down to an integer nearest to x.

Mt_rand (min, max)

Returns a random number between min and max.

Intval (x)

Convert x to int type data

Floatval (x)

Convert x to float type data


String operation functions

Addcslashes-add backslash escape characters to some characters in the string

Chr (x)-returns the ASCII code of a character

Echo-used to display some content

Explode-convert a string into an array using a delimiter

Htmlspecialchars-convert some characters in a string to HTML entities

Implode-convert an array into a string using a specific delimiter

Ltrim-removes white spaces or specified characters on the left of the string

Rtrim-removes spaces or specified characters on the right of the string

Trim-removes spaces or specified characters on both sides of the string

Md5-encrypt a string using the MD5 Algorithm

Nl2br-replace the linefeed "\ n" in the string with"
"

Str_replace-match and replace strings

Strip_tags-Remove HTML and PHP code from a string

Stripos-locate and return the position of the first matching item. The matching is case insensitive.

Stristr-returns the part of a string through comparison. The comparison is case insensitive.

Strlen-get the encoding length of a string

Strpos-locate and return the position of the first matching item

Strrchr-returns the part of a string through comparison from the back to the back

Strripos-search from the back and return the position of the first matching item. The matching is case insensitive.

Strrpos-search from the back and return the position of the first matching item

Strstr-returns the part of a string through comparison

Strtolower-convert string to lowercase

Strtoupper-converts a string to uppercase

Substr-truncates a string

Ucfirst-converts the first letter of the given string into uppercase letters.

Ucwords-converts the first letter of each English word of the given string into uppercase letters.
Common Array Operations

Array-create an array

Array_keys-returns all the key names in the array.

Array_map-calls the callback function to the unit of the given array.

Array_merge-merge one or more Arrays

Array_pop-bring up the last unit of the array (Out stack)

Array_push-Push one or more units to the end of the array (into the stack)

Array_rand-randomly retrieve one or more units from the array

Array_shift-Removes elements starting with an array from the array

Array_unshift-insert one or more units at the beginning of the array

Array_slice-extract a segment from the array

Array_splice-removes a part of the array and replaces it with other values.

Count-calculates the number of units in the array or the number of attributes in the object.

Each-returns the current key/value pair in the array and moves the array pointer one step forward.

End-points the internal pointer of the array to the last unit.

In_array-check whether a value exists in the array

Array_key_exists-check whether the given key name or index exists in the array

Next-move the internal pointer in the array to a forward position.

Reset-point the internal pointer of the array to the first unit

Rsort-reverse sorting of Arrays

Shuffle-disrupt the Array

Sort-sort Arrays
Directory and file operation functions

I. File Type

1. filetype ()

Get the file type function. The parameter is a string (path + file name)

The returned value is a string, file (common file), dir (directory), or unknown (unknown file)

2. is_file () is_dir ()

Returns a Boolean value if it is of this type.

Ii. file attributes (the following function parameters are file names)

1. file_exists () check whether the file or directory exists

2. filesize (): Get the file size. If an error occurs, false is returned.

3. filectime () obtains the creation time.

4. filemtime () Get the modification time

5. fileatime () obtain the access time

6. stat () gets most of the file's attribute values

Iii. parsing directory

1. basename (path, [suffix]) returns the part of the file name in the path, and the second parameter is the extension (such as "php" or ". php "). If given, no extension is included in the returned value.

2. dirname (path) returns the directory name after removing the file name.

3. pathinfo () returns an associated array, including dirname (directory name), basename (Basic name), extension (extension)

4. traverse directories

1. Open the specified directory in opendir () and return the directory handle that can be used by other directory functions. False is returned for failure.

2. readdir () reads the specified directory. The parameter is the directory handle. Return a file name of the current directory pointer position and move the pointer back. If no more files exist, false is returned.

3. closedir () closes the specified directory. The parameter is the directory handle.

4. rewinddir () returns the directory handle. The parameter is the directory handle and the Directory pointer is reset to the start position.

5. Create and delete directories

1. Create a new directory using mkdir (). The parameter is the directory name.

2. rmdir (): delete a directory. The deleted directory can only be empty. If it is not empty, you must first enter the Directory and delete the files using the unlink () function.

6. Copy or move a directory

1. copy: php does not have a specific function. You must first create a directory mkdir () and then use the copy () function to copy each file.

2. Move: copy the file first, and then delete the original directory.

7. Open and Close a file

1. fopen (filename, mode [, use_include_path [, zcontext]) open the file. The parameter is the file name and file mode. The third parameter is optional, if it is set to 1, PHP will consider configuring the path specified in the directive include_path. The fourth parameter is optional. Set the allowed file name to start with the protocol name, such as http ://. Returns the file pointer. If the pointer fails, false is returned.

Summary:

R read-only

R + read/write

W write only (if the file exists, the original data will be deleted. If the file does not exist, the file will be created)

W + read/write (same as w)

X write (if the file exists, false is returned. If the file does not exist, it is created and only local)

X + read/write (same as x)

A write (the Pointer Points to the end of the file, and the file is created if the file does not exist)

A + write (same as)

B binary mode

T text mode

2. Close fclose ()

8. Operation File Content

1. fwrite (handle, string [, length]) writes a string. \ N \ r is the end character of the line. Returns the number of written characters. If the number of written characters fails, false is returned.

2. fread (handle, length) reads open files

3. file_get_contents () reads the file into a string

4. fgets (handle [, length]) returns a row

5. fgetc () Return Characters

6. file () reads a file into an array, with each action being an element.

7. readfile () reads a file and outputs it to the output buffer.

8. If feof () is used to determine whether the object has reached the end of the file, true is returned.

9. file_get_contents ()

10. File Locking Mechanism (preventing multiple users from simultaneously accessing the same file and causing file confusion)

1. flock (handle, operation [, & wouldblock]) file lock operation. Parameter 2: LOCK_SH shared lock, used for data reading; LOCK_EX exclusive lock, used for data writing; LOCK_UN released lock; LOCK_NB attaches a lock to prevent timed locking. Parameter 3: when it is set to 1, other processes are blocked during the lock.

10. Copy and delete files

1. copy (this source file, destination file)

2. unlink (target file) delete the file

11. File upload and download

1. Global Array $ _ FILES

$ _ FILES ["myfile"] ["name"] original name, including the extension

$ _ FILES ["myfile"] ["size"] size of the uploaded file, in bytes

$ _ FILES ["myfile"] ["tmp_name"] temporary file name after upload

$ _ FILES ["myfile"] ["error"]

0: successful;

1: The size exceeds the PHP configuration file limit;

2: The size exceeds the form limit;

3: incomplete file upload;

4: no files are uploaded.

$ _ FILES ["myfile"] ["type"] Get the MIME type of the uploaded file

2. is_uploaded_file () determines whether it was uploaded through HTTPPOST.

3. move_uploaded_file (): Move the uploaded file from the temporary location to the new location.

Date and Time Functions

Date

Usage: date (format, [time]);

If there is no time parameter, the current time is used. The format is a string, which is replaced with the number of seconds since the start time (January 1, January 1, 1970 ).

Time ()

Usage: time (); returns the number of seconds since, January 1, January 1, 1970.

Microtime ()

Usage: microtime (); returns a string, which is divided into two parts by space. The latter part is equivalent to time ()

The first part is the number of microseconds.

Set_time_limit

Usage: set_time_limit (seconds );

Indicates that the program must end in the specified number of seconds from the time of running the sentence. If the time-out occurs, the program exits after an error.

Mktime (hour, minute, second, month, day, year)

Returns the number of seconds for a given time.

Strtotime (string format of Time) returns the number of seconds of the given time
Regular Expression Functions

Preg_match (string $ pattern, string $ content [, array $ matches])

In the $ content string, search for the content that matches the regular expression given by $ pattern. If $ matches is provided, the matching result is put into it.

Preg_match_all (string $ pattern, string $ content [, array $ matches])

Similar to the preg_match () function. If the third parameter is used, all possible matching results are put. This function returns the number of times (which may be 0) of the entire pattern match. If an error occurs, False is returned.

Preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit])

Search for the pattern string $ pattern in $ string and replace the matched result with $ replacement. When $ pattern contains a mode unit (or sub-mode, the position in $ replacement, such as "\ 1" or "$1", will be replaced by the content matched by these submodes in sequence. "\ 0" or "$0" indicates the content of the entire matching string. It should be noted that the backslash in double quotation marks is used as an escape character, so it must be in the form of "\ 0" and "\ 1. Arrays can be used for the first three parameters. The fourth parameter $ limit can be used to set the number of replicas. By default, all replicas are replaced. Arrays can be used for the first three parameters. The fourth parameter $ limit can be used to set the number of replicas. By default, all replicas are replaced.

Preg_split (string $ pattern, string $ string [, int $ limit])

This function returns a string array. Each unit is $ string. The regular expression $ pattern is used as the substring separated by the boundary. If $ limit is set, the returned array can contain up to $ limit units.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.