Home > Backend Development > PHP Problem > Technical Answer: How to view and determine PHP data types (Learning Sharing)

Technical Answer: How to view and determine PHP data types (Learning Sharing)

WBOY
Release: 2023-03-13 09:04:01
Original
2395 people have browsed it

In the previous article, I brought you "Get PHP's if else syntax and NULL data type in 5 minutes". Today I will continue to explain PHP knowledge to you and introduce it to you. View and judge PHP data types. Hope it helps everyone!

Technical Answer: How to view and determine PHP data types (Learning Sharing)

Viewing and judging PHP data types

Viewing data types

We In learning, different operations can be performed based on different data types, so how to check a data type is particularly important. Only when we know the type of data can we proceed to the next step. Next, I will introduce to you two functions for viewing data types.

  • <strong>gettype</strong>(pass in a variable) can get the type of the variable

Gettype passes in a declared variable and the variable type of the variable can be passed out. For example:

<?php
  //声明一个变量10086,可以自己多试几次换成其他类型看看$b输出是多少
  $a = 10086;
  $b = gettype($a);
  echo $b;
?>
Copy after login

Output result:

Technical Answer: How to view and determine PHP data types (Learning Sharing)

It can be seen that gettypt() can get the variable type of the variable, but it needs to be displayed by echo. Is there a function that can directly output the variable type?

  • <strong>var_dump</strong>(Pass in a variable) Output variable type and value

var_dump can not only directly output the variable type of the variable, but also output the value of the variable. For example:

<?php
//可以自己多换几个类型试试
$a = &#39;好好学习,天天向上&#39;;
var_dump($a);
?>
Copy after login

Output result:

Technical Answer: How to view and determine PHP data types (Learning Sharing)

It can be seen that var_dump() can not only directly output the variable type of the variable, It is also possible to directly output the value of a variable.

Judge data type

In daily use and learning, we must not only check the data type of the variable, but also judge. There are not many data types of variables, and it is very simple to judge. You need to use the is_* series of functions.

The reason why it is called a series function is that it does not exist alone. Since you want to judge whether something is of a certain type, it often appears together with the variable type that needs to be judged.

If it is this type, it returns true, if it is not this type, it returns false.

  • is_bool()Whether it is a Boolean type

If the variable is a Boolean type, the return result is true and the output True interval; if the variable is not of Boolean type, the return result is false and the false interval is output.

<?php
//赋值变量flse是布尔型
$a = false;
if(is_bool($a)){
    echo &#39;好好学习&#39;;
}else{
    echo &#39;天天向上&#39;;
}
?>
Copy after login

Output result:

Technical Answer: How to view and determine PHP data types (Learning Sharing)

The output result is: study hard. Outputs the true interval, returns true, and the variable is of Boolean type.

  • is_null Whether it is empty type

If the variable is empty type, the return result is true and the true range is output ; If the variable is not of empty type, the return result is false and the false interval is output.

<?php
//并未对$a赋值,$a无意义是空型,(is_null($a)判断变量是否为空型
if(is_null($a)){
    echo &#39;好好学习&#39;;
}else{
    echo &#39;天天向上&#39;;
}
?>
Copy after login

Output result:

Technical Answer: How to view and determine PHP data types (Learning Sharing)

The output result is: study hard. Output the true range, return the result as true, and the variable is of empty type.

Also:

  • is_int Is it an integer?

  • is_float Whether it is a floating point

  • is_string Whether it is a string

  • is_array Whether it is an array

  • is_object Whether it is an object

  • is_resource Whether it is a resource

  • is_scalar Whether it is a scalar number

  • is_numeric Whether it is a numeric type

  • is_callable Whether it is a function

They have the same syntax as the two examples I just listed. The is_* series of functions are not difficult and easy to remember. I believe everyone can Be proficient in it.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Technical Answer: How to view and determine PHP data types (Learning Sharing). 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