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;
?>

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

?>

```