php 版本 < 的 goto 等效项5.3.0?

发布于 2025-01-02 23:01:44 字数 568 浏览 1 评论 0 原文

我需要在代码中使用 goto 运算符,因为我似乎想不出解决方法。但是问题是我的主机只安装了 PHP 版本 5.2.17。

有什么想法吗?

下面是我的代码:

if ($ready !=="y")
{
    $check=mysql_query("SELECT `inserted` FROM `team`");
    $numrows  = mysql_num_rows($check);  

    $i="0";

    while ($i<$numrows && $row = mysql_fetch_assoc($check))
    {

        $array[$i] = $row['inserted'];
        $i++;

    }
    if (in_array("n", $array)) 
    {

        goto skip;

    }
    else
    {
        mysql_query("

            UPDATE game SET ready='y'

        ");
    }

}

skip:

I need to use the goto operator in my code as I can't seem to think of a way around it. However the problem is my host only has PHP version 5.2.17 installed.

Any ideas?

Below is my code:

if ($ready !=="y")
{
    $check=mysql_query("SELECT `inserted` FROM `team`");
    $numrows  = mysql_num_rows($check);  

    $i="0";

    while ($i<$numrows && $row = mysql_fetch_assoc($check))
    {

        $array[$i] = $row['inserted'];
        $i++;

    }
    if (in_array("n", $array)) 
    {

        goto skip;

    }
    else
    {
        mysql_query("

            UPDATE game SET ready='y'

        ");
    }

}

skip:

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

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

发布评论

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

评论(4

淡水深流 2025-01-09 23:01:45

您想要使用正确的控制字来打破循环:

if ($ready !=="y")
{
    $check=mysql_query("SELECT `inserted` FROM `team`");
    $numrows  = mysql_num_rows($check);  

    $i="0";


    while ($i<$numrows && $row = mysql_fetch_assoc($check))
    {


    $array[$i] = $row['inserted'];
    $i++;

    }
    if (in_array("n", $array)) 
    {

        break;

    }
    else
    {
    mysql_query("

    UPDATE game SET ready='y'

    ");
    }

}

break< /a> 关键字将完全执行您想要的操作:结束 while 循环的执行。永远不要使用 goto!

You want to use the correct control word to break from the loop:

if ($ready !=="y")
{
    $check=mysql_query("SELECT `inserted` FROM `team`");
    $numrows  = mysql_num_rows($check);  

    $i="0";


    while ($i<$numrows && $row = mysql_fetch_assoc($check))
    {


    $array[$i] = $row['inserted'];
    $i++;

    }
    if (in_array("n", $array)) 
    {

        break;

    }
    else
    {
    mysql_query("

    UPDATE game SET ready='y'

    ");
    }

}

The break keyword will do exactly what you want: End the execution of the while loop. Never ever ever use a goto!

谎言 2025-01-09 23:01:45

作为对这篇文章标题的直接回应:

do{

  if (!$condition){
    break;
  }

  // Code called if conditions above are met

}while(0);

//Code always called

在某些情况下,这个或 goto 可以生成非常整洁和可读的代码。

As a direct response to the title of this post:

do{

  if (!$condition){
    break;
  }

  // Code called if conditions above are met

}while(0);

//Code always called

In some circumstances, this, or a goto, can make for very tidy and readable code.

百合的盛世恋 2025-01-09 23:01:45

首先,您可以使用 break 退出初始循环。其次,如果您需要测试任何内容,请在调用 break 之前设置一个变量(必须是全局的而不是本地的)作为标志或指示器,然后执行一个条件测试语句,其中您的 skip 行用于执行您需要的任何其他步骤。

First, you could use a break to exit your initial loop. Second, if you need to test for anything, set a variable (must be global not local) as a flag or indicator before calling break, then do a conditional test statement where your skip line is to perform any additional steps you need.

爱格式化 2025-01-09 23:01:44

您的代码中有一些反模式。让我们把它清理干净。我将立即解释发生了什么变化。

if($ready !== "y") {
    $sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
    if(mysql_num_rows($sth) > 0) {
        mysql_query("UPDATE game SET ready = 'y'");
    }
}

首先要做的事情是:无需执行查询、获取所有结果,然后循环遍历这些结果 (in_array) 来查找特定值。让数据库通过明确查找 inserted 为字符串文字 "n" 的行来为您完成此操作。

因为我们知道我们只会返回 "n" 条记录,所以我们只需要检查是否有任何结果。如果是这样,请运行查询。如果没有 "n" 记录,则不会运行 UPDATE

如果您需要知道 UPDATE 已运行,请添加检查:

$ran_update = false;
if($ready !== "y") {
    $sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
    if(mysql_num_rows($sth) > 0) {
        mysql_query("UPDATE game SET ready = 'y'");
        $ran_update = true;
    }
}
if($ran_update) {
// ...
}

There are a few anti-patterns in your code. Let's clean it up. I'll explain what's been changed in a jiffy.

if($ready !== "y") {
    $sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
    if(mysql_num_rows($sth) > 0) {
        mysql_query("UPDATE game SET ready = 'y'");
    }
}

First things first: There is no need to perform a query, fetch all of the results, then loop through those results (in_array) looking for a specific value. Let the database do that for you by expressly looking only for rows where inserted is the string literal "n".

Because we know that we're only getting "n" records back, we just need to check if there are any results. If so, run the query. If there are no "n" records, the UPDATE isn't run.

If you need to know that the UPDATE ran, add a check for it:

$ran_update = false;
if($ready !== "y") {
    $sth = mysql_query("SELECT inserted FROM team WHERE inserted = 'n'");
    if(mysql_num_rows($sth) > 0) {
        mysql_query("UPDATE game SET ready = 'y'");
        $ran_update = true;
    }
}
if($ran_update) {
// ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文