Changing a global variable from inside a function PHP

后端 未结 4 1440
难免孤独
难免孤独 2020-12-01 05:15

I am trying to change a variable that is outside of a function, from within a function. Because if the date that the function is checking is over a certain amount I need it

4条回答
  •  旧巷少年郎
    2020-12-01 05:59

    Just use the global keyword like so:

    $var = "01-01-10";
    function checkdate(){
         global $var;
    
         if("Condition"){
                $var = "01-01-11";
          }
    }
    

    Any reference to that variable will be to the global one then.

提交回复
热议问题