Home  >  Article  >  Backend Development  >  Example of comparing two strings to find the first different character position in PHP_PHP Tutorial

Example of comparing two strings to find the first different character position in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:28774browse

The general approach is like this:

Copy the codeThe code is as follows:

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

The best answer given below is to use the XOR operator (^). I have never used this operator before, and I don’t know where it can be used. Today It's learned.

Because generally, when you perform an XOR operation on two strings, the XOR result of the same characters is null("

http://www.bkjia.com/PHPjc/750855.htmlwww.bkjia.com
true

http: //www.bkjia.com/PHPjc/750855.html

TechArticle
The general approach is like this: Copy the code as follows: ?php for ($offset = 0; $offset $ length; ++$offset) { if ($str1[$offset] !== $str2[$offset]) { return $offset; } } And the question...
Statement:
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