Summary of common methods of php function and points of attention

  • 2020-05-09 18:17:39
  • OfStack

 
<?php 
/** 
* @author Yuans 
* @copyright php.com 
* @package  The common usage method and characteristic of the function . 
*/ 
#  Note about basic function writing . 
//  In order to facilitate ide Management and code prompt function , We use it for all function names fun_ At the beginning . 
function fun_cutstr($str,$str_width=0,$str_pad='...'){ 
//  Every function has to be considered 1 Something unusual ,  Let's say the function is not introduced correctly ,  for 0, for false Etc. . 
//  Because the external expectation is to return the character after the intercepting character , So even if this function doesn't work , You should also return the value it passed in . 
if(empty($str) === true || empty($str_width) === true) 
return $str; 
//  Parameters of the filter  
$str_width += 0; 
//  keep 1 A principle ,  Try not to pollute the original parameters , 
$return_str = mb_strcut($str,0,$str_width,'utf-8'); 
//  Strengthen the judge ,  if return_str Unable to have value , Because it is mb function , Many servers will fail to execute . 
if(empty($return_str) === false){ 
return $return_str.$str_pad; 
}else{ 
return $str; 
} 
} 
echo fun_cutstr('aaaaaaaaaaaaaaaaaaaaaaaa',5); // out disply: "aaaaa..."; 
#  Because it is utf-8 coding ,  So each character is 4 byte ,  This will return " I am a ..."; 
echo fun_cutstr(' I'm a technician ',8); 
#  Or we need to think about a serious disruption of the function , For example, the following function  
echo fun_cutstr(false); //out: false 
echo fun_cutstr('tbbbbbbbbs','aaaaaaaa'); // out: tbbbbbbbbs 
echo fun_cutstr('','aaaaaaaa'); //out: empty 
?> 

Some basics of the PHP function
A: like variable name 1, you can't build in function names. You can't name functions by Numbers.
B: repeatability.
C: supports static elements.
D: supports variable parameters
I suggest the technician to make the following specifications for the function:
A: class prefixes are established for function names, such as str_xxx for characters, bool_xxxx for Boolean, APP_xxxx for public functions, open_xxx for application functions, and temp_xxx for temporary functions
B: the first step of the function is to judge first. Although you sometimes know that 1 will pass in a certain type of parameter, judging before processing is for the sake of the program's robustness and security as a standard.
C: don't contaminate the original variables. If you have experience with projects and debug applications, you will understand.
D: use the reference function as little as possible. It takes up a lot of memory and consumes a lot of energy.
E: don't write code in uppercase and don't feel too cool.
F: overproduction of functions is a way of stepping back. You can think about whether it's repetitive, whether it needs wrapping, and whether it's unwise to arbitrarily wrap the process into functions.
G: write your function comments.
 
<?php 
$b = &fun_cutstr('aaaaaaaaaaaaaaaaaaaaaaaa',5); // out disply: "aaaaa..."; 
fun_cutstr('cccccccccccccccccc',5); 
echo $b; 
?> 

The reference function will not work properly on php version 5.3, and 6.0 will eventually discard it. Theoretically, echo $b will return ccccc...
$b introduces the address of the function, for which any change in the function is assigned to $b.
Of course these can really be used sparingly, and don't worry too much, especially for new learners.
The static function is as follows:
 
<?php 
/** 
* @author Yuans 
* @copyright php.com 
* @package  The common usage method and characteristic of the function . 
*/ 
#  Static function writing attention points . 
function fun_static(){ 
static $a = 1; 
echo $a ++; 
} 
fun_static(); 
fun_static(); 
fun_static(); 
?> 
static $a = 1;  Only in the first 1 Executed the next time the function is called ,  That means it's static ,  The first 2 When running time , $a The variable is just fetching the static value ,  They don't execute $a = 1 The assignment of . So on ,  The Numbers keep adding up .<BR> 

Related articles: