Default parameter values
📖 Scenario: Imagine you are creating a simple greeting system for a website. Sometimes visitors provide their name, and sometimes they don't. You want to greet them politely either way.
🎯 Goal: Build a PHP function that greets a user by name if given, or uses a default greeting if no name is provided.
📋 What You'll Learn
Create a function named
greet with one parameter $name that has a default value.Set the default value of
$name to 'Guest'.Inside the function, return a greeting string like
'Hello, NAME!' where NAME is the value of $name.Call the function twice: once without any argument, and once with the argument
'Alice'.Print the results of both function calls.
💡 Why This Matters
🌍 Real World
Default parameter values help make functions flexible and easier to use when some information is optional.
💼 Career
Understanding default parameters is important for writing clean, reusable code in PHP and many other programming languages.
Progress0 / 4 steps