Warning: Undefined variable $kind in /home4/wgate/public_html/include/checkfunpdo.php on line 662

Warning: Undefined variable $vers in /home4/wgate/public_html/include/checkfunpdo.php on line 662

Warning: Undefined variable $kind in /home4/wgate/public_html/include/checkfunpdo.php on line 662

Warning: Undefined variable $vers in /home4/wgate/public_html/include/checkfunpdo.php on line 662
:::| 目前位置圖示目前位置:首頁圖示回首頁 | 主功能頁圖示相關問答
PHP 若變數為NULL時處理

[日期]:2026/07/23  [瀏覽人數]:55

如果在要處理的變數為NULL時,要做round、number_format...等函數處理時會出現

Deprecated: number_format(): Passing null to parameter #1 ($num) of type float is deprecated in....

Deprecated: round(): Passing null to parameter #1 ($num) of type int|float is deprecated in...

在舊版PHP5不會出錯誤,但在PHP8會出現上面的錯誤訊息

如:number_format($owtal[$owgrp]["npayamt"])

可以在顯示之前先判別是否為存在的變數L,不存在時設定為0,

如:

if (isset($owtal[$owgrp]["npayamt"]))

$owtal[$owgrp]["npayamt"] = 0;

echo number_format($owtal[$owgrp]["npayamt"]);

另一種方式相容PHP5,7,8的做法

echo number_format((isset($owtal[$owgrp]["npayamt"]) ? $owtal[$owgrp]["npayamt"] : 0));

若是PHP8可以改為

echo number_format(($owtal[$owgrp]["npayamt"]) ?? 0))