gpt4 book ai didi

php - 使 undefined variable 在php中出错

转载 作者:行者123 更新时间:2023-12-02 17:44:45 25 4
gpt4 key购买 nike

我有以下代码片段:

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL | E_NOTICE);
print $bla;
print 7;
?>

打印出 bla 未定义的警告,但之后继续。我希望 php 在遇到 undefined variable 时抛出错误并停止代码执行。如何做到这一点?

以上只是一个例子。我想在数千个 block 代码块中处理每个 undefined variable 。

最佳答案

您可以编写自己的错误处理程序。当你遇到这种通知时让它停止执行。看看

http://php.net/manual/en/function.set-error-handler.php

一个小而简单的例子:

function new_error_handler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_NOTICE:
if (strstr($errstr, 'Undefined variable')) {
die('Undefined variable found');
}
break;
}
}

set_error_handler('new_error_handler');

echo $foo;

关于php - 使 undefined variable 在php中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16537562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com