代码之家  ›  专栏  ›  技术社区  ›  GorillaApe

为什么全局在函数中为空?

php
  •  8
  • GorillaApe  · 技术社区  · 15 年前

    我有个奇怪的问题……

    所有页面都只有此代码。 全局$currentpage;为空,我不知道为什么…

    <?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>
    

    问题

    echo $currentPage; prints 1 
            function drawNumbers($from, $to) {
                global $currentPage;
               echo $currentPage; prints nothing
    
    2 回复  |  直到 15 年前
        1
  •  21
  •   zerkms    15 年前

    global

        2
  •  3
  •   Luke    15 年前

    $currentPage global

    drawNumbers( $currentPage, 1, 1 );
    
    function drawNumbers($currentPage, $from, $to) {
    // no need define $currentPage here since it's passed
    }
    
    推荐文章