Dynamic typing means PHP variables do not have a fixed type. The type is determined automatically based on the value assigned to the variable.
Click to reveal answer
beginner
How does PHP handle adding a string and an integer?
PHP converts the string to a number if possible, then adds the two numbers. For example, '5' + 3 results in 8.
Click to reveal answer
beginner
What happens if you use a variable before assigning a value in PHP?
PHP treats the variable as NULL and may show a warning depending on error settings.
Click to reveal answer
intermediate
Explain type juggling in PHP.
Type juggling is PHP automatically converting types when performing operations, like converting strings to numbers or booleans to integers.
Click to reveal answer
intermediate
Can you force a variable to a specific type in PHP? How?
Yes, by using type casting like (int), (string), (bool), etc. For example, (int) '123' converts the string to an integer.
Click to reveal answer
What type will the variable have after this code? $var = 10;
AFloat
BInteger
CBoolean
DString
✗ Incorrect
Assigning 10 makes $var an integer type.
What is the result of '5' + 2 in PHP?
AError
B'52'
C7
D'5 2'
✗ Incorrect
PHP converts '5' to integer 5 and adds 2, resulting in 7.
What does PHP do if you add a boolean TRUE to an integer 3?
ATRUE3
B3
CError
D4
✗ Incorrect
TRUE is converted to 1, so 1 + 3 = 4.
Which of these is NOT a way to cast a variable to a type in PHP?
A(var)
B(string)
C(float)
D(int)
✗ Incorrect
(var) is not a valid type cast in PHP.
If you assign $x = 'abc'; and then add 5 to $x, what is the result?
A5
B'abc5'
CError
D0
✗ Incorrect
PHP converts 'abc' to 0 when used in arithmetic, so 0 + 5 = 5.
Explain how PHP's dynamic typing affects variable usage and operations.
Think about how PHP changes variable types based on context.
You got /4 concepts.
Describe how you can control or change variable types explicitly in PHP.
Consider how to force a variable to be a certain type.
You got /3 concepts.
Practice
(1/5)
1. In PHP, what happens if you assign a string value to a variable and then assign an integer to the same variable later?
easy
A. PHP throws a type error because the variable type is fixed.
B. The variable keeps the original string type and ignores the integer.
C. The variable changes its type dynamically to integer.
D. The variable becomes an array containing both values.
Solution
Step 1: Understand PHP variable typing
PHP variables do not have fixed types and can change type dynamically during execution.
Step 2: Assign different types to the same variable
Assigning a string first and then an integer changes the variable's type to integer automatically.
Final Answer:
The variable changes its type dynamically to integer. -> Option C
Quick Check:
PHP variables are dynamically typed = B [OK]
Hint: Remember: PHP variables change type automatically when reassigned [OK]
Common Mistakes:
Thinking PHP variables have fixed types like in Java
Assuming PHP throws errors on type changes
Believing variables keep old values as arrays
2. Which of the following is the correct way to assign a float value to a variable in PHP?
easy
A. $num = 3.14; // float assignment
B. $num = '3.14'; // string assignment
C. $num = (int)3.14; // integer cast
D. $num = float(3.14); // float function
Solution
Step 1: Identify correct float assignment syntax
In PHP, assigning a float is done by directly assigning a number with decimal point without quotes.
Step 2: Evaluate each option
$num = 3.14; // float assignment assigns 3.14 as a float correctly. $num = '3.14'; // string assignment assigns a string, C casts to int, D uses invalid syntax.
Final Answer:
$num = 3.14; // float assignment -> Option A
Quick Check:
Float assignment uses decimal number without quotes = A [OK]
Hint: Assign floats without quotes; quotes make it a string [OK]
Common Mistakes:
Using quotes around numbers to assign floats
Trying to cast with invalid functions like float()
Casting floats to int unintentionally
3. What will be the output of the following PHP code?