亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP 未定義錯誤變量的問題

PHP 未定義錯誤變量的問題

PHP
HUH函數 2023-08-06 14:34:27
我遇到了“未定義的變量:第 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';    }?>
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

當你實例化變量時,它們有一定的范圍,在這個范圍內變量是已知的。我不明白你是如何加載文件的,但我假設你希望在訪問 index.php 時發生 transaction.php 的邏輯。要實現此目的,可以將 transaction.php 放入一個以 $errors 作為構造函數變量的類,實例化它并調用該方法:


[交易.php]


class Transaction

{

? ? private $errors;

? ? public function _construct($errors)

? ? {

? ? ? ? ?$this->errors = $errors;

? ? }


? ? public function handleErrors()

? ? {

? ? ? ? if(is_array($errors))

? ? ? ? {

? ? ? ? ? ? echo 'something';

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? echo 'something';

? ? ? ? }

? ? }

}

[getTransaction.php]


Class Template

{

? ? public function __construct($template, $vars)

? ? {

? ? ? ? $this->template = $template;

? ? ? ? $this->vars = $vars;

? ? ? ? $transaction = new Transaction($vars); //instantiates your class

? ? ? ? $transaction->handleErrors(); //calls the method

? ? }

? ? 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();

? ? }

}

或者將 transaction.php 變成您在 index.php 中定義并調用的函數: [index.php]


function checkTransaction()

{

? ? if(!empty($_POST['code']))

? ? {

? ? ? ? return true;

? ? }

? ? return [

? ? ? ? 'code' => false

? ? ];

}


function handleErrors($errors)

{

? ? if(is_array($errors))

? ? {

? ? ? ? echo 'something';

? ? }

? ? else

? ? {

? ? ? ? echo 'something';

? ? }

}


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);

}

請注意,我沒有審查您的功能、效率或安全性代碼,而只是制定了解決您的范圍問題的答案。

查看完整回答
反對 回復 2023-08-06
  • 1 回答
  • 0 關注
  • 121 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號