Question: This is a simplified version of what I want to accomplish:

In my script I want a static variable x to be incremented every time the submit button is pressed.

```
<?php
function IncX(){
   static $x = 0;
    $x++;
    echo $x;
}
?>

<body>
<form>
<input type="submit" name="submit" class="next btn btn-primary" value="Submit" />
</form>
</body>

```

But it **initializes to x=0** on every page reload after submit.

Answer: 
You're loading the variable afresh every time the page loads, so it's always going to be the same.

The solution is to store it in a session and then increment it there. Include a conditional to create the variable if it doesn't already exist.

```
<?php
  session_start();
  if (!isset($_SESSION['x'])) {
    $x = $_SESSION['x'];
  } else {
    $x = 0;
  }
  $x++;
  echo $x;
  $_SESSION['x'] = $x;
?>

```
Apache doesn't keep track of variables in php scrips between clicks, you would have to store it somewhere, be it the `$_SESSION` or a database.

Moreover, the [static](http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static) keyword doesn't do what you seem to think it does. It would work for successive calls of the function **in a single run of the script**, but not between clicks.

in any event, you can use the ternary operator to achieve this, would you happen to put it in the session. I've also added a check to make sure the variable is actually a viable count number:

```
session_start();
$_SESSION['x'] = isset($_SESSION['x']) && is_int($_SESSION['x'])
    ? $_SESSION['x'] + 1
    : 1;
echo $_SESSION['x'];

```