Summary of common PHP functions (recommended)

Source: Internet
Author: User
1. write files 1. open the resource (file) fopen ($ filename, $ mode) 2. write file fwrite ($ handle, $ str) 3. close the file fclose ($ handle) 4. write file_put_contents ($ filename, $ str, $ mode) FILE _...

I. Writing files

1. open the resource (file) fopen ($ filename, $ mode)
2. write the file fwrite ($ handle, $ str)
3. close the file fclose ($ handle)
4. write file_put_contents ($ filename, $ str, $ mode) FILE_APPEND LOCK_EX in one step
}

II. reading files
1. read the file fread ($ handle, number of bytes)
2. read a row fgets ($ handle );
3. read a character fgetc ($ handle)
4. read the file ($ filename) into an array)
5. read file_get_contents ($ filename) in one step)

III. Directory operations
1. create the directory mkdir ($ dirname)
2. delete the rmdir Directory ($ dirname)
3. open the directory handle opendir ($ dirname)
4. read the number of directory lines readdir ($ handle)
5. close the directory resource closedir ($ handle)
6. reset the directory resource rewinddir ($ dirname );

IV. Directory and file operations
1. check whether file_exists ($ filename) exists in the file or directory)
2. rename a file or directory ($ file)

V. file operations
1. copy a file ('original file', 'destination file ')
2. delete the file unlink ($ filename)
3 get the file size filesize ($ filename)
4. obtain the file creation Time: filectime ($ filename)
5. obtain the file access time fileatime ($ filename)
6. get the file modification time filemtime ($ filename)

VI. path operation 1 obtain the path dirname ($ path)
2. get the file name basename ($ path)
3. obtain the path information pathinfo ($ path)

VII. array functions (extremely important)

1. Insert an element array_unshift ($ arr, $ v) at the beginning of the array)
2. add the array element array_push ($ arr, $ v, $ v1. ..) to the end of the array ...)
3. remove the first element of the array and return the array_shift ($ arr) element)
4. delete the element array_pop ($ arr) at the end of the array)
5. connect the array with $ separator into a string implode ($ a, $ arr)
6. check whether the variable is an array is_array ($ arr)
7. obtain the array key name array_keys ($ arr)
8. obtain the array value array_values ($ arr)
9. retrieve $ value in $ arr and return the boolean value in_array ($ v, $ arr)
10. search for the key name array_key_exists ($ k, $ arr) in the array $ arr)
11. check whether $ value is in $ arr. if the return key name Array_search ($ value, $ arr) exists)
12. sort an array in reverse order. if the second parameter is true, keep the key name Array_reverse ($ arr, true)
13. swap the key and value of the array Array_flip ($ arr)
14. Count ($ arr) the number of array elements)
15. count the occurrences of all values in the array. Array_count_values ($ arr)
16. remove the repeated value Array_unique ($ arr) from the array)
17. Sort values from small to large Sort ($ arr)
18. values are sorted from large to small Rsort ($ arr)
19. Sort keys in ascending order of ksort ($ arr)
20. Sort keys in ascending order of krsort ($ arr)
21. randomly retrieve $ num elements from the array Array_rand ($ arr, $ num)
22. sum all elements of the array, Array_sum ($ arr)
23. Merge array array_merge ($ arr, $ arr)



8. string functions (extremely important)

1. output string echo ($ str) echo
2. original output (separate single quotes and double quotes) print ($ str)
3. output the string and end the script to run Die ($ str): die ($ str) die;
4. output the string and end the script to execute exit ($ str) exit;
5. output the formatted string printf ($ str, $ p1 ,...)
6. do not directly output formatted strings. return formatted strings and save them to the variables sprintf ($ str, $ p1 ,...)
7. print the variable information var_dump ($ p)
8. convert string to lower case strtolower ($ str)
9. convert string to uppercase strtoupper ($ str)
10. convert the first character of the string to uppercase ucfirst ($ str)
11. convert each word in the string to uppercase ucwords ($ str)
12. remove white spaces at both ends of the string. Trim ($ str ,',')
13. remove the white spaces on the left of the string. Ltrim ($ str)
14. remove the white spaces on the right of the string. Rtrim ($ str)
Blank characters: "", "\ t", "\ n", "\ r", "\ 0"
15 Get the string length strlen ($ str)
16 count the number of contained strings substr_count ($ str, 'substring ')
17. returns a substring starting from $ start in the string $ string with a length of $ length.
Substr ($ string, $ start [, $ length])
18. in the string $ string, $ search returns the substring that ends with the string for the first time.
Strstr ($ string, $ search)
19. search for the first position of $ search in $ str, starting from $ offset.
Strpos ($ str, $ search [, int $ offset])
20. find the last position of $ search in $ str, starting from $ offset.
Strrpos ($ str, $ search [, int $ offset])
21. replace all $ search in $ str with $ replace.
Str_replace ($ search, $ replace, $ str)
22. repeated output of the specified string
Str_repeat ()
23. encrypted string
Md5 ()
24. string flip
Strrev ()
25. use one string to separate another string to form an array // convert the string into an array
Explode ("separator", $ str );

The above is a summary of common PHP functions (recommended). For more information, see other related articles in the first PHP community!

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.