What is a type declaration for a parameter in PHP?
A type declaration for a parameter in PHP specifies the expected data type of the argument passed to a function or method. It helps catch errors early by ensuring the correct type is used.
Click to reveal answer
beginner
How do you declare a parameter to accept only integers in PHP?
You add the type before the parameter name, like this: <code>function example(int $number) { }</code>. This means the function expects an integer argument.
Click to reveal answer
intermediate
What happens if you pass a wrong type to a function with a declared parameter type in PHP?
PHP will throw a TypeError at runtime, stopping the program unless caught. This helps prevent bugs caused by wrong data types.
Click to reveal answer
intermediate
Can you declare a parameter to accept multiple types in PHP?
Yes, since PHP 8.0, you can use union types like <code>function example(int|string $value) { }</code> to accept either an integer or a string.
Click to reveal answer
beginner
What is the difference between type declarations and type hints in PHP?
They mean the same thing: specifying the expected type of a parameter. The term 'type hint' was used before PHP 7, now 'type declaration' is more common.
Click to reveal answer
How do you declare a function parameter to accept only strings in PHP?
Afunction example(string $text) {}
Bfunction example($text: string) {}
Cfunction example(text string) {}
Dfunction example($text) : string {}
✗ Incorrect
The correct syntax is to put the type before the parameter name: string $text.
What error does PHP throw if a wrong type is passed to a typed parameter?
ATypeError
BSyntaxError
CValueError
DRuntimeException
✗ Incorrect
PHP throws a TypeError when a parameter type declaration is violated.
Which PHP version introduced union types for parameters?
APHP 7.4
BPHP 5.6
CPHP 7.0
DPHP 8.0
✗ Incorrect
Union types were introduced in PHP 8.0, allowing multiple types for a parameter.
What does this function declaration mean? function test(array $data) {}
AThe function accepts any type of argument.
BThe function accepts only arrays as argument.
CThe function returns an array.
DThe function accepts only strings.
✗ Incorrect
The parameter $data must be an array due to the type declaration.
Can you omit the type declaration for a parameter in PHP?
AOnly in PHP 8 and above.
BNo, all parameters must have a type declaration.
CYes, parameters can be untyped and accept any value.
DOnly for class methods.
✗ Incorrect
Type declarations are optional; if omitted, the parameter accepts any type.
Explain how type declarations for parameters improve PHP code quality.
Think about how telling PHP what type to expect helps avoid mistakes.
You got /4 concepts.
Describe how to declare a parameter that accepts either an integer or a string in PHP.
Use the vertical bar | to separate types.
You got /3 concepts.
Practice
(1/5)
1. What does a type declaration for a function parameter in PHP do?
easy
A. It tells PHP what kind of data the function expects for that parameter.
B. It changes the function's return type automatically.
C. It makes the function run faster by optimizing memory.
D. It hides the parameter from being accessed outside the function.
Solution
Step 1: Understand the purpose of type declarations
Type declarations specify the expected data type for function parameters, helping PHP check inputs.
Step 2: Identify what type declarations do not do
They do not change return types, optimize speed, or hide parameters.
Final Answer:
It tells PHP what kind of data the function expects for that parameter. -> Option A
Quick Check:
Type declarations = specify expected parameter type [OK]
Hint: Type declarations specify expected input types for functions [OK]
Common Mistakes:
Thinking type declarations change return types
Believing they improve speed directly
Confusing parameter visibility with type declarations
2. Which of the following is the correct syntax to declare a function parameter as an integer in PHP?
easy
A. function add(integer $a, integer $b) {}
B. function add($int a, $int b) {}
C. function add(int: $a, int: $b) {}
D. function add(int $a, int $b) {}
Solution
Step 1: Recall PHP type declaration syntax
PHP uses the type name followed by the parameter with a $ sign, like int $a.
Step 2: Check each option's syntax
function add(int $a, int $b) {} matches correct syntax. Options A, B, and D use invalid syntax.
Final Answer:
function add(int $a, int $b) {} -> Option D
Quick Check:
Correct syntax = type before $parameter [OK]
Hint: Type before $parameter name, no extra symbols [OK]
Step 1: Understand parameter types and argument passing
The function expects floats. PHP will convert integer 3 to float 3.0 automatically.
Step 2: Calculate the multiplication
3.0 * 4.5 = 13.5, so the function returns 13.5.
Final Answer:
13.5 -> Option A
Quick Check:
3 * 4.5 = 13.5 [OK]
Hint: PHP auto-converts int to float if declared [OK]
Common Mistakes:
Expecting TypeError for int passed to float
Calculating wrong multiplication
Confusing output with integer result
4. Identify the error in this PHP function declaration:
function greet(string name) {
return "Hello, $name!";
}
medium
A. Wrong type declaration keyword
B. Function must return int
C. Missing $ before parameter name
D. No error, code is correct
Solution
Step 1: Check parameter syntax
In PHP, parameter names must start with a $ sign, so string name is incorrect.
Step 2: Identify the correct syntax
The correct syntax is string $name with $ before the parameter name.
Final Answer:
Missing $ before parameter name -> Option C
Quick Check:
Parameter names need $ [OK]
Hint: Parameter names always start with $ in PHP [OK]
Common Mistakes:
Omitting $ before parameter name
Confusing type declaration with variable name
Assuming no $ needed in function parameters
5. Given this function:
function process(array $data, int $limit = 10) {
return array_slice($data, 0, $limit);
}
Which call will cause a TypeError?
hard
A. process([1, 2, 3], 2)
B. process('not an array', 5)
C. process([1, 2, 3])
D. process([1, 2, 3], 0)
Solution
Step 1: Analyze parameter types and defaults
The first parameter must be an array; the second is an int with default 10.
Step 2: Check each call
process([1, 2, 3], 2) passes array and int correctly. process('not an array', 5) passes a string instead of array, causing TypeError. Options A, B, and C pass valid types.
Final Answer:
process('not an array', 5) causes TypeError -> Option B
Quick Check:
First param must be array, string causes error [OK]
Hint: Check argument types match declared parameter types [OK]
Common Mistakes:
Passing wrong type for array parameter
Assuming default values fix type errors
Ignoring parameter order and types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.