Php 简明教程
PHP - Type Casting
术语 "类型转换" 指的是将一种类型的数据转换为另一种类型。由于 PHP 是弱类型语言,解析器在执行某些操作时会将某些数据类型强制转换为其他类型。例如,如果一个带有数字的字符串是加法运算中涉及的运算符之一,则它将转换为整数。
The term "Type Casting" refers to conversion of one type of data to another. Since PHP is a weakly typed language, the parser coerces certain data types into others while performing certain operations. For example, a string having digits is converted to integer if it is one of the operands involved in the addition operation.
Implicit Type Casting
下面是一个强制或隐式类型转换的示例:
Here is an example of coercive or implicit type casting −
<?php
$a = 10;
$b = '20';
$c = $a+$b;
echo "c = " . $c;
?>
在这种情况下,$b 是一个字符串变量,被转换为整数以启用加法。它将生成以下 output −
In this case, $b is a string variable, cast into an integer to enable addition. It will produce the following output −
c = 30
我们再来看另一个示例。这里,整数变量 $a 被转换为一个字符串,以便与一个字符串变量连接。
Let’s take another example. Here, an integer variable $a is converted to a string so that it is concatenated with a string variable.
<?php
$a = 10;
$b = '20';
$c = $a.$b;
echo "c = " . $c;
?>
它将生成以下 output −
It will produce the following output −
c = 1020
除了此类强制类型转换之外,还有其他方法可将一种类型的数据显式转换为另一种类型。为此,您可以使用 PHP 的类型转换运算符或类型转换函数。
In addition to such coercive type conversion, there are other ways to explicitly cast one type of data to other. You can use PHP’s type casting operators or type casting functions for this purpose.
Type Casting Operators
若要将一个类型的表达式转换为另一个类型,您需要在表达式前面的括号中放入后一个类型的数据类型。
To convert an expression of one type to another, you need to put the data type of the latter in parenthesis before the expression.
$var = (type)expr;
PHP 中的某些类型转换运算符为 −
Some of the type casting operators in PHP are −
-
(int) or (integer) casts to an integer
-
(bool) or (boolean) casts to a boolean
-
(float) or (double) or (real) casts to a float
-
(string) casts to a string
-
(array) casts to an array
-
(object) casts to an object
Casting to Integer
您可以轻松地将浮点值转换为整数。看看以下 example −
You can easily convert a float value to an integer. Take a look at the following example −
<?php
$a = 9.99;
$b = (int)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
int(9)
请注意,浮点值不会四舍五入到最接近的整数;它只会返回整数部分。
Note that the float value is not rounded to the nearest integer; instead it just returns the integer part.
String to Integer Conversion
(int) 运算符还会将字符串转换为整数。如果字符串仅由数字组成,则转换非常简单。
The (int) operator also coverts a string to integer. The conversion is straightforward if the string consists of just digits.
<?php
$a = "99";
$b = (int)$a;
var_dump($b);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
int(99)
即使字符串包含浮点数,(int) 运算符也会仅返回整数部分。
Even if the string contains a floating point number, the (int) operator returns just the integer part.
现在我们再举一个例子来理解一个特殊情况。 If the string is alphanumeric, casting with (int) works differently 。
Now let’s take another example to understand a special case. If the string is alphanumeric, casting with (int) works differently.
-
If the string starts with digits followed by non-numeric characters, only the initial digits are considered.
-
If the string starts with non-numeric characters and the digits are in the middle, the csting operator returns "0".
看看以下 example −
Take a look at the following example −
<?php
$a = "10 Rs.";
$b = (int)$a;
var_dump($b);
$a = "$100";
$b = (int)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
int(10)
int(0)
Casting to Float Type
您可以使用 (float) 或 (double) 强转运算符将变量或表达式显式转换为浮点数。
You can use either the (float) or (double) casting operator to explicitly convert a variable or expression to a float.
<?php
$a = 100;
$b = (double)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
float(100)
包含任何有效数字表示的字符串都可以使用强转运算符转换为浮点类型。
A string containing any valid numeric representation may be cast to a float type by using a casting operator.
<?php
$a = "100";
$b = (double)$a;
var_dump($b);
$a = "9.99";
$b = (float)$a;
var_dump($b);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
float(100)
float(9.99)
即使字符串嵌入了浮点的科学计数法,该字符串也会转换为浮点。看看下面的例子 −
The string gets converted to float even when it embeds a scientific notation of float. Take a look at the following example −
<?php
$a = "1.23E01";
$b = (double)$a;
var_dump($b);
$a = "5.5E-5";
$b = (float)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
float(12.3)
float(5.5E-5)
所有浮点数后边的非数字字符均被忽略。类似的,如果字符串以任何一个非数字字符开头,则会转换为“0”。见以下的 example −
All the non-numeric characters after the floating point numbers are ignored. Similarly, the string converts to "0" if it starts with any non-numeric character. See the following example −
<?php
$a = "295.95 only";
$b = (double)$a;
var_dump($b);
$a = "$2.50";
$b = (float)$a;
var_dump($b);
?>
它将生成以下 output −
It will produce the following output −
float(295.95)
float(0)
Casting to String Type
通过使用一个强制类型转换操作符,任何求值为一个浮点数或整数的表达式都可以强制转换为一个字符串类型。下面给出几个示例−
By using a casting operator, any expression evaluating to a floating-point or integer may be cast to a string type. Few examples are given below −
<?php
$a = 100;
$b = (string)$a;
var_dump($b);
$x = 55.50;
$y = (string)$x;
var_dump($y);
?>
您将获得以下内容 output −
You will get the following output −
string(3) "100"
string(4) "55.5"
Casting to Bool Type
任何非零数字(可以是整数或浮点数)使用 (bool) 操作符强制转换为 true。求值为“0”的表达式返回 false。字符串始终强制转换为 true。
Any non-zero number, either integer or float, is cast to true with (bool) operator. An expression evaluating to "0" returns false. A string is always cast to true.
看看以下 example −
Take a look at the following example −
<?php
$a = 100;
$b = (bool)$a;
$x = 0;
$y = (bool)$x;
$m = "Hello";
$n = (bool)$m;
var_dump($b);
var_dump($y);
var_dump($n);
?>
它将生成以下 output −
It will produce the following output −
bool(true)
bool(false)
bool(true)
Type Casting Functions
PHP 包含用于执行类型强制转换的以下内置函数−
PHP includes the following built-in functions for performing type casting −
-
intval()
-
floatval()
-
strval()
让我们详细讨论这些内置函数。
Let’s discuss these built-in functions in detail.
The intval() Function
这个函数获取一个变量的整数值。
This function gets the integer value of a variable.
intval(mixed $value, int $base = 10): int
$base 参数默认为 10,这意味着该值转换为十进制数。
The $base parameter is 10 by default, which means the value is converted to decimal number.
-
If the value is a float, the intval() function returns an integer, discarding the fractional part.
-
A string representation of a number returns a corresponding integer, discarding the fractional part, if any.
-
If the value is a string with a valid octal number and the base is 8, the intval() function returns a corresponding octal number.
When the base is "0" ,值的转换基于字符前缀。
When the base is "0", the conversion of value takes place on the basis of character prefix.
-
If the value starts with 0X or 0x, a hexadecimal number is returned.
-
If the value starts with 0B or 0b, a binary number is returned
-
If the value starts with 0, the function returns an octal number.
intval() 函数对于 true,返回 1,对于 false 布尔值,返回 0。
The intval() function returns 1 for true, 0 for false Boolean values.
Example
下面的示例展示了 intval() 函数的工作方式−
The following example shows how the intval() function works −
<?php
echo intval(42). PHP_EOL;
echo intval(4.2). PHP_EOL;
echo intval('42') . PHP_EOL;
echo intval(042) . PHP_EOL; # 0ctal number
echo intval('042', 0) . PHP_EOL; # 0ctal number
echo intval('42', 8) . PHP_EOL; # octal
echo intval(0x1A) . PHP_EOL; # Hexadecimal
echo intval('0x1A', 16) . PHP_EOL; # Hexadecimal
echo intval('0x1A', 0) . PHP_EOL; # Hexadecimal
echo intval(false) . PHP_EOL;
echo intval(true) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
42
4
42
34
34
34
26
26
26
0
1
The floatval() Function
floatval() 函数获取一个表达式的浮点值。
The floatval() function gets the float value of an expression.
floatval(mixed $value): float
该值可能是一个任意的标量变量。带有非数字字符的字符串返回“0”。带有数字表示或带有一个数字表示起始子串的字符串返回对应的数字。下面的 example 展示了 floatval() 函数的工作方式 −
The value may be any scalar variable. String with non-numeric characters returns "0". A string with a numeric representation or with the starting substring with a numeric representation returns the corresponding number. The following example shows how the floatval() function works −
<?php
echo floatval(42). PHP_EOL;
echo floatval(4.2). PHP_EOL;
echo floatval('42') . PHP_EOL;
echo floatval('99.90 Rs') . PHP_EOL;
echo floatval('$100.50') . PHP_EOL;
echo floatval('ABC123!@#') . PHP_EOL;
echo (true) . PHP_EOL; ;
echo (false) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
42
4.2
42
99.9
0
0
1
doubleval() 函数是 floatval() 函数的一个别名,因此返回类似的结果。
The doubleval() function is an alias of floatval() function, and hence returns similar results.
The strval() Function
strval() 函数获取变量的字符串值。此函数不对返回的值执行任何格式化。
The strval() function gets the string value of a variable. This function performs no formatting on the returned value.
strval(mixed $value): string
正在转换为字符串的值可以是任何标量类型、null 值或实现了 __toString() 方法的对象。查看以下内容:
The value that is being converted to a string may be any scalar type, null, or an object that implements the __toString() method. Take a look at the following example −
<?php
echo strval(42). PHP_EOL;
echo strval(4.2). PHP_EOL;
echo strval(4.2E5) . PHP_EOL;
echo strval(NULL) . PHP_EOL;
echo (true) . PHP_EOL;
echo (false) . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
42
4.2
420000
1
以下内容定义了一个实现了 __toString() 方法的类:
The following example defines a class that implements the _toString() method.
<?php
class myclass {
public function __toString() {
return __CLASS__;
}
}
echo strval(new myclass);
?>
在这里,你会得到以下 output −
Here, you will get the following output −
myclass