Home  >  Article  >  Backend Development  >  What are the methods of forced type conversion in php

What are the methods of forced type conversion in php

青灯夜游
青灯夜游Original
2022-04-26 15:21:085682browse

Forcing conversion method: 1. Add the target type "(int)", "(bool)", "(float)", etc. enclosed in parentheses before the variable to be converted, and it will be converted For the corresponding type; 2. Use type conversion functions "intval (variable)", "floatval (variable)", "strval (variable)", etc.

What are the methods of forced type conversion in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Data type conversion refers to converting variables or values ​​from Convert one data type to other data types. There are two methods of conversion, namely automatic type conversion and forced type conversion.

The following article will introduce to you the forced type conversion method of PHP.

There are two ways to force type conversion in PHP:

  • You can add parentheses before the variable to be converted. Target type (such as $var = (int)3.1415;),

  • can use specific type conversion functions (such as intval(), floatval(), strval (), etc.) or settype() to convert the type.

1. Add the target type enclosed in parentheses before the variable to be converted.

The variable types allowed in the parentheses are as follows: Display:

  • (int), (integer): Convert to integer type;

  • (bool), (boolean): Convert to Boolean Type;

  • (float), (double), (real): converted to floating point type;

  • (string): converted to String type;

  • (array): converted to array type;

  • (object): converted to object type.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;123.456abc&#39;;
echo &#39;原变量 $str 的类型为:&#39;.gettype($str)."<br>";
$int = (int)$str;
echo &#39;变量 $int 的类型为:&#39; . gettype($int) . &#39;<br>&#39;;
$float = (float)$str;
echo &#39;变量 $float 的类型为:&#39; . gettype($float) . &#39;<br>&#39;;
$bool = (bool)$str;
echo &#39;变量 $bool 的类型为:&#39; . gettype($bool). &#39;<br>&#39;;
$arr = (array)$str;
echo &#39;变量 $arr 的类型为:&#39; . gettype($arr);
?>

What are the methods of forced type conversion in php

2. Use specific type conversion functions

  • intval(): used to get the integer value of the variable;

  • floatval(): used to get the floating point value of the variable;

  • boolval(): used to get the Boolean value of the variable;

  • strval(): used to get the string value of the variable;

  • settype(variable, "type"), used to convert the variable to the specified type.

Example 1:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;123.456abc&#39;;
echo &#39;原变量 $str 的类型为:&#39;.gettype($str)."<br>";
$int = intval($str);
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
$float = floatval($str);
echo &#39;变量 $float 的类型为:&#39;.gettype($float).&#39;<br>&#39;;
$bool = boolval($str);
echo &#39;变量 $bool 的类型为:&#39;.gettype($bool);
?>

What are the methods of forced type conversion in php

Example 2:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$str = &#39;123.456abc&#39;;
echo &#39;原变量的类型为:&#39;.gettype($str)."<br>";

settype($str, &#39;integer&#39;);
echo &#39;变量类型变为:&#39;.gettype($str).&#39;<br>&#39;;

settype($str, &#39;boolean&#39;);
echo &#39;变量类型变为:&#39;.gettype($str).&#39;<br>&#39;;

settype($str, &#39;null&#39;);
echo &#39;变量类型变为:&#39;.gettype($str);
?>

What are the methods of forced type conversion in php

Note: The settype() function will change the original variable itself; it returns TRUE when the setting is successful, and returns FALSE when it fails.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the methods of forced type conversion in php. 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