PHP determines variable type

怪我咯
Release: 2023-03-10 20:02:02
Original
21580 people have browsed it
Recommended Manual: php complete self-study manual

##php Commonly used functions for judging variables include gettype(), is_array(), is_bool(), is_float(), is_integer(), is_null(), is_numeric(), is_object(), is_resource(), is_scalar() and is_string() .

The gettype() function returns the type of the variable, such as "boolean", "integer", "double" (the float type will return "double" instead of "float"), "string", "array" ", "object", "resource", "NULL" and "unknown type" and other values ​​indicate the variable type, such as:

$var = "coding";
echo gettype($var);//string
Copy after login

while other functions confirm a certain variable type and return true or false, for example:

$var1=321;
var_dump(is_numeric($var1));//bool(true)
$var2="123";
var_dump(is_numeric($var2));//bool(true)
Copy after login

Note: is_numeric() is used to determine whether the variable is a number or a numeric string

Attachment: type conversion

If you say gettype( ) is used to obtain the variable type, then settype() is obviously to set the variable type. Here are several type conversion methods:

1.settype($var,type) function conversion

$var='124das.21321';
settype($var,'int');//第二个参数指定转换类型,可以是int、float、bool、string、array和object
var_dump($var);//int 124
Copy after login

2. Target type specified conversion

(int)$var;//括号内指定转换类型,可以是int、float、bool、string、array和object
Copy after login

3. Three specific type function conversion

intval($var) , floatval($var) and strval($var)

$var = '121.212sdsa';
var_dump(floatval($var)); // float 121.212
Copy after login

Recommended related articles: ​       1.
How does PHP use the gettype() function to determine the type of a variable? 2.
How does php determine the file type 3.
php implementation to determine client type
Related video recommendations: 1.
Dugu Jiujian (4)_PHP video tutorial

The above is the detailed content of PHP determines variable type. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!