本文目錄一覽:
php強制轉換類型
PHP 中的類型強制轉換和 C 中的非常像:
在要轉換的變量之前加上用括號括起來的目標類型。
允許的強制轉換有:
(int),(integer) – 轉換成整型
(bool),(boolean) – 轉換成布爾型
(float),(double),(real) – 轉換成浮點型
(string) – 轉換成字符串
(array) – 轉換成數組
(object) – 轉換成對象
注意在括號內允許有空格和製表符。
還可以用settype ( mixed var, string type )進行強制轉換。
PHP 字符1串轉換成int類型變成了0?
string轉換為int示例:
?php
//字符串
$str = ‘1 ‘;
$str = trim($str,” “);
$str = (int)$str;
var_dump($str);
?
“php”字符串如何轉換成數字?
1.強制類型轉換方式 \x0d\x0a$foo = “1”; // $foo 是字符串類型 \x0d\x0a$bar = (int)$foo; // $bar 是整型 \x0d\x0a\x0d\x0a2.內置函數方式 \x0d\x0a$foo = “1”; // $foo 是字符串類型 \x0d\x0a$bar = intval($foo); // $bar 是整型 \x0d\x0a\x0d\x0a3.格式化字符串方式 \x0d\x0a$foo = “1”; // $foo 是字符串類型 \x0d\x0a$bar = sprintf(“%d”, $foo); // $bar 是字符串類型
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/157791.html