0
0
PHPprogramming~10 mins

String comparison functions in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String comparison functions
Start
Get two strings
Choose comparison function
Compare strings
Return comparison result
Use result to decide next step
End
The program gets two strings, compares them using a chosen function, then returns and uses the result.
Execution Sample
PHP
<?php
$str1 = "apple";
$str2 = "banana";
$result = strcmp($str1, $str2);
echo $result;
?>
Compares two strings 'apple' and 'banana' using strcmp and prints the result.
Execution Table
StepActionInput StringsFunction CalledReturn ValueMeaning
1Initialize strings$str1 = 'apple', $str2 = 'banana'N/AN/AStrings ready for comparison
2Call strcmp'apple', 'banana'strcmp-1'apple' is less than 'banana'
3Print resultN/AN/A-1Output is -1 showing order
4EndN/AN/AN/AProgram ends
💡 All steps completed, comparison result printed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$str1undefinedappleappleappleapple
$str2undefinedbananabananabananabanana
$resultundefinedundefined-1-1-1
Key Moments - 3 Insights
Why does strcmp return -1 instead of true or false?
strcmp returns an integer: negative if first string is less, zero if equal, positive if greater. See execution_table step 2 where -1 means 'apple' is less than 'banana'.
What happens if strings are equal?
If strings are equal, strcmp returns 0. This is shown by the return value meaning in execution_table step 2.
Why not use == to compare strings?
Using == checks equality only, but strcmp gives order info (less, equal, greater). This helps in sorting or ordering decisions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $result after step 2?
A0
B-1
C1
Dundefined
💡 Hint
Check the 'Return Value' column in execution_table row for step 2.
At which step does the program print the comparison result?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the 'Print result' action in execution_table.
If $str1 was 'banana' and $str2 was 'apple', what would strcmp return?
A1
B0
C-1
Dundefined
💡 Hint
strcmp returns positive if first string is greater; see meaning column in execution_table step 2.
Concept Snapshot
PHP string comparison functions:
- strcmp(str1, str2): returns <0 if str1 < str2, 0 if equal, >0 if str1 > str2
- Useful for sorting or ordering strings
- Returns integer, not boolean
- Use === or == for equality only
Full Transcript
This example shows how PHP compares two strings using strcmp. First, two strings are set: 'apple' and 'banana'. Then strcmp is called with these strings. It returns -1 because 'apple' comes before 'banana' alphabetically. The program prints this result. strcmp returns an integer indicating order: negative means first string is less, zero means equal, positive means greater. This helps decide string order, unlike simple equality checks. The execution table tracks each step and variable values. Beginners often confuse strcmp's return values with true/false, but it actually returns an integer to show order. This is important for sorting or comparing strings beyond equality.