views:

52

answers:

2

Hi

I'm having issues with the functions.php file with variables

        $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>';
        }

I can't access $comment_index so I can't set nor get it from within a function. What should I do to fix this?

Yours truthfully

+2  A: 

$comment_index is not within the scope of the functions, you need to use global. More details on scoping in PHP.

unholysampler
thanks, I thought it was strange because normally I use private, public etc... while in other languages this is possible to get access when you're working in classes. Thanks I'll mark your answer right when possible (11min)
Ayrton
If you are comfortable with classes, you can use them in PHP. And then you could have the variables within the scope of the class, which would then make it accessible from class methods. http://us.php.net/manual/en/language.oop5.basic.php
unholysampler
+1  A: 

The way functions.php works is not just a plain include, try GLOBAL it might help.

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