Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is method to compare two text strings in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 396
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss What is method to compare two text strings in PHP

    There are two method to check two string In php.

    1: "==" Operator
    2- "strcasecmp() method"

     

    Let explain one by one

    '==': This operator return TRUE, otherwise FALSE and this method is very easy and simple to used for comparing two strings.

    You van see below example :

    Lets take two same string for compare

    Lets take two same string for compare
    <?php
    //define first string
    $str1 = "Hello joy";
    //define second string
    $str2 = "Hello joy";
    //here use '==' operator
    if($str1 == $str2){
    echo "True";
    }else{
    echo "False";
    }
    
    ?>

    Output : True

     'strcasecmp()': This method is Case-insensitive comparison and this is used for comparing two strings.

    strcasecmp() return following value:

     a)  0   - if the two strings are equal
     b) < 0 - if string1 is less than string2
     c) >0  - if string1 is greater than string2

    You can see below example

     let take two string for comparing

    <?php
    
    $str1 = "Hello joy";
    $str2 = "hello joy";
    echo strcasecmp($str1, $str2);
    
    ?>

    Output : 0

     

 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: