How can foreach() be utilized to efficiently store form data in session variables in PHP?

When storing form data in session variables in PHP, the foreach() function can be used to iterate through the form data array and store each key-value pair in session variables. This allows for a more efficient way of handling multiple form inputs without explicitly writing a separate line of code for each input.

// Start the session
session_start();

// Assume form data is submitted using POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Iterate through the form data array and store each key-value pair in session variables
    foreach ($_POST as $key => $value) {
        $_SESSION[$key] = $value;
    }
}