Compare Two Strings in PHP


Q. Write a PHP program to compare two strings

<?php
// Define two strings
$str1 = "Hello";
$str2 = "hello";

// Compare using strcmp (case-sensitive)
if (strcmp($str1, $str2) == 0) {
    echo "Strings are equal (case-sensitive).";
} else {
    echo "Strings are NOT equal (case-sensitive).";
}

echo "<br>";

// Compare using strcasecmp (case-insensitive)
if (strcasecmp($str1, $str2) == 0) {
    echo "Strings are equal (case-insensitive).";
} else {
    echo "Strings are NOT equal (case-insensitive).";
}
?> 

Q. Write an algorithm to compare two strings in PHP

1. Start
2. Define two strings: str1 and str2
3. Use strcmp(str1, str2) to compare (case-sensitive)
4. If result is 0, print "Strings are equal"
5. Else, print "Strings are not equal"
6. Use strcasecmp(str1, str2) to compare (case-insensitive)
7. If result is 0, print "Strings are equal"
8. Else, print "Strings are not equal"
9. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.