Php 函数放置错误

Php 函数放置错误,php,wordpress,Php,Wordpress,我试图通过将函数放在div中来测试它。我首先创建了一个testfunction,它只返回一个随机字符串。我正试图在resultfunction的div中显示这个。它显示字符串,但它显示在页面顶部而不是在div中?我怎样才能解决这个问题 功能: function testfunction() { return "lol"; } function resultpage() { $output = ''; $output .= '<ul id

我试图通过将函数放在div中来测试它。我首先创建了一个testfunction,它只返回一个随机字符串。我正试图在resultfunction的div中显示这个。它显示字符串,但它显示在页面顶部而不是在div中?我怎样才能解决这个问题

功能:

function testfunction() {
    return "lol";
}


function resultpage() {



    $output = '';


            $output .= '<ul id="nav">';
       $output .= ' <li><a href="#part-1">League of Legends</a></li>';
        $output .= '<li><a href="#part-2">CS:GO</a></li>';
        $output .= '<li><a href="#part-3">Dota2</a></li>';
        $output .= '<li><a href="#part-3">Hearthstone</a></li>';
    $output .= '</ul>';
    $output .= '<div id="content">';
        $output .= '<div id="part-1">';

        echo testfunction();


        $output .= '</div>';
       $output .= ' <div id="part-2">';
       $output .= '     Ma deuxième partie';
       $output .= ' </div>';
       $output .= ' <div id="part-3">';
       $output .= '     Ma troisième partie';
       $output .= ' </div>';
    $output .= '</div>';




return $output;
}
add_shortcode('resultpage', 'resultpage');
function testfunction(){
返回“lol”;
}
函数结果页(){
$output='';
$output.='';
$output.='
  • '; $output.='
  • '; $output.='
  • '; $output.='
  • '; $output.=''; $output.=''; $output.=''; echo testfunction(); $output.=''; $output.=''; $output.='Ma deuxième partie'; $output.=''; $output.=''; $output.='Ma troisième partie'; $output.=''; $output.=''; 返回$output; } 添加_短代码('resultpage','resultpage');
    因为您是回显而不是将其添加到字符串中

        $output .= '<div id="part-1">';
    
        echo testfunction()
    
    $output.='';
    echo testfunction()
    
    将此更改为

       $output .= '<div id="part-1">';
    
       $output .= testfunction()
    
    $output.='';
    $output.=testfunction()
    
    该函数中发生的事情是逐行进行的,一些html被连接到一个变量中,在所有这些过程中,一个函数的回显正在发生(因此它会立即显示),然后您可以继续连接到该变量,在函数完成运行后,您回显$output

    要解决它,只需

    $output .= testfunction();
    
    $output.=testfunction()