【问题标题】:calculating button size using foreach使用 foreach 计算按钮大小
【发布时间】:2020-07-12 04:23:33
【问题描述】:

我遵循 Luke Welling 和 Laura Thomson 的 PHP 和 MySQL Web 开发示例。 在第 6 章中有一个 //calculate 按钮大小,它具有在 7.2 中弃用的 each() 函数。根据 Visual Studio 的提示。我可以使用 foreach 循环代替 each()。

public function DisplayMenu($buttons)
{
    echo "<table width=\"100%\" bgcolor=\"white\" 
      cellpadding=\"4\" cellspacing=\"4\">\n";
    echo "<tr>\n";

    //calculate button size
    $width = 100 / count($buttons);

    while (list($name, $url) = each($buttons)) {
        $this->DisplayButton(
            $width,
            $name,
            $url,
            !$this->IsURLCurrentPage($url)
        );
    }
    echo "</tr>\n";
    echo "</table>\n";
}

public function IsURLCurrentPage($url)
{
    if (strpos($_SERVER['PHP_SELF'], $url) == false) {
        return false;
    } else {
        return true;
    }
}

【问题讨论】:

    标签: php arrays foreach


    【解决方案1】:

    我可以使用 foreach 循环代替 each()。

    是的,您可以使用 foreach 代替 each() 作为

    foreach($buttons as $key => $value) {
            $this->DisplayButton(
                $width,
                $key,
                $value,
                !$this->IsURLCurrentPage($value)
            );
        }
    

    【讨论】:

    • 你不再需要 while
    • @Triby 非常感谢,我完全忘记了。?
    • 没关系@RudulphValNario,顺便说一句,很高兴为您提供帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2012-08-09
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多