How to use implicit conversion of in_array

php中世界最好的语言
Release: 2023-03-22 08:44:02
Original
1564 people have browsed it

This time I will show you how to use the implicit conversion of in_array, what are the precautions when using the implicit conversion of in_array, the following is a practical case, let's take a look.

Question

When writing an interface today, you need to pass in a large number of basic information parameters. The parameters are of two types: int and string. For the convenience of verification , I plan to put all the parameters in the array, and then use in_array(0, $param) to determine whether the int parameter is 0, and then separately determine whether the string parameter is empty. The sample code is as follows:

      if(in_array(0, $param) || $param['img'] == '') {
        $this->errorCode = 10030;
        $this->errorMessage = '参数不正确';
        return false; 
      }
Copy after login

But During the self-test, I found that if the correct parameters are passed in, a prompt indicating that the parameters are incorrect will be returned! ! !

Reason

This situation occurs precisely because in_array is causing trouble. in_array(search,array) is equivalent to combining each value in the array with search Comparison, since in addition to the int parameter in my $param array, there is also a string parameter, which is equivalent to using string and int to compare. PHP's implicit conversion rules:

non-numeric characters String is compared with an integer, and the string is automatically converted to int(0)

The following example verifies our statement:

<?php
  $a = (int)'abc';
  var_dump($a); //int(0)
  $c = array(0,1,2,3);
  if(in_array('abc', $c)) {
    echo 'exist';
  } else {
    echo 'not exist';
  } //exist
Copy after login

Solution

in_array adds a third parameter true, which is used to check whether the type of the searched data is the same as the value of the array. In this way, the function can only be used when the element exists in the array and the data The type will return true only when it is the same as the given value

For the business I presented above, you can be more rigorous and store int type data in an array and string in an array, two arrays of different types. Perform data verification separately, so that the above problems will not occur

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use the recursive function of PHP

Data crawling of Tmall and Taobao products

Implementation of Laravel password reset in PHP

The above is the detailed content of How to use implicit conversion of in_array. For more information, please follow other related articles on the PHP Chinese website!

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