【问题标题】:Undefined variable $e [closed]未定义的变量 $e [关闭]
【发布时间】:2014-10-28 13:59:33
【问题描述】:

[28-Oct-2014 15:46:22 Europe/Bucharest] PHP 通知:未定义 变量:e in /home/lagramad/public_html/discounts/system/modules/gateway/mobilpay/cardRedirect.php 在第 226 行

     }
catch (Exception $e) {


    echo "\r\n<h3>Plata voucher prin card</h3>\r\n";

if (!($e instanceof Exception)) 
    echo "<p> \r\n\t<form name=\"frmPaymentRedirect\" method=\"post\" action=\"";
    echo $paymentUrl;
    echo "\">\r\n\t<input type=\"hidden\" name=\"env_key\" value=\"";
    echo $objPmReqCard->getEnvKey();
    echo "\"/>\r\n\t<input type=\"hidden\" name=\"data\" value=\"";
    echo $objPmReqCard->getEncData();
    echo "\"/>\r\n\t<p>\r\n\t\tPentru a finaliza plata vei redirectat catre pagina de plati securizata a mobilpay.ro\r\n\t</p>\r\n\t<p>\r\n\t\tDaca nu esti redirectat in 5 secunde apasa <input type=\"image\" src=\"images/12792_mobilpay-96x30.gif\" />\r\n\t</p>\r\n\t</form>\r\n</p>\r\n";

    echo "<script type=\"text/javascript\" language=\"javascript\">\r\n\twindow.setTimeout(document.frmPaymentRedirect.submit(), 5000);\r\n</script>\r\n";
} else {
    echo "<p>";

    echo "<strong>";
    echo $e->getMessage();
    echo "</strong></p>\r\n";
}


echo "\r\n\r\n\r\n";
return 1;


?>

【问题讨论】:

  • $e 只有在实际发生异常时才会被定义,例如catch 块被触发。但是在您后来的if() 中,您在两个代码路径中都无条件地使用$e

标签: php var


【解决方案1】:

$e在发生异常时才存在:

try {
   throw new Exception('foo');
} catch (Exception $e) {
   // do nothing
}

try {
   $foo = 1; // no exception occurs
} catch (Exception $f) {
   // do nothing
}

var_dump($e); // spits out the exception info
var_dump($f); // undefined variable, because no exception occured to define it

【讨论】:

    【解决方案2】:

    把你的 if 语句放在异常中

    catch (Exception $e) { if ($e......) { blah vlah blah.... } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-13
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2016-04-06
      • 2021-08-03
      • 1970-01-01
      • 2020-04-12
      相关资源
      最近更新 更多