In this post, you will learn how to compare two strings in PHP. It’s really simple to compare strings in PHP and also there are multiple ways you can compare strings in PHP. So without wasting our time let’s see how to do it.
1. Method 1: PHP Snippet to compare strings using === (triple equals):
1 2 3 4 5 6 7 8 9 10 11 |
<?php $str1 = 'agur'; $str2 = 'babu'; //let's compare the strings if ($str1 === $str2) { echo "Both the strings are equal!"; } else { echo "Both the strings are not equal!"; } |
Method 1 is very simple, compare both the string using (===) triple equals if both the strings are equal then it is true otherwise it is false. So the return value is boolean here.
2. Method 2: PHP Snippet to compare strings using strcmp():
1 2 3 4 5 6 7 8 9 10 11 |
<?php $str1 = 'agur'; $str2 = 'babu'; //let's compare the strings using strcmp() if ( strcmp($str1, $str2) == 0 ) { echo "Both the strings are equal!"; } else { echo "Both the strings are not equal!"; } |
Method 2 is slightly different from the (===) triple equals operator because the return for strcmp() is not a boolean instead it is an integer. So, if the return value strcmp() is 0 then both the strings are equal otherwise not equal.
Let’s write another useful PHP example with a form so we can get the strings from the user to compare them and print the result. Also, we will use the bootstrap CSS to create the form so the look and feel will be much better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php $alert_class='alert-warning'; $alert_msg = 'Please fill the form'; //if the form is submitted if ($_POST) { //get the input values $str1 = $_POST['first_string']; $str2 = $_POST['second_string']; //let's compare the strings if ( $str1 === $str2 ) { $alert_class = 'alert-success'; $alert_msg = 'Both the strings are equal!'; } else { $alert_class = 'alert-danger'; $alert_msg = 'Both the strings are not equal!'; } } ?> <!DOCTYPE html> <html lang=""> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>PHP program to compare Two Strings!</title> <!-- Bootstrap CSS --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h1 class="text-center">PHP program to compare Two Strings!</h1> <!-- Output --> <div id="msg"> <div class="alert <?=$alert_class;?>"> <div id="result"><?=$alert_msg;?></div> </div> </div> <!-- our form --> <form action="" method="POST" role="form"> <div class="form-group"> <label for="first_string">First string:</label> <input type="text" class="form-control" name="first_string" id="first_string" value="<?=(isset($_POST['first_string'])) ? $_POST['first_string'] : '';?>"> </div> <div class="form-group"> <label for="second_string">Second string:</label> <input type="text" class="form-control" name="second_string" id="second_string" value="<?=(isset($_POST['second_string'])) ? $_POST['second_string']: '';?>"> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </div> </body> </html> |
You can check the demo of the above script here: