PHP ECHO 语句中的函数
PHP Function inside of ECHO statement
我在下面的 Wordpress 中使用这个函数:
function wpstudio_doctype() {
$content = '<!DOCTYPE html>' . "\n";
$content .= '<html ' . language_attributes() . '>';
echo apply_filters( 'wpstudio_doctype', $content );
}
问题在于该函数在 <!DOCTYPE html>
标签上方显示 $content
,而不是在 HTML
标签内添加字符串。
我做错了什么?
language_attributes()
没有 return 属性,它呼应它们。
// last line of language_attributes()
echo apply_filters( 'language_attributes', $output );
这意味着它将在您的字符串组装之前显示。您需要使用输出缓冲捕获此值,然后将其附加到您的字符串。
// Not sure if the output buffering conflicts with anything else in WordPress
function wpstudio_doctype() {
ob_start();
language_attributes();
$language_attributes = ob_get_clean();
$content = '<!DOCTYPE html>' . "\n";
$content .= '<html ' . $language_attributes . '>';
echo apply_filters( 'wpstudio_doctype', $content );
}
而不是输出缓冲,只需在 ECHO 语句中使用 get_language_attributes()
。在这种特定情况下:
function wpstudio_doctype() {
$content = '<!DOCTYPE html>' . "\n";
$content .= '<html ' . get_language_attributes() . '>';
echo apply_filters( 'wpstudio_doctype', $content );
}
我在下面的 Wordpress 中使用这个函数:
function wpstudio_doctype() {
$content = '<!DOCTYPE html>' . "\n";
$content .= '<html ' . language_attributes() . '>';
echo apply_filters( 'wpstudio_doctype', $content );
}
问题在于该函数在 <!DOCTYPE html>
标签上方显示 $content
,而不是在 HTML
标签内添加字符串。
我做错了什么?
language_attributes()
没有 return 属性,它呼应它们。
// last line of language_attributes()
echo apply_filters( 'language_attributes', $output );
这意味着它将在您的字符串组装之前显示。您需要使用输出缓冲捕获此值,然后将其附加到您的字符串。
// Not sure if the output buffering conflicts with anything else in WordPress
function wpstudio_doctype() {
ob_start();
language_attributes();
$language_attributes = ob_get_clean();
$content = '<!DOCTYPE html>' . "\n";
$content .= '<html ' . $language_attributes . '>';
echo apply_filters( 'wpstudio_doctype', $content );
}
而不是输出缓冲,只需在 ECHO 语句中使用 get_language_attributes()
。在这种特定情况下:
function wpstudio_doctype() {
$content = '<!DOCTYPE html>' . "\n";
$content .= '<html ' . get_language_attributes() . '>';
echo apply_filters( 'wpstudio_doctype', $content );
}