如何修复语法错误,php 中的意外 T_IF 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10860371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:14:38  来源:igfitidea点击:

How to fix syntax error, unexpected T_IF error in php?

phpsyntax

提问by STEEL

Parse error: syntax error, unexpected T_IF in view.php on line 27

解析错误:语法错误,第 27 行 view.php 中出现意外的 T_IF

I really dont see any problem in my code, why this is happening, please help. Im a beginner in PHP

我真的没有在我的代码中看到任何问题,为什么会发生这种情况,请帮忙。我是 PHP 初学者

Where my LINE 21 is >> if(isset($_GET['page']) && is_numeric($_GET['page']))

我的第 21行在哪里 >> if(isset($_GET['page']) && is_numeric($_GET['page']))

if ($result = $mysqli->query("SELECT * FROM players ORDER BY id"))
{
    if ($result->num_rows > 0)
    {
        $total_result = $result->num_rows;
        $total_pages = ceil($total_result / $per_page)

        if(isset($_GET['page']) && is_numeric($_GET['page']))
        {
            $show_page = $_GET['page'];

            if ($show_page > 0 && $show_page <= $total_pages)
            {
                $start = ($show_page - 1) * $per_page;
                $end = $start + $per_page;
            }
            else
            {
                $start = 0;
                $end = $per_page;
            }               

        }
        else
        {
            $start = 0;
            $end = $per_page;
        }


        //display paginations
        echo "<p> View pages: ";
        for ($i=1; $i < $total_pages; $i++)
        { 
            if (isset($_GET['page']) && $_GET['page'] == $i)
            {
                echo  $i . " ";
            }
            else
            {
                echo "<a href='view-pag.php?$i'>" . $i . "</a> | ";
            }

        }
        echo "</p>";

    }
    else
    {
        echo "No result to display.";
    }

}
else
{
    echo "Error: " . $mysqli->error;
}

回答by Ja?ck

PHP parser errors take some getting used to; if it complains about an unexpected 'something' at line X, look at line X-1 first. In this case it will not tell you that you forgot a semi-colon at the end of the previous line , instead it will complain about the ifthat comes next.

PHP 解析器错误需要一些时间来适应;如果它在 X 行抱怨意外的“某事”,请先查看 X-1 行。在这种情况下,它不会告诉您您忘记了前一行末尾的分号,而是会抱怨if接下来的分号。

You'll get used to it :)

你会习惯的:)

回答by Robbie

add semi-colon the line before:

在该行之前添加分号:

$total_pages = ceil($total_result / $per_page);

回答by Moyed Ansari

Here is the issue

这是问题

  $total_result = $result->num_rows;

try this

尝试这个

<?php
if ($result = $mysqli->query("SELECT * FROM players ORDER BY id"))
{
    if ($result->num_rows > 0)
    {
        $total_result = $result->num_rows;
        $total_pages = ceil($total_result / $per_page);

        if(isset($_GET['page']) && is_numeric($_GET['page']))
        {
            $show_page = $_GET['page'];

            if ($show_page > 0 && $show_page <= $total_pages)
            {
                $start = ($show_page - 1) * $per_page;
                $end = $start + $per_page;
            }
            else
            {
                $start = 0;
                $end = $per_page;
            }               

        }
        else
        {
            $start = 0;
            $end = $per_page;
        }


        //display paginations
        echo "<p> View pages: ";
        for ($i=1; $i < $total_pages; $i++)
        { 
            if (isset($_GET['page']) && $_GET['page'] == $i)
            {
                echo  $i . " ";
            }
            else
            {
                echo "<a href='view-pag.php?$i'>" . $i . "</a> | ";
            }

        }
        echo "</p>";

    }
    else
    {
        echo "No result to display.";
    }

}
else
{
    echo "Error: " . $mysqli->error;
}


?>