Home >Backend Development >PHP Problem >What is the permanent conversion function of php data type

What is the permanent conversion function of php data type

青灯夜游
青灯夜游Original
2021-12-10 18:32:591910browse

The permanent conversion function of PHP data type is "settype()". The settype() function is used to set the type of a variable and can permanently change the data type of the variable itself; the syntax is "settype ($var, $type)" and the parameter "$type" specifies the target data type to be converted.

What is the permanent conversion function of php data type

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

In PHP, the function to realize data type conversion There are many: intval(), floatval(), boolval(), strval(), settype().

However, the forced type conversion of intval(), floatval(), boolval(), and strval() functions will not change the type of the converted variable itself, but will convert the new type of data obtained When assigned to a new variable, the type and value of the original variable remain unchanged. This cannot be a permanent conversion.

And settype() can permanently change the data type of the variable itself. Syntax:

settype ($var,$type)

will set the type of the variable $var to $type. Possible values ​​for

$type are:

  • "boolean" (or "bool" since PHP 4.2.0)

  • "integer" (or "int" since PHP 4.2.0)

  • "float" (only since PHP 4.2.0 Can be used, for "double" used in older versions is now deprecated)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0 onwards)

Example:

<?php
$foo = "5bar"; // string
$bar = true;   // boolean
var_dump($foo);
var_dump($bar);
settype($foo, "integer"); // $foo 现在是 5   (integer)
settype($bar, "string");  // $bar 现在是 "1" (string)
var_dump($foo);
var_dump($bar);
?>

What is the permanent conversion function of php data type

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the permanent conversion function of php data type. 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