Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果meta没有值/为空,则输出0的最短路径_Php_Wordpress_If Statement_Is Empty - Fatal编程技术网

Php 如果meta没有值/为空,则输出0的最短路径

Php 如果meta没有值/为空,则输出0的最短路径,php,wordpress,if-statement,is-empty,Php,Wordpress,If Statement,Is Empty,希望标题有意义,我有几个元字段,如果它们是空的,我想输出0 我目前正在用以下命令输出值: <?php meta('some-field'); ?> 编辑:上述代码将回显值,例如使用三元运算符。 在PHP 5.3之后: echo meta('some-field') ? : 0; 在PHP 5.3之前: echo meta('some-field') ? meta('some-field') : 0; 假设meta() function callback($buffer) {

希望标题有意义,我有几个元字段,如果它们是空的,我想输出
0

我目前正在用以下命令输出值:

<?php meta('some-field'); ?>


编辑:上述代码将回显值

,例如使用三元运算符。 在PHP 5.3之后:

echo meta('some-field') ? : 0;
在PHP 5.3之前:

echo meta('some-field') ? meta('some-field') : 0;
假设
meta()

function callback($buffer) {
    // check if buffer is empty, else return 0
    return (!empty($buffer)) ? $buffer : 0;
}

// turn output buffering on. the output is stored in an internal buffer. 
// The callback function will be called when the output buffer is flushed
ob_start('callback');

meta('some-field');

// Flush the output buffer
ob_end_flush();
// Check if meta return value is empty, print meta value else print 0
echo ( !empty(meta('some-field')) ) ? meta('some-field') : 0;
工作示例:


假设
meta()

function callback($buffer) {
    // check if buffer is empty, else return 0
    return (!empty($buffer)) ? $buffer : 0;
}

// turn output buffering on. the output is stored in an internal buffer. 
// The callback function will be called when the output buffer is flushed
ob_start('callback');

meta('some-field');

// Flush the output buffer
ob_end_flush();
// Check if meta return value is empty, print meta value else print 0
echo ( !empty(meta('some-field')) ) ? meta('some-field') : 0;
您可以在自己的自定义函数中包装
meta()

function myMeta($key, $default = '0') {
   $meta = meta($key);
   return empty($meta) ? $default : $meta;
}
并使用它而不是元:

myMeta('some-field');
myMeta('some-field', 'other-default-value');

也许这有助于您不需要empty()函数调用,因为null和空字符串将转换为false。回音很好@格里菲尔德我知道。。。但是对于一个新手来说可能更容易理解,我得到了一个致命的错误:在检查我的代码的第1行代码中不能在写上下文中使用函数返回值,因为它似乎会破坏页面,不知道该做什么。@AlexSzymanowski在
ob_start
ob_end_flush
echo meta('some-field')之间的代码中有一个错误:0; 即使元有一个值,似乎也会输出额外的0,因此word的值现在是word0,4000是40000,无法让其他建议真正起作用。您的函数返回该值,并且不尝试打印它?不确定您的意思?您的元函数以return$something结束,而不是echo$something结束?可以肯定的是。:)是的,我相信它与我使用的代码相呼应,我将数据准确地输出到js谷歌图表中。