php代码,是慢还是错误?

发布于 2024-10-12 11:29:34 字数 306 浏览 4 评论 0原文

“在这一行找到多个注释”

我以前一直在使用 aptana 和其他 IDE,但从未给过我这个错误,但昨天我安装了 zend studio,它在我分配的所有代码中给出了以下错误,并且还检查了条件同时。

code:
line 16: if ($message_array = @unserialize($e->getMessage()))
line 17:    $message = $message_array;

在所有 if 条件下,我将值分配给变量并检查变量是否为真/假,它给我错误“在这一行找到多个注释”

"multiple annotations found at this line"

i have been using aptana and other IDE before, but never gave me this error, but yesterday i installed zend studio and it was giving following error in all my code where i have assigned and also check if condition at same time.

code:
line 16: if ($message_array = @unserialize($e->getMessage()))
line 17:    $message = $message_array;

on all if condition, where i assigned the value to a variable and also check if the variable is true/false, it gives me error "multiple annotations found at this line"

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

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

发布评论

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

评论(2

左秋 2024-10-19 11:29:34

是的,这种语法通常被大多数像样的 IDE 标记为“意外赋值”(因为如果您指的是 ===,这并不明显)。大多数将允许您将其包装在 () 中以消除错误(因为很明显您想要结果而不是测试):

if (($message_array = @unserialize($e->getMessage()))) {
}

另外,为了可读性和可维护性,我会建议一些事情那里。

首先,使用大括号。由于这只是一种特殊情况,不允许您使用它们,因此我个人认为最好始终使用它们,以便清楚地表达含义。

其次,在 if 子句之外进行所有赋值。它使您的意思更加明确、更容易一目了然。另外,它看起来不那么混乱。

$message_array = @unserialize($e->getMessage());
if ($message_array) {
    ...
}

第三,我建议避免使用 @ 运算符。对我来说,这是代码腐烂的标志。虽然我知道它很容易使用,并且比正确处理错误更容易,但我认为这只是一条捷径,会让你的生活变得更加困难。您可以通过几种方式避免它。首先,您可以在将字符串传递给unserialize 之前检查该字符串。确保它非空,字符串等。其次,您可以安装 错误处理程序 抛出 PHP 错误异常(我所做的)。这样,您只需将 unserialize 调用包装在 try {} catch(){} 块中。这更好,因为您实际上可以检查错误,而不是仅仅相信抛出的错误就是您认为的那样......

Yeah, that syntax is typically flagged by most decent IDEs as "Accidental Assignment" (since it's not obvious if you meant = or ==). Most will allow you to wrap it in a () to silence the error (since then it's apparent you want the result rather than a test):

if (($message_array = @unserialize($e->getMessage()))) {
}

Also, for readability and maintainability, I would suggest a few things there.

First, use braces. Since it's only a special case that lets you not use them, I personally think it's better form to always use them so that it's clear what was meant.

Second, do all asignment outside of the if clause. It makes it more explicit and easier to tell at a quick glance what you meant. Plus it looks less cluttered.

$message_array = @unserialize($e->getMessage());
if ($message_array) {
    ...
}

Third, I would suggest avoiding the @ operator. To me it's a sign of code-rot. While I know it's easy to use and easier than properly handling the error, I think it's just a short-cut that will make life harder for you. You can avoid it in a few ways. First, you can check the string before you pass it to unserialize. Make sure it's non-empty, a string, etc. Secondly, you can install an error handler to throw exceptions on PHP errors (what I do). That way, you just wrap the unserialize call in a try {} catch(){} block. It's nicer since you actually can inspect the error rather than just trusting that the thrown error is what you think it is...

黒涩兲箜 2024-10-19 11:29:34

这不是一个实际的错误。相反,IDE 在同一代码行中发现了多个错误、警告或提示。

您可以查看“问题”选项卡以查看实际的错误和警告。

That is not an actual error. Instead the IDE has found more than one error, warnings or hints at the same code line.

You can look at the Problems tab to see the actual errors and warnings.

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