📖 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
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
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
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
Hint
The output should be the number 12 because 5 + 7 = 12.
Practice
(1/5)
1. What is the role of parameters in a PHP function?
easy
A. They are used to store the function's return value.
B. They are the actual values passed to the function during a call.
C. They are variables declared outside the function.
D. They are placeholders for values the function will receive when called.
Solution
Step 1: Understand function definition
Parameters are the names inside the parentheses of a function definition that act as placeholders.
Step 2: Differentiate parameters from arguments
Arguments are the actual values given to the function when it is called, not the placeholders.
Final Answer:
They are placeholders for values the function will receive when called. -> Option D
Quick Check:
Parameters = placeholders [OK]
Hint: Parameters are names in function definition, not actual values [OK]
Common Mistakes:
Confusing parameters with arguments
Thinking parameters hold return values
Assuming parameters are global variables
2. Which of the following is the correct way to define a function with one parameter named name in PHP?
easy
A. function greet(name) { echo "Hello $name"; }
B. function greet(&name) { echo "Hello $name"; }
C. function greet($name) { echo "Hello $name"; }
D. function greet($name) echo "Hello $name";
Solution
Step 1: Check parameter syntax in PHP
In PHP, parameters must have a $ sign before the name inside the function definition.
Step 2: Validate function body syntax
The function body must be enclosed in curly braces {} to be valid.
Final Answer:
function greet($name) { echo "Hello $name"; } -> Option C
Quick Check:
Parameters need $ and braces [OK]
Hint: Parameters always start with $ in PHP function definitions [OK]
Common Mistakes:
Omitting $ before parameter name
Missing curly braces around function body
Using & without purpose
3. What will be the output of the following PHP code?