📖 Scenario: Imagine you are working with a small PHP script that handles simple math operations. Sometimes, the numbers come as strings, and PHP needs to convert them automatically to do the math correctly. This is called type coercion.
🎯 Goal: You will create variables with string and integer values, then perform addition to see how PHP changes types automatically. Finally, you will print the result to understand how type coercion works in PHP.
📋 What You'll Learn
Create variables with string and integer values
Add these variables together to see type coercion
Print the result to show the final value
💡 Why This Matters
🌍 Real World
When working with user input or data from files, numbers may come as strings. Understanding how PHP converts types helps avoid bugs in calculations.
💼 Career
Many PHP jobs require handling mixed data types safely. Knowing type coercion helps write reliable code that works with different input formats.
Progress0 / 4 steps
1
Create variables with string and integer values
Create a variable called $numString and set it to the string "10". Then create a variable called $numInt and set it to the integer 5.
PHP
Hint
Remember to use double quotes for the string "10" and no quotes for the integer 5.
2
Add the variables to see type coercion
Create a variable called $sum and set it to the result of adding $numString and $numInt.
PHP
Hint
Use the plus sign + to add the two variables.
3
Print the result to see the output
Use echo to print the value of $sum.
PHP
Hint
Use echo $sum; to show the result.
4
Add a string that is not a number and print the result
Create a variable called $text and set it to the string "abc". Then create a variable called $result and set it to the sum of $numString and $text. Finally, use echo to print $result.
PHP
Hint
Adding a number string and a non-number string results in PHP converting the non-number string to 0.
Practice
(1/5)
1. In PHP, what happens when you add a string containing a number to an integer, like '5' + 3?
easy
A. PHP throws a type error because you can't add string and integer.
B. The result is the string '53' by concatenating '5' and '3'.
C. The string '5' is converted to the number 5, then added to 3, resulting in 8.
D. The operation returns null because of incompatible types.
Solution
Step 1: Understand PHP type coercion in addition
PHP automatically converts strings containing numbers to integers or floats when used in arithmetic operations.
Step 2: Apply addition after conversion
The string '5' becomes the integer 5, then 5 + 3 equals 8.
Final Answer:
8 -> Option C
Quick Check:
'5' + 3 = 8 [OK]
Hint: Strings with numbers convert to numbers in math operations [OK]
Common Mistakes:
Thinking PHP concatenates strings with + operator
Expecting a type error on mixed types
Assuming the result stays a string
2. Which of the following PHP expressions correctly adds a boolean true to an integer 2 and returns 3?
easy
A. (int)'true' + 2
B. true + 2
C. true . 2
D. 'true' + 2
Solution
Step 1: Understand boolean to integer conversion
In PHP, boolean true converts to integer 1 in arithmetic operations.
A. PHP converts '5 cats' to 5, so 5 - 3 = 2, which might be unexpected if you expect string behavior.
B. Because subtraction is not defined for strings, it causes a fatal error.
C. The variable $result will be the string '5 cats3' due to concatenation.
D. PHP treats both as strings and outputs '5 cats3'.
Solution
Step 1: Analyze subtraction with string and integer
PHP converts the string '5 cats' to the number 5 for arithmetic operations.
Step 2: Calculate the subtraction
5 - 3 equals 2, so $result is 2, which may surprise if expecting string concatenation or error.
Final Answer:
PHP converts '5 cats' to 5, so result is 2 -> Option A
Quick Check:
'5 cats' - 3 = 2 [OK]
Hint: Subtraction forces string to number conversion [OK]
Common Mistakes:
Expecting string concatenation with - operator
Thinking it causes an error
Assuming result is a string
5. You want to compare two variables in PHP: $a = '0' (string) and $b = false (boolean). Which comparison returns true when using type coercion, and which returns false when checking type strictly?
Choose the correct pair:
hard
A. $a == $b is true; $a === $b is false
B. $a == $b is false; $a === $b is true
C. $a == $b is true; $a === $b is true
D. $a == $b is false; $a === $b is false
Solution
Step 1: Understand loose (==) vs strict (===) comparison
Loose comparison converts types before comparing; strict compares value and type exactly.
Step 2: Evaluate comparisons
Loose: '0' converts to 0, false converts to 0, so 0 == 0 is true. Strict: '0' is string, false is boolean, types differ, so false.
Final Answer:
$a == $b is true; $a === $b is false -> Option A
Quick Check:
'0' == false is true; '0' === false is false [OK]
Hint: Use === to check type and value, == allows type coercion [OK]
Common Mistakes:
Assuming == checks type strictly
Thinking '0' and false are always different
Confusing loose and strict equality
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.