Home >Backend Development >PHP Problem >How to set variable type in php

How to set variable type in php

青灯夜游
青灯夜游Original
2021-11-11 18:45:562869browse

In php, you can use the settype() function to set the variable type, the syntax is "settype($var,"data type")"; the data type value can be "boolean", "integer", "float" , "string", "array", "object", "null".

How to set variable type in php

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

In php, you can use settype () function to set the variable type.

The settype() function is used to set the data type of a variable. (Version requirements: PHP 4, PHP 5, PHP 7)

Syntax:

settype ( $var ,  $type ) //将变量var类型设置成type类型

Possible values ​​of $type are:

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

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

  • "float" (only available after PHP 4.2.0, "double" used in older versions is now deprecated)

  • "string "

  • "array"

  • "object"

  • "null" (from PHP 4.2 .0)

Return value: TRUE when the setting is successful, FALSE when the setting fails.

Example:

<?php
$foo = "5bar"; // string
$bar = true;   // boolean

settype($foo, "integer"); // $foo is now 5   (integer)
settype($bar, "string");  // $bar is now "1" (string)
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to set variable type 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