0
0
PHPprogramming~30 mins

Why modern PHP matters - See It in Action

Choose your learning style9 modes available
Why Modern PHP Matters
📖 Scenario: Imagine you are building a simple website that shows a list of your favorite movies. You want to use PHP to create this list and display it nicely on the page. Using modern PHP features will help you write cleaner, safer, and faster code.
🎯 Goal: You will create a PHP script that stores a list of movies, sets a minimum rating to filter good movies, uses a modern PHP loop to select movies above that rating, and finally prints the filtered list.
📋 What You'll Learn
Create an associative array called $movies with exact movie titles as keys and their ratings as values.
Create a variable called $minRating and set it to 7.
Use a foreach loop with $title and $rating to filter movies with ratings greater than or equal to $minRating into a new array called $goodMovies.
Print the $goodMovies array using print_r.
💡 Why This Matters
🌍 Real World
Filtering and displaying data is a common task in websites and apps, such as showing only good reviews or popular items.
💼 Career
Understanding modern PHP basics like associative arrays, loops, and filtering helps you write clean and efficient code used in many web development jobs.
Progress0 / 4 steps
1
DATA SETUP: Create the movies array
Create an associative array called $movies with these exact entries: 'Inception' => 8, 'Twilight' => 5, 'Interstellar' => 9, 'Cats' => 3, 'The Matrix' => 9.
PHP
Need a hint?

Use square brackets [] to create the array and separate each movie with a comma.

2
CONFIGURATION: Set the minimum rating
Create a variable called $minRating and set it to 7.
PHP
Need a hint?

Just assign the number 7 to the variable $minRating.

3
CORE LOGIC: Filter movies with rating >= minRating
Use a foreach loop with variables $title and $rating to iterate over $movies. Inside the loop, add movies with $rating >= $minRating to a new array called $goodMovies.
PHP
Need a hint?

Start with an empty array $goodMovies = [];. Then use foreach ($movies as $title => $rating) and inside an if check, add the movie to $goodMovies.

4
OUTPUT: Print the filtered movies
Use print_r to display the $goodMovies array.
PHP
Need a hint?

Use print_r($goodMovies); to show the filtered list.