Bird
Raised Fist0
PHPprogramming~10 mins

String comparison functions in PHP - Interactive Code Practice

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

Complete the code to compare two strings for equality.

PHP
<?php
$string1 = "apple";
$string2 = "apple";
if ($string1 [1] $string2) {
    echo "Strings are equal.";
} else {
    echo "Strings are not equal.";
}
?>
Drag options to blanks, or click blank then click option'
A==
B===
C!=
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks for inequality.
Using === which also checks type, not just value.
2fill in blank
medium

Complete the code to compare two strings ignoring case.

PHP
<?php
$string1 = "Hello";
$string2 = "hello";
if ([1]($string1, $string2) === 0) {
    echo "Strings are equal ignoring case.";
} else {
    echo "Strings are different.";
}
?>
Drag options to blanks, or click blank then click option'
Astrcmp
Bstrcasecmp
Cstrnatcmp
Dstrncmp
Attempts:
3 left
💡 Hint
Common Mistakes
Using strcmp which is case-sensitive.
Using strncmp which compares only part of the strings.
3fill in blank
hard

Fix the error in the code to correctly compare the first 3 characters of two strings.

PHP
<?php
$string1 = "abcdef";
$string2 = "abcxyz";
if (strncmp($string1, $string2, [1]) === 0) {
    echo "First 3 characters are equal.";
} else {
    echo "First 3 characters are different.";
}
?>
Drag options to blanks, or click blank then click option'
A2
B4
C3
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or 4 which compares wrong number of characters.
Confusing the argument with string length.
4fill in blank
hard

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
<?php
$words = ["apple", "cat", "banana", "dog"];
$lengths = [
    [1] => strlen([2]) 
    for [1] in $words 
    if strlen([1]) > 4
];
?>
Drag options to blanks, or click blank then click option'
A$word
B$words
Cword
Dstrlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using $words which is the whole array, not a single word.
Using word without $ which is invalid in PHP.
5fill in blank
hard

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
<?php
$words = ["pear", "fig", "grape", "kiwi"];
$result = [
    [1] => [2] 
    for [3] in $words 
    if strlen([3]) > 3
];
?>
Drag options to blanks, or click blank then click option'
Astrtoupper($word)
Bstrlen($word)
C$word
D$fruit
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not using strtoupper for the key.

Practice

(1/5)
1. Which PHP function compares two strings in a case-sensitive way and returns 0 if they are exactly equal?
easy
A. strncmp
B. strcasecmp
C. strncasecmp
D. strcmp

Solution

  1. Step 1: Understand case sensitivity in string comparison

    strcmp compares two strings exactly, including case.
  2. Step 2: Identify function behavior

    strcmp returns 0 if strings are exactly equal, considering case.
  3. Final Answer:

    strcmp -> Option D
  4. Quick Check:

    Case-sensitive compare = strcmp [OK]
Hint: Case matters? Use strcmp or strncmp [OK]
Common Mistakes:
  • Confusing strcmp with strcasecmp (case-insensitive)
  • Using strncasecmp for full string compare
  • Thinking strcmp ignores case
2. Which of the following is the correct syntax to compare first 3 characters of two strings ignoring case in PHP?
easy
A. strncasecmp($str1, $str2, 3)
B. strncmp($str1, $str2, 3)
C. strcasecmp($str1, $str2, 3)
D. strcmp($str1, $str2, 3)

Solution

  1. Step 1: Identify function for case-insensitive partial compare

    strncasecmp compares first N characters ignoring case.
  2. Step 2: Check syntax correctness

    strncasecmp($str1, $str2, 3) correctly compares first 3 chars case-insensitively.
  3. Final Answer:

    strncasecmp($str1, $str2, 3) -> Option A
  4. Quick Check:

    Partial case-insensitive compare = strncasecmp [OK]
Hint: Partial + ignore case = strncasecmp [OK]
Common Mistakes:
  • Using strncmp which is case-sensitive
  • Passing length to strcasecmp (no length param)
  • Using strcmp with length parameter
3. What will be the output of the following PHP code?
$result = strcasecmp('Hello', 'hello');
echo $result;
medium
A. -1
B. 0
C. 1
D. Error

Solution

  1. Step 1: Understand strcasecmp behavior

    It compares strings ignoring case and returns 0 if equal.
  2. Step 2: Compare 'Hello' and 'hello'

    Ignoring case, both strings are equal, so result is 0.
  3. Final Answer:

    0 -> Option B
  4. Quick Check:

    strcasecmp('Hello', 'hello') = 0 [OK]
Hint: strcasecmp returns 0 if strings equal ignoring case [OK]
Common Mistakes:
  • Expecting 1 or -1 for equal strings
  • Confusing case-sensitive strcmp with strcasecmp
  • Thinking it throws error for case differences
4. Identify the error in this PHP code snippet:
if (strncmp('apple', 'Apple', 5) == 0) {
echo 'Match';
} else {
echo 'No match';
}
medium
A. No error, code works correctly
B. Use strcasecmp instead of strncmp for case-insensitive compare
C. Use strncasecmp instead of strncmp for case-insensitive compare
D. Change 5 to length of shorter string

Solution

  1. Step 1: Analyze strncmp behavior

    strncmp is case-sensitive, so 'apple' != 'Apple'.
  2. Step 2: Fix for case-insensitive compare

    Use strncasecmp to ignore case in first 5 chars.
  3. Final Answer:

    Use strncasecmp instead of strncmp for case-insensitive compare -> Option C
  4. Quick Check:

    Case-insensitive partial compare needs strncasecmp [OK]
Hint: strncmp is case-sensitive; use strncasecmp to ignore case [OK]
Common Mistakes:
  • Assuming strncmp ignores case
  • Using strcasecmp with length parameter
  • Changing length unnecessarily
5. You want to sort an array of strings alphabetically ignoring case in PHP. Which function is best to use as a comparison callback for usort?
hard
A. strcasecmp
B. strcmp
C. strncmp
D. strncasecmp

Solution

  1. Step 1: Understand sorting with case-insensitivity

    Sorting ignoring case requires a case-insensitive comparison function.
  2. Step 2: Choose correct function for full string compare ignoring case

    strcasecmp compares full strings ignoring case, ideal for sorting.
  3. Final Answer:

    strcasecmp -> Option A
  4. Quick Check:

    Case-insensitive full compare for sorting = strcasecmp [OK]
Hint: Sort ignoring case? Use strcasecmp as callback [OK]
Common Mistakes:
  • Using strcmp which is case-sensitive
  • Using strncmp or strncasecmp which compare partial strings
  • Passing no callback to usort
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard