Home  >  Article  >  Backend Development  >  What are the PHP data type conversion functions?

What are the PHP data type conversion functions?

青灯夜游
青灯夜游Original
2021-06-30 19:16:483277browse

php data type conversion function: 1. intval(), converted to integer type; 2. floatval(), converted to floating point type; 3. boolval(), converted to Boolean type; 4. strval( ), converted into a string type; 5. settype(), can be converted into a user-specified data type.

What are the PHP data type conversion functions?

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

php data type conversion Function

Specific conversion functions intval(), floatval(), boolval(), strval(), settype(), the functions of these functions are as follows:

  • 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(): used to set the type of variables.

Example 1:

<?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

intval(), floatval(), boolval() When the strval() function converts types, it will not change the type of the converted variable itself. Instead, it will assign the converted new type of data to the new variable, and the type and value of the original variable will remain unchanged.

Example 2:

<?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

settype() function will change the type of the variable itself, the syntax format is:

settype(mixed &$var, string $type)

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the PHP data type conversion functions?. 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