0
0
PHPprogramming~5 mins

Type coercion in operations in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is type coercion in PHP?
Type coercion is when PHP automatically converts one data type to another during operations, like turning a string into a number when needed.
Click to reveal answer
beginner
What happens when you add a string containing a number to an integer in PHP?<br>
$result = '5' + 3;
PHP converts the string '5' to the number 5, then adds 3, so $result becomes 8.
Click to reveal answer
intermediate
How does PHP treat a string that starts with numbers but has letters after when used in arithmetic?<br>
$result = '10abc' + 5;
PHP reads the starting number 10 and ignores the letters, so it adds 10 + 5 = 15.
Click to reveal answer
beginner
What is the result of adding a boolean to an integer in PHP?<br>
$result = true + 2;
PHP converts true to 1, so the result is 1 + 2 = 3.
Click to reveal answer
intermediate
What happens when you add a non-numeric string to a number in PHP?<br>
$result = 'hello' + 5;
PHP converts the non-numeric string to 0, so the result is 0 + 5 = 5.
Click to reveal answer
What does PHP do when adding '7' + 3?
AKeeps '7' as string and ignores 3
BConcatenates '7' and 3 to '73'
CThrows an error
DConverts '7' to number 7 and adds to 3
What is the result of '12abc' + 8 in PHP?
A20
B12abc8
CError
D8
How does PHP treat true when used in arithmetic?
AAs 1
BAs 0
CAs true
DThrows an error
What happens when you add 'hello' + 5 in PHP?
AError
Bhello5
C5
Dhello
Which of these is NOT a type PHP coerces automatically?
ABoolean to number
BArray to number
CString to number
DInteger to float
Explain how PHP handles type coercion when adding a string and a number.
Think about how PHP reads the string before adding.
You got /4 concepts.
    Describe what happens when a boolean is used in an arithmetic operation in PHP.
    Booleans act like small numbers in math.
    You got /3 concepts.