Teach you how to use PHP functions to detect the type of variables

青灯夜游
Release: 2023-04-10 14:24:02
Original
5782 people have browsed it

Variables are the core of PHP. Before we operate a variable, we need to first figure out what type the variable belongs to. So how to detect variable type in PHP? The following article will introduce to you several functions of PHP to detect variable types.

PHP provides many functions for detecting data types, but they can be roughly divided into two types: gettype() function and is_* class function. Let's take a look at it through a code example.

Let’s take a look at the following example first:

<?php
echo gettype(102) ."<br>";
echo gettype(true) ."<br>";
echo gettype(&#39; &#39;) ."<br>";
echo gettype(null) ."<br>";
echo gettype(array()) ."<br>";
echo gettype(new stdclass());
?>
Copy after login

Look at the output:

Teach you how to use PHP functions to detect the type of variables

The gettype() function can obtain and return The type of variable, the return value can be:

  • boolean

  • ##integer

  • double (Since PHP 4, if it is a float, it will return "double" instead of "float")

  • string

  • array

  • ##object

  • resource

    (resource) resource is a special variable type that saves a reference to an external resource; it mainly describes a PHP extension resource. Resources are created and used through specialized functions.


  • NULL

    NULL is also a special data type. It has only one value, NULL, which means a null value (i.e. The variable has no value).


  • unknown type

  • gettype() function contains comparison of strings, so the operation is slower. In addition to this function, we can also use the
is_*

series of functions to detect the type of variables. Let’s take a look at the following example:

<?php
header("Content-type:text/html;charset=utf-8");
$num1 = 123456;
$arr = array(1, 2, 3, 4, 5, 6, 7);
$bool = false;
$str1 = NULL;
$str2 = &#39;654321&#39;;
$float = 3.1415926;
$str3 = &#39;hello!&#39;;
class foo {
	function dosomething() {
		echo "你好!";
	}

}

$obj = new foo();
if (is_array($arr))
	echo &#39;$arr 是数组&#39;;
echo &#39;<br>&#39;;
if (!is_array($num1))
	echo &#39;$num1 不是数组&#39;;
echo &#39;<br>&#39;;
if (is_numeric($str2))
	echo &#39;$str2 是数字&#39;;
echo &#39;<br>&#39;;
if (is_bool($bool))
	echo &#39;$bool 是数字&#39;;
echo &#39;<br>&#39;;
if (is_float($float))
	echo &#39;$float 是浮点类型&#39;;
echo &#39;<br>&#39;;
if (!is_float($str2))
	echo &#39;$str2 不是浮点类型&#39;;
echo &#39;<br>&#39;;
if (is_int($num1))
	echo &#39;$num1 是整数类型&#39;;
echo &#39;<br>&#39;;
if (!is_int($float))
	echo &#39;$float 不是整数类型&#39;;
echo &#39;<br>&#39;;
if (is_string($str2))
	echo &#39;$str2 是字符串&#39;;
echo &#39;<br>&#39;;
if (is_object($obj))
	echo &#39;$obj 是一个对象&#39;;
echo &#39;<br>&#39;;
if (!isset($str1))
	echo &#39;$str1 未定义或则值为 NULL&#39;;
?>
Copy after login

Look at the output:

Teach you how to use PHP functions to detect the type of variablesis_* Multiple functions with different functions in the series Functions that can be used to check the data type of variables individually. Let’s take a look at several commonly used checking functions:

1. is_int(): Check whether the variable

$var

is an integer type (integer), if $varIf yes, TRUE is returned, otherwise FALSE is returned; the syntax is "is_int($var)", and the alias is is_integer(). 2. is_float(): Check whether the variable

$var

is a floating point type. If $var is, return TRUE, otherwise return FALSE; syntax "is_float($var)", aliased as is_real(). 3. is_string(): Check whether the variable

$var

is of string type. If $var is, return TRUE, otherwise return FALSE; syntax "is_string($var)". 4. is_array(): Check whether the variable

$var

is an array. If $var is, return TRUE, otherwise return FALSE; syntax "is_array ($var)". 5. is_object(): Check whether the variable

$var

is an object. If $var is, return TRUE, otherwise return FALSE; syntax " is_object($var)". 6, is_numeric(): Check whether the variable

$var

is a number or a numeric string. If $var is, return TRUE, otherwise return FALSE; syntax " is_numeric($var)”. Okay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:

PHP function array array function video explanation

, come and learn!

The above is the detailed content of Teach you how to use PHP functions to detect the type of variables. For more information, please follow other related articles on the PHP Chinese website!

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