【问题标题】:PHP looking for best way to use 'goto' to skip PHP scriptPHP寻找使用'goto'跳过PHP脚本的最佳方法
【发布时间】:2025-12-09 12:20:16
【问题描述】:

我正在寻找“跳过”PHP 脚本以直接传递给我的 HTML 代码的最佳方式。这是一个例子:

<?php 
include 'ws/main.php';
include 'ws/database/connection.php';

$error = false;
$routes = getCurrentParameters();

if (!isset($routes[0]) && !$error) 
{
    $error = traduction("No promotion entered in the URL");
    goto end;
}
else if (!isset($routes[1]) && !$error)
{
    $error = traduction("No page entered in the URL");
    goto end;
}

if (!$error) 
{
    $result = $connection->query("SELECT * FROM promotion WHERE name = '$routes[0]'");
    $promo = $result->fetch_row();

    if (empty($promo) && !$error) 
    {
        $error = traduction("The promotion '". $routes[0] ."' do not exist");
        goto end;
    }
    else if (!file_exists("page/" . $routes[1] . ".php"))
    {
        $error = traduction("The page '". $routes[1] ."' do not exist");
        goto end;
    }

    $result = $connection->query("SELECT * FROM traduction WHERE promotion_id = $promo[0]");
    $traduction = $result->fetch_all();
}

end:
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Start CSS import -->
    <link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/bootstrap.css' />
    <link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/font-awesome.css' />
    <link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/animate.css' />
    <link rel='stylesheet' href='<?php echo BASE_PATH ?>assets/css/main.css' />
    <!-- End CSS import -->
</head>
<body>
    <?php if (!$error) // If the app do not catch any error
    {
        include "page/$routes[1].php";
    }
    else // If the app already catch an error
    {
        // Include the error.php page
        include "page/error.php";
    } ?>

    <!-- Start JS import -->
    <script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/jquery.js'></script>
    <script type='application/javascript' src='<?php echo BASE_PATH ?>libs/js/tether/dist/js/tether.js'></script>
    <script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/bootstrap.js'></script>
    <script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/moment.js'></script>
    <script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/bootbox.js'></script>
    <script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/sweetalert.js'></script>
    <script type='application/javascript' src='<?php echo BASE_PATH ?>assets/js/main.js'></script>
    <!-- End JS import -->

    <script>
        $(".img").css("background", "url(<?php echo $promo[2]; ?>) no-repeat center center")
    </script>
</body>
</html>

所以,如果你已经阅读了我所有的代码,这并不复杂,我只是在寻找最好的方法来退出我的 PHP 脚本,在顶部,当我遇到问题时直接转到我的 HTML 代码在我的数据库中或信息不匹配。目前,我在脚本末尾使用带有标签“end:”的“goto”方法。我知道“goto”方法根本不是最佳的,所以我正在寻找一种更好的方法,因为我很确定已经存在一种方法。

【问题讨论】:

  • PHP 是否提供goto?干什么用的?
  • 为什么需要goto?您已经在要跳过的脚本部分周围有if (!$error)
  • 它甚至在文档中:php.net/manual/en/control-structures.goto.php (xkcd)
  • @KarstenKoop 我需要像goto 这样的东西,因为我的代码还没有完成,我还有很多东西要添加。我只需要一个简单的方法来摆脱我的 PHP 脚本而不退出 HTML 代码。

标签: php html exit goto skip


【解决方案1】:

如果您将可能想要运行的代码放入函数中,或作为类中的方法,那么运行该代码是一件简单的事情 - 或者选择提前退出,而不做任何事情。

<?php
function doThings($error, $routes) {
{
    $traduction = array();
    if (!isset($routes[0]) && !$error) {
        return $traduction;  // leave the function without doing anything
    }

    // the rest of the checks and actions, to get the database results

    return $traduction;
}

$traduction = doThings($error, $routes);

// loop over the (maybe empty $traduction variable) to output
?>

【讨论】: