0
0
PHPprogramming~15 mins

Parameters and arguments in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Parameters and Arguments in PHP
📖 Scenario: Imagine you are creating a simple calculator that adds two numbers. You want to write a function that takes two numbers and returns their sum.
🎯 Goal: Build a PHP function that uses parameters to accept two numbers and returns their sum. Then call the function with arguments and display the result.
📋 What You'll Learn
Create a function named addNumbers with two parameters: $num1 and $num2
Call the function addNumbers with two arguments: 5 and 7
Store the result of the function call in a variable named $result
Print the value of $result
💡 Why This Matters
🌍 Real World
Functions with parameters and arguments are used everywhere in programming to reuse code and pass information.
💼 Career
Understanding how to define and use functions with parameters is essential for any programming job, including web development with PHP.
Progress0 / 4 steps
1
Create a function with parameters
Write a function named addNumbers that takes two parameters: $num1 and $num2. Inside the function, return the sum of $num1 and $num2.
PHP
Need a hint?

Use the function keyword to create a function. Parameters go inside the parentheses.

2
Call the function with arguments
Call the function addNumbers with the arguments 5 and 7. Store the result in a variable named $result.
PHP
Need a hint?

Use the function name followed by parentheses with the values inside to call it.

3
Print the result
Print the value of the variable $result using echo.
PHP
Need a hint?

Use echo followed by the variable name to display its value.

4
Run and see the output
Run the complete program to see the output. It should display the sum of 5 and 7.
PHP
Need a hint?

The output should be the number 12 because 5 + 7 = 12.