How can PHP be used to dynamically generate menu items based on user actions, such as registration or login?

To dynamically generate menu items based on user actions such as registration or login, you can use PHP to check the user's authentication status and display the appropriate menu items accordingly. This can be achieved by setting session variables upon user registration or login, and then using conditional statements in your PHP code to determine which menu items to display based on the user's authentication status.

<?php
session_start();

// Check if user is logged in
if(isset($_SESSION['user_id'])) {
    // Display menu items for logged in users
    echo '<a href="profile.php">Profile</a>';
    echo '<a href="logout.php">Logout</a>';
} else {
    // Display menu items for guests
    echo '<a href="login.php">Login</a>';
    echo '<a href="register.php">Register</a>';
}
?>