Complete the code to compare two strings for equality.
<?php $string1 = "apple"; $string2 = "apple"; if ($string1 [1] $string2) { echo "Strings are equal."; } else { echo "Strings are not equal."; } ?>
!= which checks for inequality.=== which also checks type, not just value.The operator == checks if two strings have the same value.
Complete the code to compare two strings ignoring case.
<?php $string1 = "Hello"; $string2 = "hello"; if ([1]($string1, $string2) === 0) { echo "Strings are equal ignoring case."; } else { echo "Strings are different."; } ?>
strcmp which is case-sensitive.strncmp which compares only part of the strings.The function strcasecmp compares two strings ignoring case differences.
Fix the error in the code to correctly compare the first 3 characters of two strings.
<?php $string1 = "abcdef"; $string2 = "abcxyz"; if (strncmp($string1, $string2, [1]) === 0) { echo "First 3 characters are equal."; } else { echo "First 3 characters are different."; } ?>
The third argument of strncmp is the number of characters to compare. To compare first 3 characters, use 3.
Fill both blanks to create an associative array with words as keys and their lengths as values, only for words longer than 4 characters.
<?php $words = ["apple", "cat", "banana", "dog"]; $lengths = [ [1] => strlen([2]) for [1] in $words if strlen([1]) > 4 ]; ?>
$words which is the whole array, not a single word.word without $ which is invalid in PHP.Use $word as the variable name for each word in the array. It is used as key and inside strlen to get length.
Fill all three blanks to create an associative array with uppercase words as keys and their lengths as values, only for words longer than 3 characters.
<?php $words = ["pear", "fig", "grape", "kiwi"]; $result = [ [1] => [2] for [3] in $words if strlen([3]) > 3 ]; ?>
strtoupper for the key.Use $word as the variable name. The key is the uppercase word using strtoupper($word). The value is the length using strlen($word).