Search code examples
phpsuperglobals

$GLOBALS variable weird behavior


I am trying to understand superglobals in PHP. This is my script:

<?php

$n = $GLOBALS['abc'] + 1;
$GLOBALS['abc'] = $n;
echo $GLOBALS['abc'];

?>

When I access this file from browser, only 1 gets printed every time. Why isn't the value getting incremented each time I reload the page? Why is the value of $GLOBALS['abc'] getting lost?


Solution

  • You need to store the value in a file / database / cache for persistence. Store and read on next request. PHP is stateless means every request is independent in itself.