Detailed explanation of three methods of PHP data type conversion

Jennifer Aniston
Release: 2023-03-08 22:34:01
Original
6444 people have browsed it

There are three conversion types for data, namely forced conversion, permanent conversion, and conversion function conversion. This article introduces these three conversion methods to give you an in-depth understanding of type conversion. Let's learn with the editor together.

Detailed explanation of three methods of PHP data type conversion

The first type, forced conversion

The data passed through forced conversion does not affect the original type of the data, but only changes the data Temporarily converted.

'; echo gettype(strval(500)),'--',gettype(500),'
'; echo gettype(strval(true)),'--',gettype(true),'
'; ?>
Copy after login

The result is:

string--integer string--integer string--boolean
Copy after login

You can see that although we have converted the data type, what type was the original data and what type is it still? , the original type is not changed due to type conversion.

Second type, permanent conversion

'; $current=gettype(settype($old,'string')); echo "现类型". gettype($current),'
'; ?>
Copy after login

The result is:

原类型integer 现类型string
Copy after login

By permanently converted data, The original type of its data has also changed.

echo gettype($old);
Copy after login

The result is:

string
Copy after login

Through the above case, we can see that after we convert the data, our original data type also changes.

The third type, conversion function conversion

Convert through the three conversion functions intval() floatval() strval(), which can be converted into different types according to needs.

'; echo floatval($str),'--',gettype($str),'
'; echo strval($str),'--',gettype($str),'
'; ?>
Copy after login

Let us take a look at his output:

123--string 123.9--string 123.9abc--string
Copy after login

You can see different conversion functions, converted into different types, and the original The string type does not change.

Recommended: "PHP Video Tutorial"

The above is the detailed content of Detailed explanation of three methods of PHP data type conversion. 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
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!