How to determine variable type in PHP function?

王林
Release: 2024-04-17 12:12:01
Original
1016 people have browsed it

In PHP, you can obtain the variable type through the gettype() function, which returns type information in the form of a string, such as string, integer, etc. In addition, the is_* function series can be used to determine specific types, such as is_string() to determine whether it is a string.

如何在 PHP 函数中判断变量类型?

#How to determine variable type in PHP function?

In PHP, the most convenient way to determine the type of a variable is to use the gettype() function. This function returns a string representing the type of the variable. Here are some examples:

$variable = 'string';
echo gettype($variable); // 输出:string

$variable = 123;
echo gettype($variable); // 输出:integer

$variable = 123.45;
echo gettype($variable); // 输出:double

$variable = true;
echo gettype($variable); // 输出:boolean

$variable = [];
echo gettype($variable); // 输出:array

$variable = new stdClass();
echo gettype($variable); // 输出:object
Copy after login

In addition to the gettype() function, PHP also provides the is_* family of functions for testing specific types. For example:

$variable = 'string';
if (is_string($variable)) {
  echo '变量是字符串';
}
Copy after login

Practical case

Suppose we have an array containing elements of different types, and we want to classify the array based on the type. We can use the following function:

function categorizeVariables(array $variables): array
{
  $categorizedVariables = [];

  foreach ($variables as $key => $variable) {
    switch (gettype($variable)) {
      case 'string':
        $categorizedVariables['strings'][$key] = $variable;
        break;
      case 'integer':
        $categorizedVariables['integers'][$key] = $variable;
        break;
      case 'double':
        $categorizedVariables['doubles'][$key] = $variable;
        break;
      case 'boolean':
        $categorizedVariables['booleans'][$key] = $variable;
        break;
      case 'array':
        $categorizedVariables['arrays'][$key] = $variable;
        break;
      case 'object':
        $categorizedVariables['objects'][$key] = $variable;
        break;
    }
  }

  return $categorizedVariables;
}
Copy after login

In the above example, the categorizeVariables function will return a function that classifies the array elements into different categories (String, Integer, Double, Boolean , arrays and objects) associative arrays.

The above is the detailed content of How to determine variable type in PHP function?. 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