简体   繁体   中英

Php global keyword replacing previous variable value with empty inside function

I have to return some data by calling function, but global variable values we can't access inside function so I used global keyword,however after using global keyword it is making that variable empty,but I have to check condition by using that variable, so how to prevent variable values which is get replaced by global keyword,

    $url = $_SERVER['REQUEST_URI'];
    $url = explode('/', $url);
    $url = end(url);
    $param='some_value';

    active(); // call active unction

    function active() 
    {
        global $url, $param;
      // after using global keyword  $url,$param values replaced with empty by global keyword     
    }

try to use $GLOBALS['url'] , $GLOBALS['param'] , your function returning empty because $url and $param are not declared inside the function, initially $url and $param are not global variable

<?php

    $url = $_SERVER['REQUEST_URI'];
    $url = explode('/', $url);
    $url = end($url); // You miss `$` sign here.

    $param='some_value';

    active(); // call active unction

   function active() {
        global $url, $param;
        echo $url;
        echo $param;            
   }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM