How can the code snippet provided be optimized to prevent the click counter from resetting to 0?

The issue with the current code snippet is that the click counter is being reset to 0 every time the page is loaded because the variable holding the click count is not being persisted across page loads. To prevent the click counter from resetting to 0, we can use sessions to store and retrieve the click count value.

<?php
session_start();

// Initialize click count to 0 if it doesn't exist in the session
if(!isset($_SESSION['click_count'])) {
    $_SESSION['click_count'] = 0;
}

// Increment click count when button is clicked
if(isset($_POST['click_button'])) {
    $_SESSION['click_count']++;
}

// Display click count
echo "Click count: " . $_SESSION['click_count'];
?>

<form method="post">
    <button type="submit" name="click_button">Click me!</button>
</form>