What is the best way to display different messages based on whether a user is logged in or not in a PHP application?

To display different messages based on whether a user is logged in or not in a PHP application, you can use conditional statements to check the user's login status. You can set a session variable upon login and then check this variable to determine which message to display. If the user is logged in, display a welcome message; if not, display a message prompting the user to log in.

<?php
session_start();

if(isset($_SESSION['user_id'])) {
    echo "Welcome back, user!";
} else {
    echo "Please log in to access this content.";
}
?>