Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Compare strings in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 114
    Comment on it

    You will always get a situation where you want to compare the values with each other whether with the integers or with the string comparison.
    We can easily compare two strings with the help of comparison operator.If the two strings are equal to each other it returns true.

    if('string1' == 'string1')
    {
        echo 'Strings match.';
    } else {
        echo 'Strings do not match.';
    } 
    

    Another way to compare the strings is by using strcmp function that will return 0 if string match.

    if(strcmp('string1', 'string1') == 0)
    {
         echo 'Strings match.';
    } else {
         echo 'Strings do not match.';
    }
    

    So we can use these above function when comparing strings that are input by user.

    We can also compare case insensitive strings with the help of strcasecmp function.

    if(strcasecmp('string1', 'string1') == 0)
    {
         echo 'Strings match.';
    } else {
         echo 'Strings do not match.';
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: