我遇到了“未定义的变量:第 2 行 transaction.php 中的错误”的问题
如果我尝试使用 $errors 变量创建模板站点,它应该在所需的 transaction.php 中给出 $error 数组,但它没有,应该如何将其赋予此 transaction.php 才能使其工作?
索引.php
function checkTransaction(){
if(!empty($_POST['code'])){
return true;
}
return [
'code' => false
];
}
function renderTransactionResult($errors){
$getTemplate = new Template('transaction', [
'errors' => $errors
]);
echo $getTemplate;
}
$transactionid = htmlspecialchars($_POST['search']);
$transactionrows = $main->CheckNumRowsTransaction($transactionid);
if($transactionrows === 1){
$errors = checkTransaction($_POST);
renderTransactionResult($errors);
}else{
$errors = [
'transaction' => false
];
renderTransactionResult($errors);
}
获取交易.php
<?php
Class Template{
public function __construct($template, $vars){
$this->template = $template;
$this->vars = $vars;
}
public function __toString(){
foreach ($this->vars as $name => $value){
$name = $value;
}
unset($name, $value);
ob_start();
require('templates/' . $this->template . '.php');
return ob_get_clean();
}
}
交易.php
<?php
if(is_array($errors)){
echo 'something';
}else{
echo 'something';
}
?>
月关宝盒