📖 Scenario: You are building a simple PHP program to compare two strings in different ways. This is useful when you want to check if two words are the same, or if one word comes before another in alphabetical order.
🎯 Goal: Create a PHP script that compares two strings using different string comparison functions and shows the results.
📋 What You'll Learn
Create two string variables with exact values
Create a variable to hold the length limit for comparison
Use strcmp to compare the two strings
Use strcasecmp to compare the two strings ignoring case
Use strncmp to compare the first N characters of the two strings
Print the results of all comparisons
💡 Why This Matters
🌍 Real World
String comparison is important in many programs like sorting names, checking passwords, or filtering text.
💼 Career
Understanding string comparison functions helps in data validation, search features, and user input handling in web development.
Progress0 / 4 steps
1
DATA SETUP: Create two string variables
Create two string variables called string1 and string2 with these exact values: "HelloWorld" and "helloworld" respectively.
PHP
Hint
Use the = sign to assign the exact text to each variable.
2
CONFIGURATION: Create a length variable for partial comparison
Create an integer variable called length and set it to 5.
PHP
Hint
Just create a variable with the number 5 to use later.
3
CORE LOGIC: Compare the strings using string comparison functions
Use strcmp to compare $string1 and $string2 and store the result in $result1. Then use strcasecmp to compare $string1 and $string2 ignoring case and store in $result2. Finally, use strncmp to compare the first $length characters of $string1 and $string2 and store in $result3.
PHP
Hint
Use the exact function names and variable names as shown.
4
OUTPUT: Print the comparison results
Print the values of $result1, $result2, and $result3 each on a new line using echo.
PHP
Hint
Use echo and add a newline character \n after each result.
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
Step 1: Understand case sensitivity in string comparison
strcmp compares two strings exactly, including case.
Step 2: Identify function behavior
strcmp returns 0 if strings are exactly equal, considering case.
Final Answer:
strcmp -> Option D
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
Step 1: Identify function for case-insensitive partial compare
strncasecmp compares first N characters ignoring case.
Step 2: Check syntax correctness
strncasecmp($str1, $str2, 3) correctly compares first 3 chars case-insensitively.