【问题标题】:wordpress functions.phpwordpress 函数.php
【发布时间】:2010-04-09 17:00:12
【问题描述】:

我在使用带变量的 functions.php 文件时遇到问题

        $prev_dept = 0;
        $comment_count = 0;
        $comment_index = 0;
        function setCommentCount($size){
            $comment_count = $size;
        }        
        function flowhub_comment($comment, $args, $depth) {
            $comment_index ++;            

            if($depth > 1) {
                $line = true;
            }
            echo '$prev_dept:' . $prev_dept.'<br>';
        }

我无法访问 $comment_index,因此我无法从函数中设置或获取它。我应该怎么做才能解决这个问题?

真诚的

【问题讨论】:

    标签: php wordpress scope


    【解决方案1】:

    $comment_index不在功能范围内,需要使用globalMore details on scoping in PHP.

    【讨论】:

    • 谢谢,我觉得这很奇怪,因为通常我使用私有、公共等...而在其他语言中,当您在课堂上工作时,可以访问。谢谢,我会尽可能正确地标记你的答案(11 分钟)
    • 如果您对类感到满意,您可以在 PHP 中使用它们。然后你可以在类的范围内拥有变量,然后可以从类方法访问它。 us.php.net/manual/en/language.oop5.basic.php
    【解决方案2】:

    functions.php 的工作方式不仅仅是简单的包含,尝试 GLOBAL 可能会有所帮助。

    function setCommentCount($size){
        global $comment_count;
        $comment_count = $size;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2015-05-01
      • 1970-01-01
      • 2017-10-25
      • 2014-06-04
      • 2016-03-12
      • 1970-01-01
      相关资源
      最近更新 更多