<?php $a = 5; // Integer $b = 5.34; // Float $c = "25 kilometers"; // String $d = "kilometers 25"; // String $e = "hello"; // String $f = true; // Boolean $g = NULL; // NULL $a = (int) $a; $b = (int) $b; $c = (int) $c; $d = (int) $d; $e = (int) $e; $f = (int) $f; $g = (int) $g; //To verify the type of any object in PHP, use the var_dump() function: var_dump($a); var_dump($b); var_dump($c); var_dump($d); var_dump($e); var_dump($f); var_dump($g); ?>
Note that when casting a string that starts with a number, the (int) function uses that number. If it does not start with a number, the (int) function convert strings into the number 0.
int(5) int(5) int(25) int(0) int(0) int(1) int(0)
请注意,当强制转换一个以数字开头的字符串时,(int) 函数会使用该数字。如果字符串不以数字开头,(int) 函数会将字符串转换为数字 0。