May 11, 2021 PHP
1. First, the use of references as parameters
2. Second, the use of global variables
3. Third, the use of static variables
When learning recursive functions, the whites may not be able to understand the principle and operating mechanism of recursive functions, recursive functions are commonly used to a class of functions, the most basic feature is that the function itself calls itself, but must be conditional judgment before calling itself, if the conditions are met, then call the function itself, if not satisfied, then terminate the function's self-call, and then the current process of the main control to the previous layer of functions to execute, otherwise it will be unlimited call. Here are three basic ways php implements recursive functions.
function test($a=0,&$result=array()){
$a++;
if ($a<10) {
$result[]=$a;
test($a,$result);
}
echo $a;
return $result;
}
The above example is very short answer, with a-lt;10 as the condition of judgment, the condition is established, then assign a to the result; P
assing a reference to the result into the function adds each recursively generated a to the resulting array result.
So the $result array generated by this example is array ( . 1 .
function test($a=0,$result=array()){
global $result;
$a++;
if ($a<10) {
$result[]=$a;
test($a,$result);
}
return $result;
}
function test(){
static $count=0;
echo $count;
$count++;
}
test();
test();
test();
test();
test();
What is the execution result of this piece of code? I
s it 00,000? I
t must not be. Y
es 01234. F
irst of all, the first call test(), static initialization of $count, after each execution will retain the value of $count, no longer initialization, equivalent to directly ignoring the static $count-0;
This sentence.
function test($a=0){
static $result=array();
$a++;
if ($a<10) {
$result[]=$a;
test($a);
}
return $result;
}
function test($a=0){
$a++;
if ($a<10) {
echo $a;
test($a);
}
}
function arrContentReplact($array)
{
if(is_array($array))
{
foreach($array as $k => $v)
{
$array[$k] = arrContentReplact($array[$k]);
}
}else
{
$array = str_replace(
array('<', '>'),
array('{', '}'),
$array
);
}
return $array;
}
$arr = array(array("< 小刚>","< 小晓>",array("<小强>",array("<浪人>"))),"< 小飞>","< 小李>","< 小红>");
$arr3 = arrContentReplact($arr);
echo "
";
print_r($arr3);
echo "
";
?>
CREATE TABLE IF NOT EXISTS `class` (
`id` mediumint(6) NOT NULL AUTO_INCREMENT,
`title` varchar(30) NOT NULL,
`pid` mediumint(6) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
function get_str($id = 0) {
global $str;
$sql = "select id,title from class where pid= $id";
$result = mysql_query($sql);//查询pid的子类的分类
if($result && mysql_affected_rows()){//如果有子类
$str .= '<ul>';
while ($row = mysql_fetch_array($result)) { //循环记录集
$str .= "<li>" . $row['id'] . "--" . $row['title'] . "</li>"; //构建字符串
get_str($row['id']); //调用get_str(),将记录集中的id参数传入函数中,继续查询下级
}
$str .= '</ul>';
}
return $str;
}
The above function get_str() by recursion, constantly querying sub-categories, and eventually returning strings, you can modify the str according to the needs of the project, and eventually generate an infinite list of ratings:
include_once('connect.php'); //连接数据库,connect.php文件自己写一个啊
echo get_str(0); //输出无限级分类
function get_array($id=0){
$sql = "select id,title from class where pid= $id";
$result = mysql_query($sql);//查询子类
$arr = array();
if($result && mysql_affected_rows()){//如果有子类
while($rows=mysql_fetch_assoc($result)){ //循环记录集
$rows['list'] = get_array($rows['id']); //调用函数,传入参数,继续查询下级
$arr[] = $rows; //组合数组
}
return $arr;
}
}
Function get_array() returns an array, which is what we expect, so it is recommended to use get_array() to get the array, so that we can do anything about the array, for example, we can convert the array into a jason format of data to the front page, the front page can be resolved jason data flexible display classification information.
For example, a list of classifications for tree structures, a list of down-down categories, and so on.
include_once('connect.php'); //连接数据库
$list = get_array(0); //调用函数
print_r($list); //输出数组
echo json_encode($list);