Home > Article > Backend Development > Example of PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function
This article mainly introduces the PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. It can simulate the template variable assignment function in the tp framework and involves PHP object-oriented array assignment. For operational skills, friends in need can refer to
This article describes the example of PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. Share it with everyone for your reference, the details are as follows:
Here simulates the tp framework template variable allocation and assignment operations.
extract($arr);
//The role of extract: import variables from the array into the current symbol table, the keys are used as variables, and the values are used as values! compact();
// — Create an array, including variable names and their values
class base{ public $array; public $key; public $val; public function assign($key,$val){ if(array($val)){ $this->array["$key"] = $val; }else{ $this->array["$key"] = compact($val); } } public function display($tpl){ $this->assign($this->key,$this->val); extract($this->array); if(file_exists($tpl)){ //模板存在就加载文件。 include $tpl; } } } class indexcontroller extends base{ public function index(){ $arr = array('a'=>'aaaaaaa','b'=>array('a'=>'111111','b'=>'22222','c'=>'3333'),'c'=>'ccccccc','d'=>'dddddd','e'=>'eeeee'); $str = '我是字符串'; $this->assign('arr',$arr); $this->assign('str',$str); $this->display('index.html'); } } $base = new base; $base->index();
yii2 installation detailed process_php example
The above is the detailed content of Example of PHP custom function to implement assign() array assignment to template and extract() variable assignment to template function. For more information, please follow other related articles on the PHP Chinese website!