Home  >  Article  >  Backend Development  >  How to compare strings using PHP operator==_PHP Tutorial

How to compare strings using PHP operator==_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:30:33791browse

The output result of the above code is:

bool(true) Therefore, when comparing strings, it is recommended to use the PHP operator == to compare strings Check strictly, or use functions such as strcmp() to avoid possible problems.

In addition, the commonly used in_array() function also has weak type problems, see the following code:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>var_dump(in_array('01', array('1')));  </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>

The output result of the above code is:

bool(true)

I believe that PHP programmers who have used this function for security checks know what kind of security problems this will cause, right? Fortunately, the in_array() function provides us with a third parameter. Setting it to true can turn on the mandatory type checking mechanism of the in_array() function, as shown in the following code:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php   </span></span></li><li><span>var_dump(in_array('01', array('1'), true));   </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>

The output result is:

bool(false)

Since PHP is a weakly typed language, that is to say, the concept of data type is weakened in PHP. Therefore, if you ignore data types too much when programming (which is also a common problem among most PHP programmers), some problems will occur and even security vulnerabilities will occur. At the end of the introduction of the PHP operator ==, there is still the annoying and annoying sentence, strict inspection and filtering of external data.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446284.htmlTechArticleThe output result of the above code is: bool(true) Therefore, when comparing strings, it is recommended to use PHP Operator == does a strict check on the string, or use functions like strcmp(), from...
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