How can PHP be effectively utilized for different types of websites, such as private versus community sites?

PHP can be effectively utilized for different types of websites by using conditional statements to determine the type of site and adjusting the functionality accordingly. For example, for a private site, you can restrict access to certain pages or features based on user authentication, while for a community site, you can implement user profiles, forums, and messaging features.

// Example of restricting access for private site
if(!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Example of implementing user profiles for a community site
$user_id = $_SESSION['user_id'];
$query = "SELECT * FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $query);
$user = mysqli_fetch_assoc($result);

echo "Welcome, " . $user['username'];