0
0
PHPprogramming~15 mins

Return values in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values
📖 Scenario: You are creating a simple calculator function in PHP that adds two numbers and returns the result.
🎯 Goal: Build a PHP function that takes two numbers, adds them, and returns the sum. Then display the returned value.
📋 What You'll Learn
Create a function named addNumbers that takes two parameters: $a and $b.
Inside the function, return the sum of $a and $b.
Call the function with the numbers 5 and 7 and store the result in a variable named $result.
Print the value of $result.
💡 Why This Matters
🌍 Real World
Functions that return values are used everywhere in programming to get results from calculations or data processing.
💼 Career
Understanding return values is essential for writing reusable code and working with APIs, libraries, and frameworks in professional PHP development.
Progress0 / 4 steps
1
Create the function skeleton
Write a PHP function named addNumbers that takes two parameters: $a and $b. Do not add any code inside the function yet.
PHP
Need a hint?

Use the function keyword followed by the function name and parameters in parentheses.

2
Add the return statement
Inside the addNumbers function, write a return statement that returns the sum of $a and $b.
PHP
Need a hint?

Use return $a + $b; to send back the sum.

3
Call the function and store the result
Call the addNumbers function with arguments 5 and 7, and store the returned value in a variable named $result.
PHP
Need a hint?

Use $result = addNumbers(5, 7); to call the function and save the answer.

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

Use echo $result; to display the sum.