Home > Backend Development > PHP Problem > There are several forms of PHP coercion

There are several forms of PHP coercion

Guanhui
Release: 2023-03-01 08:52:01
Original
2927 people have browsed it

There are several forms of PHP coercion

There are several forms of forced conversion in PHP

There are three forms of forced conversion in PHP: 1. Add the expression before the variable to be converted. The target type enclosed in parentheses; 2. Use the conversion function, such as "intval()", "strval()"; 3. Use the "settype()" function, just pass the variable to be converted into the first parameter and Just pass the second parameter into the data type name.

Sample code

$a = '12';
$res = (int)$a;
var_dump($a);  //原变量不影响
var_dump($res);//返回的值为整型
Copy after login
$a = '12';
$res = intval($a);
var_dump($a); //不改变原变量
var_dump($res);
Copy after login
$a = '12';
settype($a,'integer');
var_dump($a); // 改变原变量 返回值是一个布尔型
Copy after login

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of There are several forms of PHP coercion. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template