【问题标题】:Echo a variable outside of while loop in PHP在 PHP 中的 while 循环之外回显一个变量
【发布时间】:2015-03-06 13:48:54
【问题描述】:

我有一个 PHP while 循环,我试图在它之外回显 $child_i 的内容,我该怎么做?

<li>
    <div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
        <?php if( have_rows('questions') ): $child_i = 0; ?>
            <!-- Trying to echo $child_i on the line below -->
            <div class="responsive-accordion-panel <?php echo($child_i); ?>">
                <?php while( have_rows('questions') ): the_row(); ?>
                    <div class="question">
                        <h6><?php the_sub_field('question'); ?></h6>    
                        <p><?php the_sub_field('answer'); ?></p>
                    </div>
                <?php $child_i++; endwhile; ?>
            </div>
        <?php endif; //if( get_sub_field('items') ): ?>
    </div>
</li>   

【问题讨论】:

  • $child_i 值将继续存在于循环之外,因此您可以将其输出到那里。 (Endwhile 是 PHP 的替代语法,表示 while 循环在其上方一点开始的结束)
  • @GauravDave 请read docs
  • @GauravDave 它结束了 while 循环...
  • 谢谢大家,endwhile我没用过。现在,大家会解决@egr103的问题吗
  • 使用旧的卷曲 ({}) 比使用 :endwhile 更好。代码编辑器不支持这种语法,这使得调试和阅读变得困难。所有代码编辑器都支持 curlies,这让调试变得轻而易举:-)

标签: php wordpress while-loop advanced-custom-fields


【解决方案1】:

可以将结果放入您自己的数组中,然后通过其编号访问它

<li>
    <div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
        <?php if( have_rows('questions') ): $child_i = 0; ?>
            <!-- Trying to echo $child_i on the line below -->
            <div class="responsive-accordion-panel <?php echo($child_i); ?>">
                <?php while( have_rows('questions') ): the_row(); ?>
                    <div class="question">
                        <h6><?php the_sub_field('question'); ?></h6>    
                        <p><?php the_sub_field('answer'); ?></p>
                    </div>
                <?php $results[] = $child_i; $child_i++; endwhile; ?>
            </div>
        <?php endif; 
print_r($results);
//OR foreach($results as $result){echo $result;} 
//OR echo $result[1].$result[1]; //etc
//if( get_sub_field('items') ): ?>
    </div>
</li>   

【讨论】:

  • 发布前请确保您的代码在正确的代码块中
  • 我能从那个数组中获取总值吗?
  • foreach($results as $result){echo $result;}
【解决方案2】:

最简单的方法是将你的类移动到你的 while() 循环中,然后使用计数器

例子

<li>
    <div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">4 Questions</span></div>
        <?php if( have_rows('questions') ): $child_i = 0; ?>
            <!-- Trying to echo $child_i on the line below -->
                <?php while( have_rows('questions') ): the_row(); ?>

            <div class="responsive-accordion-panel <?php echo ($child_i); ?>">

                    <div class="question">
                        <h6><?php the_sub_field('question'); ?></h6>    
                        <p><?php the_sub_field('answer'); ?></p>
                    </div>

             </div>

                <?php $child_i++; endwhile; ?>
        <?php endif; //if( get_sub_field('items') ): ?>
    </div>
</li>   

【讨论】:

    【解决方案3】:

    尝试插入

    echo '<xmp>'; var_dump($child_i); echo '</xmp>';
    

    而不是

    echo($child_i);
    

    ($child_i 可以包含一个空字符串)

    【讨论】:

    • $child_i 是一个简单的计数器,它返回一个整数,您可以只使用echo。它永远不能是空字符串,OP 在0 处开始了计数器
    猜你喜欢
    • 2012-06-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多