Home > Backend Development > PHP Tutorial > Data type coercion in php_PHP tutorial

Data type coercion in php_PHP tutorial

WBOY
Release: 2016-07-13 17:13:21
Original
1214 people have browsed it

PHP is a loosely typed language. There is no need to specifically define the variables used, which brings great flexibility and convenience to program writing. But when we write programs, we need to know what type of variables we use, because variables always have a type corresponding to them. Although you can almost freely convert between types, if you use or convert variable types arbitrarily, it may lead to some potential errors

Type coercion in PHP is very similar to that in C: add before the variable to be converted Target type enclosed in parentheses:

The code is as follows Copy code
 代码如下 复制代码

$foo = 10; // $foo 为整型
$bar = (boolean) $foo; // $bar 为布尔型
?>

$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is Boolean ?>

Type conversion

PHP does not require (or support) explicit type definitions in variable definitions; the variable type is determined based on the context in which the variable is used. That is, if you assign a string value to the variable var, var becomes a string. If you assign an integer value to var, it becomes an integer.
An example of PHP's automatic type conversion is the plus sign "+". If any operand is a floating point number, all operands are treated as floating point numbers, and the result is also a floating point number. Otherwise the operands are interpreted as integers and the result is also an integer. Note that this does not change the types of the operands themselves; only how the operands are evaluated and the type of the expression itself is changed.

 代码如下 复制代码
$foo = 10; // $foo 为整型
$bar = (boolean) $foo; // $bar 为布尔型
?>
Type casting

Type casting in PHP is very much like in C: the variable to be converted is preceded by the target type enclosed in parentheses:

The code is as follows Copy code
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is Boolean
?>

The allowed casts are:
(int) or (integer) - Convert to integer type

(bool) or (boolean) - Convert to boolean

(float) or (double) or (real) - Convert to floating point type
 代码如下 复制代码

$foo = 10; // $foo 为整型
$str = "$foo"; // $str 为字符串
?>

(string) - Convert to string

(array) - Convert to array
(object) - Convert to object

In addition, to restore a variable to a string, you can also place the variable in double quotes:
 代码如下 复制代码

$str=www.bKjia.c0m;
$int = intval($str);
这样$int=0;了哦。

Convert numbers to characters
The code is as follows Copy code
$foo = 10; // $foo is an integer <🎜> $str = "$foo"; // $str is a string<🎜> ?>
There is a super simple method for converting our commonly used strings into integers
The code is as follows Copy code
$str=www.bKjia.c0m; $int = intval($str); This way $int=0;.

When a string is evaluated as a number, the following rules are used to determine the type and value of the result:

If it contains any of the characters ".", "e" or "E", the string is evaluated as a float, otherwise it is treated as an integer
The value is determined by the first part of the string. If the string begins with legal numeric data, that number is used as its value, otherwise its value is 0 (zero). Legal numeric data begins with an optional sign, followed by one or more digits (optionally including a decimal fraction), followed by an optional exponent. The exponent is an "e" or "E" followed by one or more digits
Example:

$foo = 1 + "10.5"; // $foo is a floating point type: 11.5
The code is as follows
 代码如下 复制代码

$foo = 1 + "10.5"; // $foo为浮点型:11.5
$foo = 1 + "-1.3e3"; // $foo 为浮点型:-1299
$foo = 1 + "bob-1.3e3"; // $foo 为整型:1
$foo = 1 + "bob3"; // $foo为整型:1
$foo = 1 + "10 Small Pigs"; // $foo为整型:11
$foo = "10.0 pigs " + 1; // $foo 为浮点型:11

?>

Copy code

$foo = 1 + "-1.3e3"; // $foo is a floating point type: -1299

$foo = 1 + "bob-1.3e3"; // $foo is an integer: 1

$foo = 1 + "bob3"; // $foo is an integer: 1

$foo = 1 + "10 Small Pigs"; // $foo is an integer: 11 ?> Since PHP does not need to define variables when using data, we can flexibly define variables and flexibly convert data types.
http://www.bkjia.com/PHPjc/629165.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629165.htmlTechArticlePHP is a loosely typed language. There is no need to specifically define the variables used, which facilitates program writing. Brings great flexibility and convenience. But in the process of programming, we need to know...
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