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

青灯夜游
Release: 2023-03-14 12:18:02
Original
1884 people have browsed it

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)
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template