需要一些简单的帮助来理解一些 PHP5 之前的代码

发布于 2024-09-13 15:54:50 字数 1132 浏览 2 评论 0原文

我从另一个问题中转向了 nstrees,并且一直在研究该脚本。它最后一次更新是在 2005 年,并且依赖于一些显然已被弃用的东西,例如 HTTP_POST_VARS,因为我使用 PHP 还不到一年,所以我并没有立即熟悉这些东西。

无论如何,编码风格在我的菜鸟眼中似乎很奇怪,我想对该函数的哪些部分的作用有第二个意见:

// returns the first node that matches the '$whereclause'.
// The WHERE clause can optionally contain ORDER BY or LIMIT clauses too.
function nstGetNodeWhere ($thandle, $whereclause) {
    $noderes['l'] = 0;
    $noderes['r'] = 0;
    $res = mysql_query("SELECT * FROM ".$thandle['table']." WHERE ".$whereclause);
    if (!$res) { // problem area 1
        _prtError();
    } else {
        if ($row = mysql_fetch_array($res)) { // problem area 2
            $noderes['l'] = $row[$thandle['lvalname']];
            $noderes['r'] = $row[$thandle['rvalname']];
        }
    }
    return $noderes;
}

在上面的代码中,我将我不太确定的点标记为 // 问题区域x,其他一切都是原始脚本。

关于 PA1,这只是检查查询是否成功运行吗?

对于 PA2,NetBeans 向我发出警告“可能的意外赋值,应避免条件赋值。” ...所以我立即将其从 = 更改为 = = 当然破坏了脚本,呵呵。

想了想,我猜这只是对 $res 进行了另一次错误检查,这次是为了确保确实返回了一些数据?

最后,这是奇怪的 PHP 还是我太新手而无法理解它?

谢谢老兄!

I was turned on to nstrees from another question, and have been working through the script. It was last updated in 2005 and relied on some stuff that has since apparently been deprecated like HTTP_POST_VARS which I wasn't immediately familiar with since I've been doing PHP for only a little less than a year.

Anyways, the coding style seems weird to my rookie eyes, and I'd like a second opinion on what part of this function does:

// returns the first node that matches the '$whereclause'.
// The WHERE clause can optionally contain ORDER BY or LIMIT clauses too.
function nstGetNodeWhere ($thandle, $whereclause) {
    $noderes['l'] = 0;
    $noderes['r'] = 0;
    $res = mysql_query("SELECT * FROM ".$thandle['table']." WHERE ".$whereclause);
    if (!$res) { // problem area 1
        _prtError();
    } else {
        if ($row = mysql_fetch_array($res)) { // problem area 2
            $noderes['l'] = $row[$thandle['lvalname']];
            $noderes['r'] = $row[$thandle['rvalname']];
        }
    }
    return $noderes;
}

In the above code I marked the spots I'm not quite sure about as // problem area x, everything else is the original script.

Regarding PA1, is that just a check on whether the query was successfully run?

And PA2, NetBeans gives me a warning "Possible accidental assignment, assignments in conditions should be avoided." ... so I promptly changed that from = to == and of course broke the script, heh.

Thinking about it, I guess it's just another error check on $res, this time to make sure that some data was actually returned?

Lastly, is this bizarre PHP or am I just too green to grok it?

Thanks dude(tte)s!

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

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

发布评论

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

评论(3

单身狗的梦 2024-09-20 15:54:50

问题区域 1 正是您所想的那样。它检查查询是否成功运行。查看 mysql_query 的文档(因为您使用 Netbeans,您还可以在编辑器并按 F2 以获得内联弹出窗口)。您正在寻找“返回值”部分。那里说如果出现任何错误,它将返回 FALSE(布尔值 FALSE)。

检查

if (!$res)

与检查相同

if ($res == false)

理想情况下,您应该检查,

if ($res === false)

因为它更安全,因为 PHP 对变量 == false 非常宽松,但我现在不想让您感到困惑。如果您愿意,可以先阅读

问题区域 2 很常见,但会在 Netbeans 中显示警告,是的。它所做的是将 mysql_fetch_array() 中的一个值分配给 $row,并检查该值现在是 true 还是 false,同时还允许您在以下代码块中的 $row 中使用相同的值if()。

Problem Area 1 is exactly what you think it is. It checks if the query ran successfully. Look at the documentation for mysql_query (since you use Netbeans you can also highlight the function name in the editor and hit F2 to get an inline popup). You're looking for the "Return Values" section. Over there it says it will return FALSE (the boolean FALSE) on any errors.

Checking

if (!$res)

is the same as checking for

if ($res == false)

Ideally you should be checking for

if ($res === false)

because it's safer since PHP is very relaxed about variables == false, but I'd rather not confuse you with that right now. If you'd like you can start by reading about strong typing.

Problem Area 2 is pretty common but will show a warning in Netbeans, yes. What it's doing is assigning a value from mysql_fetch_array() into $row, and checking if that is now true or false, while also letting you use that same value now in $row in the following code block inside the if().

面犯桃花 2024-09-20 15:54:50

我假设此代码可以加载文章,然后将它们排序到“左右”数组中,以便可以以两列布局打印。或许。 :)

是的,PA1 正在检查查询是否成功。 PA2 只是获取实际行。

I assume this code can load articles then sort them into a 'left-right' array so it can be printed in a two column layout. Maybe. :)

Yep, PA1 is checking if the query was successful or not. PA2 is simply fetching the actual row.

心房敞 2024-09-20 15:54:50
if (!$res) { // problem area 1 

如果 mysql_query 未能执行 SQL 查询,则返回 false。这基本上是对除 false 之外的任何值的测试,因此 if 测试将匹配 mysql_query 中是否存在错误并执行代码的打印错误分支

if ($row = mysql_fetch_array($res)) { // problem area 2  

将结果集中的下一行分配给 $row;或者如果结果集中没有更多行,则将 $row 设置为 false,在这种情况下,“if 测试”将为 false

if (!$res) { // problem area 1 

mysql_query returns a false if it fails to execute the SQL query. This is basically a test for not (!) any value other than false, so the if test will match if there was an error in mysql_query and execute the print error branch of the code

if ($row = mysql_fetch_array($res)) { // problem area 2  

Assigns the next row from the resultset to $row; or sets $row to false if no more rows in the resultset, in which case the "if test" will be false

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