简体   繁体   English

goto等同于PHP版本<5.3.0?

[英]goto equivalent for php version < 5.3.0?

I need to use the goto operator in my code as I can't seem to think of a way around it. 我需要在代码中使用goto运算符,因为我似乎想不出办法。 However the problem is my host only has PHP version 5.2.17 installed. 但是问题是我的主机只安装了PHP版本5.2.17。

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:

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. 首先,首先要做的是:无需执行查询,获取所有结果,然后循环遍历这些结果( in_array )寻找特定值。 Let the database do that for you by expressly looking only for rows where inserted is the string literal "n" . 让数据库通过只查找inserted字符串文字"n"行来为您做到这一点。

Because we know that we're only getting "n" records back, we just need to check if there are any results. 因为我们知道只返回"n"条记录,所以我们只需要检查是否有任何结果。 If so, run the query. 如果是这样,请运行查询。 If there are no "n" records, the UPDATE isn't run. 如果没有"n"条记录,则不会运行UPDATE

If you need to know that the UPDATE ran, add a check for it: 如果您需要知道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) {
// ...
}

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. break关键字将完全满足您的要求:结束while循环的执行。 Never ever ever use a goto! 永远不要使用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. 在某些情况下,这或转到代码可以使代码整洁易读。

First, you could use a break to exit your initial loop. 首先,您可以使用break退出初始循环。 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. 其次,如果您需要进行任何测试,请在调用break之前将变量(必须是全局的而不是局部的)设置为标志或指示符,然后执行条件测试语句,其中的skip行将执行您需要的任何其他步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM