开发者

Code review - PHP syntax error unexpected $end [duplicate]

开发者 https://www.devze.com 2022-12-25 05:35 出处:网络
This question already has answers here: PHP parse/syntax errors; and how to solve them (20 answers) Closed 4 years ago.
This question already has answers here: PHP parse/syntax errors; and how to solve them (20 answers) Closed 4 years ago.

Hey guys! I keep getting a syntax error (unexpected $end), and I've isolated it to this chunk of code. I can't for the life of me see any closure issues. It's probably something obvious but I'm going nutty trying to find it. Would appreciate an additional set of eyes.

function generate_pagination( $base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE )
    {
        global $lang;
        if ( $num_items == 0 )
        {
        }
        else
        {
            $total_pages = ceil( $num_items / $per_page );
            if ( $total_pages == 1 )
            {
                return "";
            }
            $on_page = floor( $start_item / $per_page ) + 1;
            $page_string = "";
            if ( 8 < $total_pages )
            {
                $init_page_max = 2 < $total_pages ? 2 : $total_pages;
                $i = 1;
                for ( ; $i < $init_page_max + 1; ++$i )
                {
                    $page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                    if ( $i < $init_page_max )
                    {
                        $page_string .= ", ";
                    }
                }
                if ( 2 < $total_pages )
                {
                    if ( 1 < $on_page && $on_page < $total_pages )
                    {
                        $page_string .= 4 < $on_page ? " ... " : ", ";
                        $init_page_min = 3 < $on_page ? $on_page : 4;
                        $init_page_max = $on_page < $total_开发者_如何学Pythonpages - 3 ? $on_page : $total_pages - 3;
                        $i = $init_page_min - 1;
                        for ( ; $i < $init_page_max + 2; ++$i )
                        {
                            $page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                            if ( $i < $init_page_max + 1 )
                            {
                                $page_string .= ", ";
                            }
                        }
                        $page_string .= $on_page < $total_pages - 3 ? " ... " : ", ";
                    }
                    else
                    {
                        $page_string .= " ... ";
                    }
                    $i = $total_pages - 1;
                    for ( ; $i < $total_pages + 1; ++$i )
                    {
                        $page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                        if ( $i < $total_pages )
                        {
                            $page_string .= ", ";
                        }
                    }
                    continue;
                }
            }
            else
            {
                do
                {
                    $i = 1;
                    for ( ; $i < $total_pages + 1; ++$i)

                {
                    $page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
                    if ( $i < $total_pages )
                    {
                        $page_string .= ", ";
                        break;
                    }
                } 
            } while (0);
            if ( 1 < $on_page )
            {
                $page_string = " <font size='2'><a href=\"".$base_url."&amp;offset=".( $on_page - 2 ) * $per_page."\">"."&laquo;"."</a></font>&nbsp;&nbsp;".$page_string;
            }
            if ( $on_page < $total_pages )
            {
                $page_string .= "&nbsp;&nbsp;<font size='2'><a href=\"".$base_url."&amp;offset=".$on_page * $per_page."\">"."&raquo;"."</a></font>";
            }
            $page_string = "Pages ({$total_pages}):"." ".$page_string;
            return $page_string;
        }
    }


The code around while (0); followed by if (1 < $on_page) looks wrong to me. At a glance it looks like the else is not closed. Have you tried php -l (lint) on your code?


Put a } in the last line of your code. You are just not closing the body of your function.

Of course it could also be that you are missing a closing bracket somewhere in between.
But as we don't know how the code works (i.e. we don't know when which block should be executed), you should intend it correctly from the beginning and look over it again.


You need a } at the end of the file to close the function body.


You are missing a closing } for the do statement starting on line 65.

Replace that statement with the following text to repair it.

do
{
    $i = 1;
    for ( ; $i < $total_pages + 1; ++$i)
    {
        $page_string .= $i == $on_page ? "<font face='verdana' size='2'><b>[{$i}]</b></font>" : "<a href=\"".$base_url."&amp;offset=".( $i - 1 ) * $per_page."\">{$i}</a>";
        if ( $i < $total_pages )
        {
            $page_string .= ", ";
            break;
        }
    }
} while (0);


your indentation inside the do ... while(0) code is off. If you look at the for loop, you'll notice that everything inside it is one indentation less than it should be.

Add this indentation, and you'll see that what the others mentioned (extra } at the end of the file) is the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号