Home  >  Article  >  Backend Development  >  How to force data type conversion in php

How to force data type conversion in php

青灯夜游
青灯夜游Original
2021-11-18 18:03:172894browse

Forced conversion method: 1. Add the target type enclosed in parentheses ("(int)", "(bool)", "(float)", etc.) before the variable to be converted; 2. Use type conversion functions intval(), floatval(), boolval(), strval(), settype().

How to force data type conversion in php

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

PHP forced type conversion

Coercive type conversion in PHP is very similar to other languages. You can add the target type enclosed in parentheses before the variable to be converted (for example, $var = (int )3.1415;), you can also use specific type conversion functions (such as intval(), floatval(), strval(), etc.) or settype() to convert the type.

When the variable type is cast by adding the target type enclosed in parentheses before the variable, the variable types allowed in the parentheses are as follows:

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

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

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

  • (string): Convert to string type;

  • (array): Convert to array type;

  • (object): Convert to object type.

Use specific conversion functionsintval()floatval()boolval() strval() When converting the type of a variable, the functions of these functions are as follows:

  • intval(): used to obtain 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 a variable.

[Example] Use the above functions to force the type of variables.

<?php
    $str = &#39;123.456abc&#39;;
    $int = intval($str);
    echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
    $float = floatval($str);
    echo &#39;变量 $float 的类型为:&#39;.gettype($float).&#39;<br>&#39;;
    $string = strval($str);
    echo &#39;变量 $string 的类型为:&#39;.gettype($string);
?>

The running results are as follows:

变量 $int 的类型为:integer
变量 $float 的类型为:double
变量 $string 的类型为:string

The two forced type conversion methods described above will not change the type of the converted variable itself, but will convert the new type of data obtained by the conversion. When assigned to a new variable, the type and value of the original variable remain unchanged.

If you need to change the type of the variable itself, you can use the settype() function. The syntax format of the function is as follows:

settype(mixed &$var, string $type)

Among them, $var is the variable to be converted. ; $type is the type to be converted, which can be boolean (bool) , integer (int) , float (double) , string, array, object, null.

[Example] Use the settype() function to force the type of a variable.

<?php
    $str = (string)&#39;123abc&#39;;
    $bool = (bool)true;
    settype($str, &#39;integer&#39;);
    settype($bool, &#39;string&#39;);
    echo &#39;$str 的类型为:&#39;.gettype($str).&#39;<br> $bool 的类型为:&#39;.gettype($bool);
?>

The running results are as follows:

$str 的类型为:integer
$bool 的类型为:string

There are the following points to note when using forced type conversion:

  • Integer type is converted to floating point type. The precision range of floating point type is much larger than that of integer type, so the precision after conversion will not change;

  • When floating point type is converted to integer type, the decimal part will be automatically discarded and only the integer will be retained. part. If a floating point number exceeds the valid range of integer numbers, the result will be undefined;

  • When a string is converted to a number, it is from the beginning of the string to the first one that is not a number character ends (the non-matching content is cleared), that is to say, if the first character of the string is not a number, the converted result is 0;

  • #NULL value is converted to a string , the result is a null character.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to force data 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