Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php类型转换的判别

php类型转换的判别

伊谢尔伦
伊谢尔伦asal
2016-11-24 09:06:311446semak imbas

PHP 在变量定义中不需要(或不支持)明确的类型定义;变量类型是根据使用该变量的上下文所决定的。也就是说,如果把一个字符串值赋给变量 $var,$var 就成了一个字符串。如果又把一个整型值赋给 $var,那它就成了一个整数。

PHP 的自动类型转换的一个例子是加法运算符“+”。如果任何一个操作数是浮点数,则所有的操作数都被当成浮点数,结果也是浮点数。否则操作数会被解释为整数,结果也是整数。注意这并没有改变这些操作数本身的类型;改变的仅是这些操作数如何被求值以及表达式本身的类型。

<?php
$foo = "0";  // $foo 是字符串 (ASCII 48)
$foo += 2;   // $foo 现在是一个整数 (2)
$foo = $foo + 1.3;  // $foo 现在是一个浮点数 (3.3)
$foo = 5 + "10 Little Piggies"; // $foo 是整数 (15)
$foo = 5 + "10 Small Pigs";     // $foo 是整数 (15)
?>

如果想要测试本节中任何例子的话,可以用 var_dump() 函数。

Note:

自动转换为 数组 的行为目前没有定义。

此外,由于 PHP 支持使用和数组下标同样的语法访问字符串下标,以下例子在所有 PHP 版本中都有效:

dca3c56123eb20b47782eac3a51e167a

类型强制转换

PHP 中的类型强制转换和 C 中的非常像:在要转换的变量之前加上用括号括起来的目标类型。

<?php
$foo = 10;   // $foo is an integer
$bar = (boolean) $foo;   // $bar is a boolean
?>

允许的强制转换有:

(int), (integer) - 转换为整形 integer

(bool), (boolean) - 转换为布尔类型 boolean

(float), (double), (real) - 转换为浮点型 float

(string) - 转换为字符串 string

(array) - 转换为数组 array

(object) - 转换为对象 object

(unset) - 转换为 NULL (PHP 5)

(binary) 转换和 b 前缀转换支持为 PHP 5.2.1 新增。

注意在括号内允许有空格和制表符,所以下面两个例子功能相同:

6e8decf56c9b6043ce6ed12c1d03d220

将字符串文字和变量转换为二进制字符串:

2aff4fc40458e3581787db5b001a7427

Note:

可以将变量放置在双引号中的方式来代替将变量转换成字符串:

<?php
$foo = 10;            // $foo 是一个整数
$str = "$foo";        // $str 是一个字符串
$fst = (string) $foo; // $fst 也是一个字符串// 输出 "they are the same"
if ($fst === $str) {
echo "they are the same";
}
?>


有时在类型之间强制转换时确切地会发生什么可能不是很明显。更多信息见如下:

转换为布尔型

转换为整型

转换为浮点型

转换为字符串

转换为数组

转换为对象

转换为资源

转换为 NULL


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:PHP 简单 遍历 重命名Artikel seterusnya:php—goto语句