How to set variable type in php

青灯夜游
Release: 2023-03-13 19:44:01
Original
2807 people have browsed it

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类型
Copy after login

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

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!

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