How to change data type in php

青灯夜游
Release: 2023-03-09 15:12:01
Original
5146 people have browsed it

Methods to change the data type: 1. Forced type conversion, add the target type enclosed in parentheses before the variable to be converted. Example "(int)3.14"; 2. Use the specific type conversion functions intval(), floatval() and strval(); 3. Use the general type conversion function settype().

How to change data type in php

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

Method 1. Forced type conversion- -Add the target type enclosed in parentheses

before the variable to be converted. The PHP data types allowed for conversion are:

  • (int), ( integer): Convert to integer

  • (float), (double), (real): Convert to floating point type

  • (string) : Convert to string

  • (bool), (boolean): Convert to Boolean type

  • (array): Convert to array

  • (object): Convert to object

Example:

Copy after login

Method 2: Use a specific type conversion function , intval(), floatval(), strval()

Copy after login

Method 3: Use the general type conversion function settype()

Copy after login

Instructions: settype( ) function is used to set the type of a variable. The syntax is as follows:

settype ( $var , $type )
Copy after login

Set the type of variable var to type.

To be converted Variables. The possible values of settype() function will affect The type of the original variable.
Parameters Description
##var
type
##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)

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to change data 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!