PHP:我能在变量中放置一个if语句吗?

3

我对PHP还是有些初学者,如果这是一个愚蠢的问题,请原谅 =)

我的目标是在WordPress博客中,在我的RSS订阅中插入具有多个值(“成分”)的自定义字段。(我还有其他不是食谱的文章,这就是为什么“成分”和“说明”标题位于if语句内部的原因。)以下是到目前为止我的全部代码:

    <?php
    function insertIngredients($content) {
     /* get ingredients into variable $recipeStuff */
     $recipeStuff = 
   if ($ingredients = get_post_custom_values('ingredients')) {
    echo '<h3>Ingredients</h3><ul id="ingredients">';
    foreach ( $ingredients as $key => $value ) {
   echo '<li>';
   echo $value;
   echo '</li>';
    } 
    echo '</ul><h3>Instructions</h3>';
   }
     /* add before content */
        $content = $recipeStuff . $content;
        return $content;
    }
    /* Do it! */
    add_filter('the_excerpt_rss', 'insertIngredients');
    add_filter('the_content_rss', 'insertIngredients');
    ?>

但是我遇到了“意外的IF”错误,所以我猜我不能把所有内容放在$recipeStuff变量里=)我想不出其他方法把它放进去。
(如果有影响的话,IF语句正是我在页面上使用的帖子,它完美地工作!)
非常感谢您提前的任何帮助!=D
更新!
这是我现在代码中的内容:
function insertIngredients($content) {
/* test for presence of ingredients & set variables */
if ($ingredients = get_post_custom_values('ingredients')) {
    $heading1 = '<h3>Ingredients</h3><ul id="ingredients">';
    foreach ( $ingredients as $key => $value ) {
        $ings = '<li>' . $value . '</li>';
    }
    $heading2 = '</ul><h3>Instructions</h3>';
}
/* if no ingredients, variables are empty */
else { $heading1=''; $ings=''; $heading2=''; }

$recipeStuff = $heading1 . $ings . $heading2 ;

/* add before content */
    $content = $recipeStuff . $content;
    return $content;
}
/* Do it! */
add_filter('the_excerpt_rss', 'insertIngredients');
add_filter('the_content_rss', 'insertIngredients');

我不再收到错误信息,但是配料没有显示在rss源中。我不确定是否代码还有问题,或者需要一段时间才能生效(尽管我不知道为什么会这样)。如果使用FeedBurner,可能会有所不同。
非常感谢大家的回答。我会尝试几种不同的方法,并稍后更新。谢谢!=)

针对您的UPDATE代码,请尝试以下写法:if ($ingredients == get_post_custom_values('ingredients')) { - Phill Pafford
6个回答

8
为什么不反其道而行之?
if (condition) {
$recipeStuff = '';
}
else
{
$recipeStuff = '';
}

兄弟!我为什么没想到那个?!谢谢 =)))) - KeriLynn
出于好奇,PHP是否支持 $variable = (condition)? true : false ; ? - joslinm
1
是的,在上面的代码中有一些循环,但是内联if可能不会很好地完成。 - Aurel Bílý

6

Even though this question is quite old, IMO the best answer is missing, the ternary operator:

<?php
    $recipeStuff = (  HERE CHECK FOR INGREDIENTS  ) ? SOMETHING : SOMETHING ELSE ;
?>

$recipeStuff will get the value SOMETHING if the check is true, otherwise it will get SOMETHING ELSE


2
不,事实并非如此。 应该是这样的(忽略函数头和其他内容):
<?php
if (  HERE CHECK FOR INGREDIENTS  ){
    $recipeStuff = SOMETHING ;
} else {
    $recipeStuff = SOMETHING ELSE ;
}
?>

因此,您不能像内联函数一样使用if(以这种形式)。有一个内联替代方案,但在其中使用循环很困难。Echo将始终向输出缓冲区输出某些内容。即使$myVar = echo 'something';也无法使用echo。只需在条件内执行分配即可。

1
没有 IF 作用域。你想说什么意思,"这必须在 if 语句之外定义,这样即使之后也可以访问(作用域)"? - Dennis Haarbrink

2
最简单的方法,也是保持简单的方法是通过:

if (!yourcondition) {//if its not true
$recipeStuff = '';
}
else{//else if it is true
$recipeStuff = '';
}

1

我认为这些是有问题的代码行

$recipeStuff = 
   if ($ingredients = get_post_custom_values('ingredients')) {

返回翻译后的文本:

这个做什么?或它应该有什么值?

$recipeStuff = 

试试这个替代方案

if ($ingredients == get_post_custom_values('ingredients')) {

在比较值时,应该使用双等号==或三等号===,而使用单个等号是用来设置值的。

$one = 1; // assigns the value 1 to the variable $one

// Compares the values
if($one == 1) {
   echo "True<br />";
} else {
   echo "False<br />";
}

请阅读比较运算符页面


1

显然,这不是正确的PHP语法。你需要一个变量来连接字符串(li等),并返回它。

你应该将if移动到$recipeStuff变量外面。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接