Home > Backend Development > PHP Tutorial > PHP Feast - Collection of Commonly Used Functions_PHP Tutorial

PHP Feast - Collection of Commonly Used Functions_PHP Tutorial

WBOY
Release: 2016-07-13 10:19:46
Original
730 people have browsed it

PHP Feast - Collection of Commonly Used Functions

I have written a lot of PHP recently, and I have been exposed to a lot of commonly used functions. Most of them have taken notes and posted them on a blog to learn together. In fact, I feel that when learning a language, grammatical logic is a soft quality, and the familiarity with the language can only grow slowly as the time of use increases. Only when you have a deep understanding of the functions, libraries, and features of a language can you It can barely be called proficient or proficient.

1. trim(), removes blank characters and other predefined characters from both ends of the string, and of course can delete specified characters.

Similar ones include ltrim() and rtrim().

2. __CLASS__, this constant returns the name of the class when it was defined.

3. strtotime(), describes the date and time of any English text as a unix timestamp, such as strtotime(‘yesterday’) returns yesterday’s unix timestamp.

Often used in conjunction with date(), functions, such as date('Y-m-d H:i:s', strtotime('yesterday')) returns yesterday's year, month, day, hour, minute and second.

4. intval(), convert any type variable other than array or class to integer type.

Similar ones include floatval(), etc.

5. explode(), split the string into arrays according to specified characters, very useful! !

6. file_get_contents(), read the entire file into a string.

The corresponding file_put_contents() can put a string into a file, directly into a text file, or split by commas and output to a csv file, which can be opened with excel.

7. PHP_EOL, this constant represents the newline character. For example, it is very useful to use it in conjunction with trim(). PHP has many similar constants representing corresponding symbols.

8. round(), you can retain the number of digits after the decimal point according to the specified number of digits, round and save.

Correspondingly, ceil() rounds up and floor() rounds down. Of course, you can also use the mentioned intval() to directly round only integers~.

9. str_replace(), a very useful function, finds the specified character from the specified string and replaces it with the specified character.

10. int_set(), you can pass in two string variable parameters, so that one of the variables maintains the specified value when the script is running, and restores the original value when the script ends.

11. system(), executes an external program and returns the result. For example, execute a shell command.

12. opendir(), opens a directory handle, you can add @ before the function to hide the error output.

There are many corresponding file operation functions, such as chdir() to change the current directory to the specified directory. file_exists() determines whether the file exists, and unlink() is used to delete the specified file.

13. array_shift(), deletes the first element in the array and returns the value of the deleted element.

There are many corresponding array operation functions, such as array_flip() to flip the key and value of the array, array_merge() to merge two or more arrays into one array, and array_diff_key() to return an array containing all the keys being compared. Key values ​​in the array that are not in any other arrays. array_unique() removes duplicate values ​​in the array and returns the array (can be used to remove duplicate elements). is_array() determines whether the variable is an array (since PHP is a weakly typed language, so Very useful), array_slice() removes a segment of values ​​from an array based on conditions, array_values() returns an array of all key values ​​in a given array without retaining key names.

14. count(), very intuitive, is used to return the number of elements in the array.

15. unset(), releases the specified variable, such as clearing the value of the specified array.

16. strpos(), returns the position of the first occurrence of a string in another string.

17. number_format(), the first parameter is the decimal, the second parameter is the number of decimals, the third parameter is the symbol to divide the decimal, and the fourth parameter is the symbol to divide every thousand digits.

18. implode(), combine the arrays into a string according to the specified delimiter, corresponding to explode().

19. file(), reads the entire file into an array, and each unit in the array is the corresponding line in the file.

20. readdir(), returns the entries in the directory handle opened by opendir().

21. var_dump(), prints relevant information about variables.

Similarly there is print_r(), but the var_dump() function is more detailed.

22. constant(), returns the value of the constant.

23. feof(), detects whether the end of the file has been reached.

24. fgets(), reads a line from the file pointer.

25. strcasecmp(), compares two strings.

26. chr(), returns characters from the specified ASCII code.

27. stripslashes(), remove backslashes in strings.

28. ucwords(), changes the first letter of each word in the string to uppercase.

29. method_exists(), check whether the method of the class exists.

30. get_class(), returns the class name of the object.

31. extract(), import variables from the array into the current symbol table, the key name is used for the variable name, and the key value is used for the variable value.

32. Both require() and include() can reference the specified php script file. The difference lies in the way of handling errors. include() will generate a warning and continue execution; while require() will cause a fatal error. , end the script.

33. Commonly used mysql operation functions

mysql_connect(), mysql_select_db(), mysql_close(), mysql_query(), mysql_num_rows(), mysql_num_rows(), mysql_free_result(), mysql_error()

mysql_num_fields() returns the number of fields in the result set

mysql_field_table() returns the table name where the specified field is located

mysql_field_name() gets the field name of the specified field in the result

mysql_fetch_row() returns a row from the result set as a numeric array


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871187.htmlTechArticlePHP Feast - Collection of Commonly Used Functions I have written a lot of PHP recently, and I have come across a lot of commonly used functions, most of them We all took notes, posted them on a blog, and studied together. In fact, it feels like learning a language...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template