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().

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:
<?php $num1=3.14; $num2=(int)$num1; var_dump($num1); //输出float(3.14) var_dump($num2); //输出int(3) ?>
Method 2: Use a specific type conversion function , intval(), floatval(), strval()
<?php $str="123.9abc"; $int=intval($str); //转换后数值:123 $float=floatval($str); //转换后数值:123.9 $str=strval($float); //转换后字符串:"123.9" ?>
Method 3: Use the general type conversion function settype()
<?php $num4=12.8; $flg=settype($num4,"int"); var_dump($flg); //输出bool(true) var_dump($num4); //输出int(12) ?>
Instructions: settype( ) function is used to set the type of a variable. The syntax is as follows:
settype ( $var , $type )
Set the type of variable var to type.
| Parameters | Description |
|---|---|
| ##var | To be converted Variables. The possible values of|
| type |
##type are:
|
Recommended learning: "
PHP Video TutorialThe 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.






