0
0
PHPprogramming~15 mins

Default parameter values in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
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
1
Create the greet function with a default parameter
Write a PHP function named greet that has one parameter called $name with a default value of 'Guest'. Inside the function, return the string 'Hello, ' . $name . '!'.
PHP
Need a hint?

Use = 'Guest' after the parameter $name to set the default value.

2
Call the greet function without arguments
Call the greet function without any arguments and store the result in a variable called $greeting1.
PHP
Need a hint?

Call greet() without any parameters to use the default value.

3
Call the greet function with the argument 'Alice'
Call the greet function with the argument 'Alice' and store the result in a variable called $greeting2.
PHP
Need a hint?

Pass 'Alice' as the argument to greet to override the default.

4
Print both greetings
Print the variables $greeting1 and $greeting2 each on a new line using echo.
PHP
Need a hint?

Use echo to print each greeting followed by a newline character "\n".