Three ways of data type conversion in PHP, three ways of data type_PHP tutorial

WBOY
Release: 2016-07-13 09:58:13
Original
878 people have browsed it

Three ways of data type conversion in PHP, three ways of data type

PHP data type conversion is a forced conversion. The PHP data types that are allowed to be converted are:

1. (int), (integer): Convert to integer
2. (float), (double), (real): Convert to floating point type
3. (string): Convert to string
4. (bool), (boolean): Convert to Boolean type
5. (array): Convert to array
6. (object): Convert to object

There are three conversion methods for PHP data types:

1. Add the target type enclosed in parentheses before the variable to be converted
2. Use three specific types of conversion functions, intval(), floatval(), strval()
3. Use the general type conversion function settype(mixed var, string type)

The first conversion method: (int) (bool) (float) (string) (array) (object)
Copy code The code is as follows:
$num1=3.14;
$num2=(int)$num1;
var_dump($num1); //Output float(3.14)
var_dump($num2); //Output int(3)
?>

The second conversion method: intval() floatval() strval()
Copy code The code is as follows:
$str="123.9abc";
$int=intval($str); //Converted value: 123
$float=floatval($str); //Converted value: 123.9
$str=strval($float); //Converted string: "123.9"
?>

The third conversion method: settype();
Copy code The code is as follows:
$num4=12.8;
$flg=settype($num4,"int");
var_dump($flg); //Output bool(true)
var_dump($num4); //Output int(12)
?>

A journey of a thousand miles begins with a single step. Change the future, starting now. Changing the present is changing the future.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/978383.htmlTechArticleThree ways of data type conversion in PHP, three ways of data type PHP data type conversion is a forced conversion. The PHP data types that allow conversion are: 1. (int), (integer): Convert...
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