0
0
PHPprogramming~10 mins

Type coercion in operations in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a string number and an integer.

PHP
<?php
$result = '5' [1] 3;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A+
B.
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) which concatenates strings instead of adding numbers.
2fill in blank
medium

Complete the code to concatenate a string and a number.

PHP
<?php
$result = 'Value: ' [1] 10;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A*
B+
C-
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus (+) which tries to add and converts the string to zero.
3fill in blank
hard

Fix the error in the code to correctly multiply a string number by an integer.

PHP
<?php
$result = '4' [1] 2;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A.
B*
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using the dot operator (.) which causes a string concatenation instead of multiplication.
4fill in blank
hard

Fill both blanks to create an array where keys are string lengths and values are the strings themselves, only for strings longer than 3 characters.

PHP
<?php
$words = ['apple', 'cat', 'banana', 'dog'];
$result = array_combine(array_map('strlen', array_filter($words, function($word) {
    return strlen($word) [1] 3;
})), array_filter($words, function($word) {
    return strlen($word) [2] 3;
}));
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or == which would select wrong words or none.
5fill in blank
hard

Fill both blanks to create a dictionary with uppercase keys, original values, and only include values greater than 10.

PHP
<?php
$data = ['a' => 5, 'b' => 15, 'c' => 20];
$result = array_filter(array_combine(array_map('[1]', array_keys($data)), $data), function($value) {
    return $value [2] 10;
});
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Astrtoupper
B>
C>=
Dstrtolower
Attempts:
3 left
💡 Hint
Common Mistakes
Using strtolower instead of strtoupper.
Using >= instead of > changes the filter condition.