Null coalescing in conditions
📖 Scenario: You are building a simple PHP script to check user input from a form. Sometimes the input might be missing or empty. You want to use the null coalescing operator to provide a default value when the input is not set.
🎯 Goal: Create a PHP script that uses the null coalescing operator ?? in a condition to check if a user input variable is set. If it is not set, use a default value. Then print a message based on the value.
📋 What You'll Learn
Create a variable
$input with a specific valueCreate a variable
$default with a default stringUse the null coalescing operator
?? in an if condition to check $input or use $defaultPrint the message showing which value was used
💡 Why This Matters
🌍 Real World
Web forms often have optional fields. Using null coalescing helps handle missing inputs gracefully.
💼 Career
PHP developers use null coalescing to write cleaner, safer code when dealing with user input or data that might be missing.
Progress0 / 4 steps