Complete the code to split the string into an array using a space.
<?php $string = "apple banana cherry"; $array = explode([1], $string); print_r($array); ?>
The explode function splits a string by the given delimiter. Here, the delimiter is a space " ".
Complete the code to split the string by commas.
<?php $csv = "red,green,blue"; $colors = explode([1], $csv); print_r($colors); ?>
The string contains colors separated by commas, so the delimiter for explode should be a comma ",".
Fix the error in the code to split the string by dash.
<?php $dates = "2023-06-15"; $parts = explode([1], $dates); print_r($parts); ?>
The delimiter must be a string inside quotes. Using "-" or '-' is correct. Option C is missing quotes and causes an error.
Fill both blanks to create an array of words longer than 3 letters.
<?php $sentence = "I love coding in PHP"; $words = explode([1], $sentence); $longWords = array_filter($words, fn($word) => strlen($word) [2] 3); print_r($longWords); ?>
The string is split by space " ". The filter keeps words with length greater than 3 using >.
Fill all three blanks to create an associative array of words and their lengths for words longer than 2 letters.
<?php $text = "apple banana cat dog"; $result = []; foreach(explode(" ", $text) as [3]) { if(strlen([3]) > 2) { $result[[1]] = [2]; } } print_r($result); ?>
We loop over each $word from exploding the string by space. The key is the word $word, the value is its length strlen($word).