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

There are several forms of PHP coercion

Guanhui
GuanhuiOriginal
2020-05-21 17:28:332844browse

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);//返回的值为整型
$a = '12';
$res = intval($a);
var_dump($a); //不改变原变量
var_dump($res);
$a = '12';
settype($a,'integer');
var_dump($a); // 改变原变量 返回值是一个布尔型

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!

Statement:
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