Copy code The code is as follows:
/**
* @author Yuans
* @copyright php.com
* Common usage methods and characteristics of @package functions.
*/
# Basic function Notes on writing.
// In order to facilitate the management of the IDE and the code prompt function, we use fun_ at the beginning of all function names.
function fun_cutstr($str,$str_width=0,$str_pad='.. .'){
// Each function must consider some abnormal situations, such as the function introduction is wrong, it is 0, it is false, etc.
// Since the external expectation returns the character after the intercepted character, so even this If the function is not working, it should return the value it passed in.
if(empty($str) === true || empty($str_width) === true)
return $str;
//Parameter filtering
$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 judgment. If return_str cannot have a value, many servers will not be able to execute it because it is an mb function.
if(empty($return_str) === false){
return $return_str .$str_pad;
}else{
return $str;
}
}
echo fun_cutstr('aaaaaaaaaaaaaaaaaaaaaa',5); // out disply: "aaaaa...";
# Since it is UTF-8 encoding, each Chinese character is 4 bytes, and "I am..." will be returned here;
echo fun_cutstr('I am a technical worker',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('tbbbbbbbbs','aaaaaaaa'); // out: tbbbbbbbbs
echo fun_cutstr('','aaaaaaaa'); //out: empty
?>
Some basic knowledge of PHP functions
A: Same as variable naming, Function names cannot be built-in, and functions cannot be named with numbers.
B: Repeatable callability.
C: Support static elements.
D: Support unfixed parameters
Personally recommend that technicians make changes to the function The following specifications:
A: Create a classification prefix for the function name, for example, the character type is str_xxx, the Boolean type is bool_xxxx, the public function is open_xxx, the application function is APP_xxxx, and the temporary type is temp_xxx
B: Function Please judge first in the first step. Although sometimes you know that a certain type of parameter will be passed in, as a standard, judging first and then processing is for the robustness of the program and for safety.
C: Do not pollute the original variables, If you have project experience and debug application experience, you will understand.
D: Use reference functions as little as possible, which takes up a lot of memory and causes serious losses.
E: Don’t write code in capital letters, don’t think it’s awkward cool.
F: Overgenerating functions is a regressive approach. You can think about whether it is repetitive and whether it needs packaging. It is not wise to arbitrarily encapsulate the process into functions.
G: Well written Your function comments.
Copy code The code is as follows:
$b = &fun_cutstr(' aaaaaaaaaaaaaaaaaaaaaa',5); // out disply: "aaaaa...";
fun_cutstr('cccccccccccccccccc',5);
echo $b;
?>
The reference function will not run properly on PHP 5.3 version, and 6.0 will eventually abandon it. Theoretically, echo $b will return ccccc...
$b introduces the address of the function, for this function Any changes will be assigned to $b.
Of course these can be rarely used, so don’t worry too much, especially for new learners.
The static function is expressed as follows:
Copy the code The code is as follows:
/**
* @author Yuans
* @copyright php.com
* Common usage methods and characteristics of @package functions.
*/
# Notes on writing static functions.
function fun_static(){
static $a = 1;
echo $a ++;
}
fun_static();
fun_static();
fun_static();
?>
static $a = 1; will only be executed when the function is called for the first time, indicating that it is static. When it is executed for the second time, the $a variable will retrieve the static value and will not execute $ Assignment of a = 1. By analogy, the values are added continuously.
http://www.bkjia.com/PHPjc/323858.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323858.htmlTechArticleCopy the code as follows: ?php /*** @author Yuans * @copyright php.com * Common usage methods and characteristics of @package functions.*/ # Notes on writing basic functions. // In order to facilitate the management of IDE...