Recall & Review
beginner
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?
✗ 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?
✗ Incorrect
PHP throws a
TypeError when a parameter type declaration is violated.Which PHP version introduced union types for parameters?
✗ 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) {}✗ Incorrect
The parameter
$data must be an array due to the type declaration.Can you omit the type declaration for a parameter in PHP?
✗ 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.