As stated by David Santos , when reloading the page the values are not saved. They are registered there only at runtime, after which they are removed from memory.
In PHP, there are several ways to maintain values. A two most common for your case would be the use of Session or Cookies.
I would choose to use the session. Sessions maintain a bridge between the client and the server, allowing you to store specific values for each client (browser) that loads your page.
In your case, you could use the variable $_SESSION
(as was quoted in one of the answers, but without any explanation as to what this would be).
See:
// inicia a sessão. Deve ser colocado antes de todo o código de saída para o
//navegador e antes de usar a variável super global '$_SESSION
session_start();
// Se existir o índice 'a', incrementa. Se não, define 0
if (isset($_SESSION['a'])) {
$_SESSION['a']++;
} else {
$_SESSION['a'] = 0;
}
// imprime o valor
var_dump($_SESSION['a']);
The code above will work as follows: $_SESSION
will be stored on the server, with a unique identification for the client (browser, which is registered in a Cookie). In if
we have the isset
that defines if the index 'a'
exists in array
of $_SESSION
. If it exists, it increments the values. But if it does not exist, we define that it will be 0
.
So every time the page is reloaded, the value will also be modified and saved in the session.
See More :
Here is an explanation of the fact that every Session uses Cookies
Sessions are used a lot to login users:
User Counter
In my humble opinion, if you want to do a user counter, as pointed out in some comments, I think the best way is to use a database.