Home >Backend Development >PHP Problem >How to force conversion of data types in php

How to force conversion of data types in php

王林
王林Original
2020-08-14 14:48:283005browse

How to force conversion of data types in php: Add the target type enclosed in parentheses before the variable to be converted to complete the forced type conversion, such as: [(int), (integer)] converted to Integer type; [(string)] is converted into a string.

How to force conversion of data types in php

In PHP, the forced type conversion can be completed by adding the target type enclosed in parentheses before the variable to be converted.

(Recommended tutorial: php graphic tutorial)

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

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

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

  • (string) Convert to string

  • (array) Convert to array

  • (object) Convert to object

(Video tutorial recommendation: php video tutorial)

Example:

<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>

The above is the detailed content of How to force conversion of data types in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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