Home > Backend Development > PHP Tutorial > PHP中比较两个字符串找出第一个不同字符位置例子_php实例

PHP中比较两个字符串找出第一个不同字符位置例子_php实例

WBOY
Release: 2016-05-17 08:46:19
Original
1002 people have browsed it

一般的做法就会这样:

复制代码 代码如下:

for ($offset = 0; $offset     if ($str1[$offset] !== $str2[$offset]) {
        return $offset;
    }
}

而问题下面给出的最佳答案是用异或操作符( ^ ),以前从来没用过这个操作符,也不知道能用到什么地方,今天算是学到。

因为一般情况下,当你对两个字符串进行异或操作的时候,相同的字符的异或结果是null(“

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template