What are the best practices for setting and passing variables in PHP to ensure consistent functionality in navigation?

When setting and passing variables in PHP for navigation, it is important to use proper naming conventions, sanitize user input to prevent security vulnerabilities, and ensure that the variables are properly scoped and initialized to avoid errors. By following these best practices, you can ensure consistent functionality in navigation throughout your PHP application.

// Set and pass variables for navigation
$page = isset($_GET['page']) ? $_GET['page'] : 'home';

// Sanitize user input
$page = filter_var($page, FILTER_SANITIZE_STRING);

// Ensure proper scoping and initialization
if ($page === 'home') {
    // Display home page content
} elseif ($page === 'about') {
    // Display about page content
} else {
    // Display error page or redirect to default page
}