简体   繁体   English

比较两个字符串并确定它们是否相等

[英]comparing two strings and determining if they are equal

I have two strings that am trying to compare but it is displaying that they are not equal. 我有两个要比较的字符串,但显示它们不相等。 What could be the problem? 可能是什么问题呢? Here is the code that am running. 这是正在运行的代码。

     <?php
        $x = "Come and enjoy the show.";
        $y = "Come and enjoy the show.";

       if (strcmp($x, $y)) {
          echo "They are the same.";
       } else {
           echo "They are not the same.";
       }
     ?>

strcmp - It returns zero on exact match & hence else condition will get executed in your case. strcmp-精确匹配返回零,因此在您的情况下条件将被执行。

Defination : Returns < 0 if str1 is less than str2; 定义 :如果str1小于str2,则返回<0;否则,返回0。 > 0 if str1 is greater than str2, and 0 if they are equal. 如果str1大于str2,则> 0;如果相等,则> 0。

Change your condition with, 改变您的状况,

if (strcmp($x, $y) === 0) {
  echo "They are the same.";
} else {
  echo "They are not the same.";
}

DEMO . 演示

strcmp can returns multiple values. strcmp可以返回多个值。

From the doc : 从文档:

Returns < 0 if str1 is less than str2; 如果str1小于str2,则返回<0;否则,返回0。 > 0 if str1 is greater than str2, and 0 if they are equal. 如果str1大于str2,则> 0;如果相等,则> 0。

So, instead try this code : 因此,请尝试以下代码:

   if (strcmp($x, $y) === 0) {
      echo "They are the same.";
   } else {
       echo "They are not the same.";
   }
 ?>

try this... 尝试这个...

strcmp function Returns < 0 if str1 is less than str2; strcmp函数如果str1小于str2,则返回<0;否则,返回0。 > 0 if str1 is greater than str2, and 0 if they are equal. 如果str1大于str2,则> 0;如果相等,则> 0。

if (strcmp($x, $y) === 0) {
          echo "They are the same.";
       } else {
           echo "They are not the same.";
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM