Zend:php 页面中未定义的变量

发布于 2024-11-19 09:09:07 字数 585 浏览 1 评论 0原文

我有一个 error.php 文件,可以将其大大简化为:

<?

  if (!isset($error))
    $error = "Unspecified Error";

  echo "Error: $error";

?>

仅导航到 error.php 不是“正常用法”。相反,我会做类似的事情:

$dbh = mysql_connect($host, $user, $pass);

if (!$dbh)
{
  $error = "Can't connect to MySQL: " . mysql_error();
  include('error.php');
  exit();
}

也就是说,如果用户确实导航到error.php,那么他们只会按预期得到“错误:未指定的错误”。

我的所有代码都正常工作,并且错误页面显示并完全按预期工作,但是 Zend 抱怨 $error 在行上未定义:if (!isset($error))

我意识到我的设计模式很糟糕,但在这种情况下我只是将一些快速而肮脏的东西放在一起。

I have an error.php file which can be grossly simplified to:

<?

  if (!isset($error))
    $error = "Unspecified Error";

  echo "Error: $error";

?>

It is not "normal usage" to just navigate to error.php. Rather, I would do something like:

$dbh = mysql_connect($host, $user, $pass);

if (!$dbh)
{
  $error = "Can't connect to MySQL: " . mysql_error();
  include('error.php');
  exit();
}

That said, if the user does navigate to error.php then they will just get "Error: Unspecified Error" as expected.

All my code is working, and the error page shows up and works exactly as expected, however Zend is complaining that $error is undefined on the line: if (!isset($error)).

I realise my design pattern is awful, but I'm just throwing together something quick-and-dirty in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女山的月亮 2024-11-26 09:09:07

更好的主意,创建一个函数:

function output_error( $error = NULL )
{
    if( !$error ) $error = "Unspecified Error";
    echo "Error: $error";
}

它的好处是既可以消除 Zend 问题,又可以让您拥有更好的设计。然后:

if (!$dbh)
{
  include('error.php');
  output_error( "Can't connect to MySQL: " . mysql_error() );
  exit();
}

Better idea, create a function instead:

function output_error( $error = NULL )
{
    if( !$error ) $error = "Unspecified Error";
    echo "Error: $error";
}

It has the benefit of both removing the Zend issue, and you have a MUCH better design. Then:

if (!$dbh)
{
  include('error.php');
  output_error( "Can't connect to MySQL: " . mysql_error() );
  exit();
}
晨光如昨 2024-11-26 09:09:07
$dbh = mysql_connect($host, $user, $pass);

if (!$dbh){
  $error = "Can't connect to MySQL: " . mysql_error();
  if(!include('error.php'){
      echo $error;
      exit();
  }
}
$dbh = mysql_connect($host, $user, $pass);

if (!$dbh){
  $error = "Can't connect to MySQL: " . mysql_error();
  if(!include('error.php'){
      echo $error;
      exit();
  }
}
自控 2024-11-26 09:09:07

试试这个:
if (!isset(@$error))

Try this:
if (!isset(@$error)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文