13

I get this strange problem....

All the page has this code only. global $currentPage; is null and i dont know why...

<?php
$pager = $_PARAMS["this"];
$pages = 5;
$currentPage = 1;
$tst="ap";
$nearPages = 5;
//Prologic
?>
<div class="pager">
<?php
$nearPagesHalf = ($nearPages - 1) / 2;

drawNumbers(1, 1);
if ($currentPage - $nearPagesHalf <= 0) {

}

drawNumbers($pages, $pages);
?> 

    <?php

    function drawNumbers($from, $to) {
        global $currentPage;



        for ($i = $from; $i <= $to; $i++) {

            echo $currentPage;

            if ($i == $currentPage) {
    ?> <span class="pageNumbers current"><?= $i ?></span>

    <?php
            } else {
    ?>
                <a href="#">
                    <span class="pageNumbers"><?= $i ?></span>
                </a>
<?php
            }
        }
?>
    <?php
    }

    function drawDots($from, $to) {

    }
    ?>

</div>

THE PROBLEM

echo $currentPage; prints 1 
        function drawNumbers($from, $to) {
            global $currentPage;
           echo $currentPage; prints nothing
1
  • 1
    If it is the only code of one page then how this $pager = $_PARAMS["this"]; supposed to work? Commented Nov 2, 2010 at 4:01

3 Answers 3

39

I bet you're executing this code by including this file inside another function.

So you need to mark the first variable occurrence as global too.

Btw, global variables are weird, the more simple and correct way to pass the data to the function is to use function parameters.

Sign up to request clarification or add additional context in comments.

5 Comments

@Parhs: then mark the first variable as global too: global $currentPage = 1;. But anyway - this solution is terrible. As well as eval() is terrible.
I hate globals too but the framework which i am forced to use is composed of many globals!
@BoltClock each page while executing php is evaled!Is that too bad? I wouldnt do something like that but i cant say exactly why it is bad
@Parhs: it is very bad ;-) it is terrible slow, it is difficult to maintain/develop/debug/etc
Global is weird but of course it exists for a reason. I have a case in which i cannot access the global var to pass it through the rest of the system execution using parameters, unless i kick away the design pattern and good practices. The only solution is the global var declaration.
5

The $currentPage defined at the top does not live in global space. Why don't you just pass the $currentPage as the first parameter to the drawNumbers function? It's much cleaner that way:

drawNumbers( $currentPage, 1, 1 );

function drawNumbers($currentPage, $from, $to) {
// no need define $currentPage here since it's passed
}

Comments

0

I had a similar problem, but the solution is not on this page so I am putting here in order to help anyone who might show up looking.

My global variable was an object and it worked fine when calling the global vairable from a method AFTER the variable was defined.

class object{

    function __construct($v1, $v2){

    }

    function methodA(){
        global $a;
        var_dump($a);
    }
}
$a = new object($var1, $var2);
$a->methodA(); //Object...

However, when trying to use the global variable before the constructor has return it does not work.

 class object{

    function __construct($v1, $v2){
        $this->methodA();//NULL
    }

    function methodA(){
        global $a;
        var_dump($a);
    }
}
$a = new object($var1, $var2);

This looks like a dunce-cap situation, but my site is a lot more complex than this simplified scenario. So it took me a while to figure out that I was calling it before it was defined.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.