0
0
PHPprogramming~20 mins

Type casting syntax in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Type Casting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code with type casting?

Consider the following PHP code snippet. What will it output?

PHP
<?php
$value = "123abc";
$intValue = (int)$value;
echo $intValue;
?>
A123
B0
C123abc
DError
Attempts:
2 left
💡 Hint

When casting a string to int in PHP, it converts the initial numeric part.

Predict Output
intermediate
2:00remaining
What is the output when casting a float to int in PHP?

What will this PHP code print?

PHP
<?php
$floatVal = 3.99;
$intVal = (int)$floatVal;
echo $intVal;
?>
A4
BError
C3.99
D3
Attempts:
2 left
💡 Hint

Casting float to int truncates the decimal part.

Predict Output
advanced
2:00remaining
What error does this PHP code raise?

What error will this PHP code produce?

PHP
<?php
$array = [1, 2, 3];
$intVal = (int)$array;
echo $intVal;
?>
A1
B0
CFatal error: Uncaught Error: Object of class Array could not be converted to int
DWarning: Array to string conversion
Attempts:
2 left
💡 Hint

Arrays cannot be cast to int in PHP.

Predict Output
advanced
2:00remaining
What is the output of casting boolean to string in PHP?

What will this PHP code output?

PHP
<?php
$boolVal = true;
$strVal = (string)$boolVal;
echo strlen($strVal);
?>
A4
B1
C0
DError
Attempts:
2 left
💡 Hint

True converts to string "1" and false to empty string "".

Predict Output
expert
3:00remaining
What is the output of this complex type casting in PHP?

What will this PHP code output?

PHP
<?php
$value = "10 apples";
$floatVal = (float)$value;
$intVal = (int)$floatVal;
echo $intVal + 5;
?>
A15
B10
C5
DError
Attempts:
2 left
💡 Hint

Casting string to float reads initial number, then casting float to int truncates decimals.