📖 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
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
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
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
Hint
Use print_r($goodMovies); to show the filtered list.
Practice
(1/5)
1. Why is using modern PHP versions important for developers?
easy
A. Because it offers new features that improve code safety and readability
B. Because older PHP versions run faster on all servers
C. Because modern PHP removes all bugs automatically
D. Because it allows running PHP code without a web server
Solution
Step 1: Understand the benefits of modern PHP
Modern PHP versions introduce features like typed properties and arrow functions that help write safer and clearer code.
Step 2: Evaluate the options
Only Because it offers new features that improve code safety and readability correctly states the importance of new features improving safety and readability. Other options are incorrect or misleading.
Final Answer:
Because it offers new features that improve code safety and readability -> Option A
Quick Check:
Modern PHP = safer, clearer code [OK]
Hint: Focus on benefits of new features in modern PHP [OK]
Common Mistakes:
Thinking older PHP is always faster
Believing modern PHP fixes bugs automatically
Confusing PHP execution environment
2. Which of the following is the correct syntax for an arrow function introduced in modern PHP?
easy
A. function($x) { return $x * 2; }
B. fn($x) => $x * 2;
C. arrow($x) => $x * 2;
D. fn $x => { $x * 2 }
Solution
Step 1: Recall arrow function syntax in PHP
Modern PHP uses fn($params) => expression; for arrow functions.
Step 2: Compare options
fn($x) => $x * 2; matches the correct syntax. function($x) { return $x * 2; } is a traditional anonymous function. Options C and D have invalid syntax.
Final Answer:
fn($x) => $x * 2; -> Option B
Quick Check:
Arrow function syntax = fn(params) => expression [OK]
Hint: Look for 'fn' keyword with arrow => syntax [OK]
Common Mistakes:
Using 'function' keyword for arrow functions
Missing parentheses around parameters
Incorrect placement of braces or arrows
3. What will be the output of this PHP code using typed properties?
class User {
public int $id;
public string $name;
public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}
$user = new User(5, 'Anna');
echo $user->id . ' - ' . $user->name;
D. TypeError because string '100' is passed instead of int
Solution
Step 1: Check constructor parameter type
The constructor expects an int for $price.
Step 2: Analyze argument passed
The argument passed is a string '100', which causes a TypeError in strict typing mode.
Final Answer:
TypeError because string '100' is passed instead of int -> Option D
Quick Check:
Typed property expects int, string given = TypeError [OK]
Hint: Match argument types exactly with typed properties [OK]
Common Mistakes:
Assuming PHP auto-converts string to int in strict mode
Ignoring type declarations in constructor
Confusing syntax errors with type errors
5. You want to update legacy PHP code to use modern features for better security and performance. Which approach best applies modern PHP advantages?
hard
A. Add typed properties, use arrow functions, and enable strict types
B. Remove all type hints and use global variables for speed
C. Keep old code but add comments about modern PHP
D. Use eval() to run code faster and avoid errors
Solution
Step 1: Identify modern PHP features for improvement
Typed properties, arrow functions, and strict typing improve code safety and performance.
Step 2: Evaluate options for best practice
Add typed properties, use arrow functions, and enable strict types applies these features correctly. Options A, B, and D either reduce safety or do not improve code.
Final Answer:
Add typed properties, use arrow functions, and enable strict types -> Option A