0
0
PHPprogramming~15 mins

Type declarations for parameters in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Type Declarations for Parameters in PHP
📖 Scenario: You are building a simple calculator that adds two numbers. To make sure the calculator works correctly, you want to tell PHP exactly what kind of values the calculator should accept.
🎯 Goal: Create a PHP function that adds two numbers using type declarations for the parameters to ensure only numbers are accepted.
📋 What You'll Learn
Create a function named addNumbers with two parameters named $a and $b.
Use type declarations to specify that both $a and $b must be integers.
Call the function with two integer values.
Print the result of the function call.
💡 Why This Matters
🌍 Real World
Type declarations help prevent errors by making sure functions get the right kind of data, which is important in real-world applications like calculators, data processing, and web forms.
💼 Career
Understanding type declarations is useful for writing clear, reliable PHP code, a skill valued in web development jobs.
Progress0 / 4 steps
1
Create the function skeleton
Write a function named addNumbers that takes two parameters named $a and $b without type declarations yet.
PHP
Need a hint?

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

2
Add type declarations to parameters
Modify the addNumbers function to add type declarations so that both parameters $a and $b are declared as int.
PHP
Need a hint?

Put int before each parameter name inside the parentheses.

3
Add the function logic
Inside the addNumbers function, write code to return the sum of $a and $b.
PHP
Need a hint?

Use the return keyword followed by $a + $b.

4
Call the function and print the result
Call the addNumbers function with the integers 5 and 7, store the result in a variable named $result, and print $result.
PHP
Need a hint?

Use $result = addNumbers(5, 7); and then print($result);.