Strict typing requires exact types. Passing '4.5' as string instead of float causes a type mismatch.
Step 2: Determine the error type
PHP throws a TypeError because it does not convert string to float automatically under strict typing.
Final Answer:
TypeError because '4.5' is a string, not float -> Option B
Quick Check:
Strict typing + string instead of float = TypeError [OK]
Hint: Strings passed as floats cause TypeError with strict typing [OK]
Common Mistakes:
Thinking PHP converts string '4.5' to float automatically
Confusing TypeError with syntax error
Expecting a warning instead of error
5. You want to write a PHP function that accepts only integers and returns their sum. Which code snippet correctly enforces strict typing and prevents accidental passing of strings or floats?
hard
A.
B.
C.
D.
Solution
Step 1: Check strict typing declaration
uses declare(strict_types=1); to enable strict typing, which is required to enforce exact types.
Step 2: Verify function argument types
declares the function to accept only integers using int ...$nums, enforcing all arguments to be integers.
Step 3: Compare other options
lacks strict typing and type hints, allowing any type. disables strict typing. lacks type hint for arguments.
Final Answer:
correctly enforces strict typing and integer-only arguments -> Option A
Quick Check:
Strict typing + int type hint = safe integer sum [OK]
Hint: Use declare(strict_types=1) and int type hints for safety [OK]
Common Mistakes:
Omitting strict_types declaration
Not using type hints for function parameters
Using declare(strict_types=0) which disables strict typing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.