PHP寻找使用'goto'跳过PHP脚本的最佳方法

时间:2017-11-16 13:44:10

标签: php html exit goto skip

我正在寻找“跳过”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'方法根本不是最优的,所以我正在寻找一种更好的方法来做到这一点因为我很确定已经存在一种方法。

1 个答案:

答案 0 :(得分: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
?>