📖 Scenario: Imagine you are building a simple calculator that adds two numbers. Sometimes, the numbers might come as strings, and sometimes as integers. Without strict typing, mistakes can happen silently, causing wrong results.
🎯 Goal: You will create a PHP script that shows how strict typing helps catch errors early by enforcing the types of inputs to a function.
📋 What You'll Learn
Create a function with strict typing enabled
Define a function that adds two integers
Try calling the function with a string and an integer
Observe the error caused by strict typing
💡 Why This Matters
🌍 Real World
Strict typing is important in real-world PHP projects to avoid bugs caused by unexpected data types, especially when working with user input or external data.
💼 Career
Many PHP jobs require writing reliable and maintainable code. Understanding strict typing helps you write safer functions and avoid common errors.
Progress0 / 4 steps
1
Enable strict typing and create the add function
Write the first line declare(strict_types=1); to enable strict typing. Then create a function called add that takes two integer parameters int $a and int $b and returns their sum as an integer.
PHP
Hint
Start your PHP file with declare(strict_types=1); to turn on strict typing. Then define the function with int type hints for parameters and return type.
2
Call the add function with integer arguments
Call the add function with two integers: 5 and 10. Store the result in a variable called $result.
PHP
Hint
Call the function with two numbers and save the answer in $result.
3
Call the add function with a string and an integer
Call the add function with a string '5' and an integer 10. Store the result in a variable called $result2.
PHP
Hint
Try passing a string instead of an integer to see how strict typing stops this.
4
Print the results
Print the variables $result and $result2 using echo statements, each on a new line.
PHP
Hint
Print both results. The first will show 15. The second will cause a TypeError because of strict typing.
Practice
(1/5)
1. What is the main purpose of using declare(strict_types=1); in a PHP file?
easy
A. To disable error reporting in the script
B. To speed up the execution of PHP scripts
C. To allow automatic type conversion between variables
D. To enforce that function arguments and return values must match declared types exactly
Solution
Step 1: Understand the role of strict typing in PHP
Strict typing ensures that the types of function arguments and return values are exactly as declared, preventing automatic type coercion.
Step 2: Analyze the effect of declare(strict_types=1);
This declaration activates strict typing mode in the file, enforcing exact type matches and reducing bugs caused by unexpected type conversions.
Final Answer:
To enforce that function arguments and return values must match declared types exactly -> Option D
Quick Check:
Strict typing = enforce exact types [OK]
Hint: Strict typing means no automatic type changes allowed [OK]
Common Mistakes:
Thinking strict typing speeds up code
Believing it disables errors
Assuming it allows type conversions
2. Which of the following is the correct way to enable strict typing in a PHP file?
easy
A.
B.
C.
D.
Solution
Step 1: Recall the syntax for enabling strict typing
PHP uses the declare statement with strict_types=1 to enable strict typing at the start of the file.
Step 2: Identify the correct syntax among options
Only uses the correct declare(strict_types=1); syntax inside PHP tags.
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.