How should I make variable global for all the pages in PHP

后端 未结 3 1438
遥遥无期
遥遥无期 2021-01-29 11:24

Question

I want to make $associate_name and $app_key global variable so I can access them on any page I want. Below is the cod

相关标签:
3条回答
  • 2021-01-29 12:09

    Put those variables you want as session or cookie data. Otherwise, you would have to resort to the global keyword, which is a very bad way of doing things in modern PHP applications.

    It would be like this (for session):

    $_SESSION["myvar"] = <value>;
    

    It's a bit more complicated with cookies, but this should get you going ;)

    0 讨论(0)
  • 2021-01-29 12:09
    • Have all your variables/constants in a separate file may be constants.php Include that constants.php wherever you want to access that variable.
    • Use $_SESSION
    0 讨论(0)
  • 2021-01-29 12:14

    Sessions are your choice in case the value is modified. Otherwise (the value is constant from your configuration and not from user's modification, go for Constants

    0 讨论(0)
提交回复
热议问题