To facilitate ide management and code prompting, we start with fun _ when naming all functions.
The code is as follows:
/**
* @ Author Yuans
* @ Copyright php.com
* @ Package: common usage and features of the function.
*/
# Notes for writing basic functions.
// To facilitate ide management and code prompting, we start with fun _ when naming all functions.
Function fun_cutstr ($ str, $ str_width = 0, $ str_pad = '...'){
// Every function has to consider some exceptions, such as incorrect function introduction, 0, or false.
// The characters after the truncated characters are expected to be returned from the outside, so even if this function does not work, the incoming values should be returned.
If (empty ($ str) === true | empty ($ str_width) === true)
Return $ str;
// Filter parameters
$ Str_width + = 0;
// Maintain a principle and try not to pollute the original parameters,
$ Return_str = mb_strcut ($ str, 0, $ str_width, 'utf-8 ');
// Strengthen the judgment. if return_str cannot have a value, many servers cannot execute it because it is a mb function.
If (empty ($ return_str) === false ){
Return $ return_str. $ str_pad;
} Else {
Return $ str;
}
}
Echo fun_cutstr ('aaaaaaaaaaaaaaaaaaaaaaaaaaa', 5); // out disply: "aaaaa ...";
# Because it is UTF-8 encoded, each Chinese character is 4 bytes. here, "I am..." is returned ...";
Echo fun_cutstr ('I am a technician', 8 );
# Or we need to consider serious damage to the function, such as the following function
Echo fun_cutstr (false); // out: false
Echo fun_cutstr ('tbbbbbbbbbbbbs ', 'aaaaaaa'); // out: tbbbbbbbbbbbbs
Echo fun_cutstr ('', 'aaaaaaaaa'); // out: empty
?>
Basic knowledge of PHP functions
A: Like naming variables, you cannot use built-in function names or numbers to name functions.
B: repeated calls.
C: supports static elements.
D: non-fixed parameters are supported.
The technician is recommended to standardize functions as follows:
A: create A category prefix for the function name, for example, str_xxx for The struct type, bool_xxxx for the Boolean type, APP_xxxx for the public function, and temp_xxx for the temporary function.
B: The first step of the function is to judge first. although sometimes you know that a certain type of parameter will be passed in, as for standardization, first judgment and then processing are for program robustness and safety.
C: do not pollute original variables. if you have project experience and debug application experience, you will understand.
D: reference functions should be used as little as possible, occupying a large amount of memory and causing serious losses.
E: Do not write code in uppercase; do not think it is cool.
F: excessive generation of functions is a method of regression. you can think about whether the process is repetitive, whether it needs to be encapsulated, and whether it is wise to block the process as a function at will.
G: write your function comments.
The code is as follows:
$ B = & fun_cutstr ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 5); // out disply: "aaaaa ...";
Fun_cutstr ('cccccccccccccccccccccccc', 5 );
Echo $ B;
?>
The referenced function will not run properly in php 5.3, and 6.0 will eventually discard it. In theory, echo $ B will return ccccc...
$ B introduces the function address, so any changes to the function will be assigned to $ B.
Of course, these can be seldom used, so you don't have to worry too much, especially for new learners.
Static functions are as follows:
The code is as follows:
/**
* @ Author Yuans
* @ Copyright php.com
* @ Package: common usage and features of the function.
*/
# Notes for writing static functions.
Function fun_static (){
Static $ a = 1;
Echo $ a ++;
}
Fun_static ();
Fun_static ();
Fun_static ();
?>
Static $ a = 1; it is executed only when the function is called for the first time, indicating that it is static. during the second execution, the $ a variable is used to retrieve the static value, instead of assigning $ a = 1. and so on, the values are constantly added.