如何正确显示函数结果


How to show function result correctly

我需要写一个简单的脚本,可以发送电子邮件或短信。我需要得到函数结果,并将其分配给一些变量。例如$message=message();并在发送SMS的脚本中获得$message。

这是我的代码示例:

function message() { $argsvsq = array( 'date_query' => array(
    array(
        'year' => date( 'Y' ),
        'week' => date( 'W' ),
    ),
),
            'author__in' => array($_GET["sendtoid"]),
            'post_type' => 'ocinky',
            'meta_key' => 'wpcf-date',
            'orderby' => 'meta_value',
            'order' => 'DESC',
            'posts_per_page' => -1
             );
        $looper = new WP_Query( $argsvsq );
        // Start the Loop.
        while ( $looper->have_posts() ) : $looper->the_post(); $urok = types_render_field("urok", array("output"=>"HTML")); echo $urok; endwhile;
        }

这是我需要显示结果的行

$text_sms = iconv('windows-1251', 'utf-8', message() );

请帮助正确获取函数message()的结果。。。非常感谢!

iconv将字符串作为第三个参数。您的message()函数不返回任何内容。

你可以简单地使用outputbuffering来解决这个问题:

function message() { $argsvsq = array( 'date_query' => array(
    array(
        'year' => date( 'Y' ),
        'week' => date( 'W' ),
    ),
),
    'author__in' => array($_GET["sendtoid"]),
    'post_type' => 'ocinky',
    'meta_key' => 'wpcf-date',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'posts_per_page' => -1
);
    ob_start();
    $looper = new WP_Query( $argsvsq );
    // Start the Loop.
    while ( $looper->have_posts() ) : $looper->the_post(); 
        $urok = types_render_field("urok", array("output"=>"HTML")); 
        echo $urok; 
    endwhile;
    return ob_get_clean();
}

可能可以只附加到字符串并返回字符串,而不使用输出缓冲:

function message() { $argsvsq = array( 'date_query' => array(
    array(
        'year' => date( 'Y' ),
        'week' => date( 'W' ),
    ),
),
    'author__in' => array($_GET["sendtoid"]),
    'post_type' => 'ocinky',
    'meta_key' => 'wpcf-date',
    'orderby' => 'meta_value',
    'order' => 'DESC',
    'posts_per_page' => -1
);
    $return = ''
    $looper = new WP_Query( $argsvsq );
    // Start the Loop.
    while ( $looper->have_posts() ) : $looper->the_post(); 
        $urok = types_render_field("urok", array("output"=>"HTML")); 
        $return .= $urok; 
    endwhile;
    return $return;
}

但我不知道所有这些函数调用都在做什么(如果它们有任何回声,则需要使用第一个方法