PHP函数在HTML标记之前打印

问题描述:

I have quite the peculiar problem... I've got a working PHP function, as shown below, which fetches a users' score and outputs some information based on that score (it's not optimized yet).

Unfortunately, for whatever reason, when I put it in my code, it's not actually being rendered inside the HTML markup/position I want it to be rendered in - instead, it's being rendered at the top of the page, and a search has not yielded any results.

The code simplified is:

<?php
require('displayCodes.php');
displayCodesMain(1);
?>

The displayCodesMain code:

<?php
function displayCodesMain($page){ 
    if($page == 1){?>
<table class="friendcodes">
    <tr>
        <th>Username</th>
        <th>Friend Code</th>
        <th>Affinity</th>
        <th>Level</th>
        <th>Rating</th>
    </tr>
<?php
        $plisting = file_get_contents('plist.txt');
        $plist = explode(' ', $plisting, 4);
        require_once('vote.php');
        array_pop($plist);
        foreach($plist as $item) {
            $item = explode(':', $item);
            echo '<tr class="fc_premium">';
            $item[3] = substr($item[3], 0, 3) . '-' . substr($item[3], 3, 7);
            $item[1] = str_replace('&', ' ', $item[1]);
            echo '<td>' . $item[1] . '</td><td>' . $item[3] . '</td><td style="text-transform:capitalize;">' . $item[4] . '</td><td>' . $item[5] . '</td><td> '. displayRating(1)
.' </td>';
            echo '</tr>'; 
        }    
        echo '</table>';
    }
} // End of the function
?>

The actual function itself:

<?php
function displayRating($id){
    if(isset($id)){
        require_once('medoo.min.php');
        // Start up the D.B. Connection
        // Removed some DB setup code here...
        $phrase = '<div class="bc"><div data-hint="%1$s" class="hint--right vote %2$s"></div></div>';
        if($vScore == 0){
            printf($phrase, 'Neutral Score', 'v_equal');
            return;
        } elseif ($vScore > 0){
            // Positive Here
            switch($vScore){
                case 1:
                    printf($phrase, '1 Like', 'v_p1 hint--success');
                    break;
                case 2:
                    printf($phrase, '2 Likes', 'v_p2 hint--success');
                    break;
                case 3:
                    printf($phrase, '3 Likes', 'v_p3 hint--success');
                    break;
                case 4:
                    printf($phrase, '4 Likes', 'v_p4 hint--success');
                    break;
                case 5:
                    printf($phrase, '5 Likes', 'v_p5 hint--success');
                    break;
                case 6:
                    printf($phrase, '6 Likes', 'v_p6 hint--success');
                    break;
                case 7:
                    printf($phrase, '7 Likes', 'v_p7 hint--success');
                    break;
                default:
                    printf($phrase, $vScore . ' Likes', 'v_p7 hint-success');
                    break;
            }
        } elseif ($vScore < 0){
            // Negative Here
            switch($vScore){
                case -1:
                    printf($phrase, '1 Dislike', 'v_m1 hint--error');
                    break;
                default:
                    if($vScore < -7){ $vClass = 7; } else { $vClass = abs($vScore); }
                    printf($phrase, abs($vScore) . ' Dislikes', 'v_m' . $vClass . ' hint--error');
            }
        }
    } else {
        return;
    }
}
?>

I've even put in a static variable for the function in the first foreach statement, yet, I still have no luck in getting it to display in the proper place.

I am currently unable to gain access to my php.ini, and phpinfo() appears to be disabled for security reasons. I will get back to you guys after I find out my info from my host.

In your main program, you're using echo to output the return value of displayRating(), but it doesn't return anything. Instead, displayRating() is also using echo. This is why your output appears out of order.

Turn this:

echo /* ... */ '</td><td> '. displayRating(1) .' </td>';

into:

echo /* ... */ '</td><td> ';
displayRating(1);
echo ' </td>';

or build and return a string from displayRating().