0
0
PHPprogramming~15 mins

Named arguments in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Named Arguments in PHP Functions
📖 Scenario: Imagine you are creating a simple program to calculate the price of a product after applying a discount and adding tax. You want to make your function calls clear and easy to read by using named arguments.
🎯 Goal: Build a PHP program that defines a function with multiple parameters and calls it using named arguments to calculate the final price of a product.
📋 What You'll Learn
Define a function called calculatePrice with parameters price, discount, and tax.
Create variables with exact values for price, discount, and tax.
Call the calculatePrice function using named arguments in any order.
Print the final calculated price.
💡 Why This Matters
🌍 Real World
Named arguments make your code easier to read and maintain, especially when functions have many parameters or optional values.
💼 Career
Understanding named arguments helps you write clearer PHP code, which is valuable in web development jobs where code readability and maintenance are important.
Progress0 / 4 steps
1
Create variables for price, discount, and tax
Create three variables called price, discount, and tax with the exact values 100, 0.2, and 0.1 respectively.
PHP
Need a hint?

Use the $ sign to create variables and assign the exact values given.

2
Define the calculatePrice function
Define a function called calculatePrice that takes three parameters: price, discount, and tax. The function should return the final price calculated as price - (price * discount) + (price * tax).
PHP
Need a hint?

Use the function keyword and return the calculation inside the function.

3
Call calculatePrice using named arguments
Call the calculatePrice function using named arguments with the variables discount, tax, and price in this exact order. Store the result in a variable called finalPrice.
PHP
Need a hint?

Use the syntax parameterName: value to pass named arguments in the function call.

4
Print the final price
Print the value of the variable finalPrice using echo.
PHP
Need a hint?

Use echo $finalPrice; to display the result.