I was wondering, is there a way to have a sort of variable that would be 'static' meaning that it would be the same for all users in php ? I know that there is a possibility to have a static variable within a function in php but that is not what I want.
I would like everyone to share an object which I would manipulate depending on the user's demand. Or, another example that is similar to what I want is, is there a way to keep a variable that counts the number of visitors (without using any sort of file or database manipulation). That variable would be incremented every time a user come to my page.
Cheers !
Without using a file or database, I believe you could do this using something like APC.
$var = 1;
$key = 'myVariable';
apc_store($key, $var);
echo apc_fetch($key); // 1
If you want to increment it, you can use apc_inc()
echo apc_inc($key); // 2
However, this variable won't be preserved if the cache is cleared (which happens when it fills up or the server is restarted).
Check out semaphores and shared memory and how they work in PHP. With a shared memory variable different processes (users) can use the same memory space and use the same variables. Here's a link to the PHP documentation to get you started:
http://www.php.net/manual/en/function.shm-get-var.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With