


PHP Feast - Collection of Commonly Used Functions_PHP Tutorial
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
