Home > Backend Development > PHP Tutorial > PHP data type conversion_PHP tutorial

PHP data type conversion_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:31:26
Original
1059 people have browsed it

This article mainly shares the knowledge of PHP data type conversion.

PHP data type conversion is a forced conversion. The PHP data types that are allowed to be converted 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

PHP data types have three conversion methods:

(1) Add the target type enclosed in parentheses before the variable to be converted, for example:

(int) (bool) (float) (string) (array) (object) The following is an example:

<?php
$num1=3.14;
$num2=(int)$num1; //强制转换为int类型
var_dump($num1); //输出float(3.14)
var_dump($num2); //输出int(3)
Copy after login

(2) Use three specific types of conversion functions, intval(), floatval(), strval(). Examples are as follows:

<?php
$str="123.9abc";
$int=intval($str); //转换后数值:123
$float=floatval($str); //转换后数值:123.9
$str=strval($float); //转换后字符串:"123.9"
Copy after login

(3) Use the general type conversion function settype(mixed var, string type). The specific examples are as follows:

<?php
$num4=12.8;
$flg=settype($num4,"int");
var_dump($flg); //输出bool(true)
var_dump($num4); //输出int(12)
Copy after login

Articles you may be interested in

  • A summary of how to use curl’s post to submit data and get to get web page data in php
  • How to use php Convert the br newline character in html to the newline character in text input
  • How to convert a multi-dimensional array into a one-dimensional array in php
  • Reset the auto-increment starting value for various MySQL data table types Methods
  • php mysql database operation class
  • Powerful PHP image processing class (watermark, transparency, zoom, sharpen, rotate, flip, cut, invert color)
  • php uses array_flip to implement array key-value exchange to remove array duplicate values
  • Introduction to the union, intersection and difference functions of arrays in php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764075.htmlTechArticleThis article mainly shares the knowledge of PHP data type conversion. PHP data type conversion is a forced conversion. The PHP data types that are allowed to be converted are: (int), (integer): converted to...
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 Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template