phpsuperglobals

Can we create a Superglobal Boolean variable in php that can be manipulated from any php script?


I have a php script that runs every minute to check something. What I need is, if the script finds what it is checking for then set a Boolean to true so that on subsequent run of the script it can know that earlier run has set the Boolean to true. As far as I understand $GLOBALS can only be accessed from the script. Currently I am using database to achieve this. Is there a better way ?


Solution

  • Solution 1 - Environment Variables

    What you might need is something which is present system-wide. the first thing that came to my mind is environment variables. You can read and write them as needed. you can check them at any point, but it will be visible to other applications as well.

    The problem with session is that sessions are closely related to the requests. basically, the session gets started when the request arrived, and they are supposed to get destroyed when the request is over (in the sense that like the user has logged in or logged out)

    But the question is about a cron job, which is basically a cli program. which don't have any requests associated with it.

    Solution 2 - File Based

    By having a file-based check, you can create a file, and add the flag inside file. or the file itself can be the flag.

    Solution 3 - By using a datastore

    If you have something like a Redis system running as part of the application, you can use this to store the flag/condition.