What is the best practice for updating and storing a variable's value when a button is clicked in PHP?

When a button is clicked in PHP, the best practice for updating and storing a variable's value is to use a form submission method such as POST to send the updated value to the server. The server-side script can then process the submitted data and update the variable accordingly. This ensures that the variable's value is updated securely and consistently.

<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Update the variable's value based on the submitted data
    $newVariableValue = $_POST['new_value'];
    
    // Store the updated value in a session or database
    // For example, storing in a session:
    session_start();
    $_SESSION['variable'] = $newVariableValue;
}
?>

<form method="post">
    <input type="text" name="new_value">
    <button type="submit">Update Variable</button>
</form>