PHP feast-collection of common functions

Source: Internet
Author: User

PHP feast-collection of common functions

I recently wrote a lot of PHP and many common functions. Most of them took notes and posted a blog to learn from them. As a matter of fact, I feel that I have learned a language. The syntax logic is soft, and the degree of familiarity with the language can only grow with the increase of time, when you have a deep understanding of the functions, libraries, and features of a language, you can barely be proficient or proficient.

1. trim (): removes spaces and other pre-defined characters from both ends of the string. You can also delete the specified characters.

Similar to ltrim () and rtrim ().

2. _ CLASS __, this constant returns the name when the CLASS is defined.

3. strtotime () describes the date and time of any English text as a unix timestamp. For example, strtotime ('yesterday') returns the unix timestamp of yesterday.

It is often used in combination with date (). For example, date ('Y-m-d H: I: s', strtotime ('yesterday') returns the year, month, day, hour, minute, and second of yesterday.

4. intval (), which converts any type variable outside the array or class to the integer type.

Similar to floatval.

5. explode (), which splits the string into an Array Based on the specified characters. It is 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, you can directly put it into a text file, or you can output it to a csv file by commas, you can open it in excel.

7. PHP_EOL. This constant indicates a linefeed. For example, it is useful to use trim. PHP has many similar constants to indicate the corresponding symbols.

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

The corresponding ceil () is rounded up and floor () is rounded down. Of course, you can also use intval () to take only integer ~.

9. str_replace (), a useful function. Find and replace the specified character with the specified character from the specified string.

10. int_set (): You can input two string variable parameters so that one of the variables remains the specified value when the script runs and the original value is restored when the script ends.

11. system (): Execute an external program and return the result. For example, execute a shell command.

12. Open a directory handle using opendir (). You can add @ in front of the function to hide the error output.

There are still many file operation functions, such as chdir () to change the current directory to the specified directory. File_exists () is used to determine whether the specified file exists. unlink () is used to delete the specified file.

13. array_shift (): Delete the first element in the array and return 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 () combines two or more numbers into an array, array_diff_key () returns an array containing all the key values in the compared array that are not in any other array. array_unique () removes the repeated values in the array and returns the Array (which can be used to remove repeated elements ), is_array () determines whether the variable is an array (it is useful because PHP is a weak language). array_slice () removes a value from the Array Based on the conditions, array_values () returns an array of all key values in a given array without retaining the key name.

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

15. unset (): Release the specified variable, for example, clear the value of the specified array.

16. strpos (), returns the position where the string first appears in another string.

17. number_format (). The first parameter is the decimal number, the second parameter is the decimal number, the third parameter is the decimal number, and the fourth parameter is the decimal number.

18. implode (): combines the array into a string based on the specified delimiter, corresponding to explode ().

19. file (): Read the entire file into an array. Each cell in the array is a corresponding row in the file.

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

21. var_dump (): prints information about the variable.

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

22. constant (), returns the constant value.

23. feof (), check whether the file has reached the end.

24. fgets (), read a row from the file pointer.

25. strcasecmp (), compares two strings.

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

27. stripslashes (): Delete the backslash from the string.

28. ucwords (): converts the first letter of each word in a string to uppercase.

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

30. get_class (), the Class Name of the returned object.

31. extract (): imports the variable from the array to 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 specified php script files. The difference is that different methods are used to handle errors. include () generates a warning and continues to execute. require () it will cause a fatal error and end the script.

33. Common 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 name of the table where the specified field is located.

The field name of the specified field in the result obtained by mysql_field_name ()

Mysql_fetch_row () returns a row from the result set as a numeric Array


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.