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

How to use function to set variable type in php

醉折花枝作酒筹
Release: 2023-03-11 22:08:02
Original
2131 people have browsed it

Starting from this time, we will start to introduce some more practical or interesting functions. This article will introduce to you how to use functions to set variable types. You can refer to them if necessary.

In PHP, we often need to set a specified type for a variable, but not everyone knows how to set the type of the variable. Today we will learn how to set the type for the variable.

First let’s look at a little chestnut.

<?php
$foo = "hello"; // string
$bar = true;   // boolean
var_dump($foo);
var_dump($bar);
settype($foo, "float");
settype($bar, "array");
var_dump($foo);
var_dump($bar);
?>
Copy after login

The result of this example is

How to use function to set variable type in php

#When we use the settype function, the type of the variable will change, and the variable will be output , the type of the variable has not changed back to its original type.

Then let’s take a closer look at this function.

The settype() function is used to set the type of a variable.

To use the settype() function, you must first pass it a variable to be changed, and a string containing a type in the type list. If the number we need to change was originally a double precision type, and now changes it to an integer or other type, then if the value is set to a double precision type, the lost precision will not be returned.

Therefore, when we set the variable type, we need to set it carefully. Once the setting is wrong, all the original types will not be restored again.

Let’s take a look at the syntax of this function.

bool settype (mixed &$var , string $type)
Copy after login

This $var means the variable to be converted.

This$type means the value we may set.

This value includes:

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

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

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

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0)

Of course the return value of this function It is also very important.

This function will return TRUE when successfully set, and FALSE when failed.

That’s all. If you want to know anything else, you can click here. → →php video tutorial

The above is the detailed content of How to use function to set variable type in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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