2017-10-10 106 views
1

我有一些代碼拒絕運行,出現錯誤;在文本字符串上遇到的「格式不正確的數字」

[ErrorException]        
A non well formed numeric value encountered 

我讀很多關於它,但所有的解決方案似乎已經處理了人不使用number_formatdatestrtotime正確。

我的代碼可以像下面一樣簡單,同時給出錯誤;

$plan == 'Event'; 
if($plan !== 'Event') { 
    $cost_of_report = $cost_of_report * 2; 
} 

該行$cost_of_report = $cost_of_report * 2;獨立工作。

我回應了gettype($plan),它是一個字符串。

我在Laravel artisan命令中使用它,在PHP 5.6.31中使用Laravel 4.2。

有沒有人有任何想法什麼是錯的,或者我可以做些什麼來嘗試使這個工作?

+0

用於記錄:'$ plan =='Event';'不會將值賦給'$ plan'。如果是這樣,if條件不會導致'true',因此永遠不會運行'$ cost_of_report = $ cost_of_report * 2;'。也許編輯你的「簡單」片段。這個例外是否因爲'if'永遠不會被滿足,並且'$ cost_of_report'從來不會作爲算術重新計算? – mickmackusa

回答

2

我相信這裏的問題不是$plan而是$cost_of_report。確保它是編號

+0

感謝Marcin,我回應了那個變量的gettype,它是一個字符串,所以我在那裏寫了一行'$ cost_of_report = floatval($ cost_of_report);'現在它可以工作。我會將你的答案標記爲正確的,但你能解釋爲什麼'$ cost_of_report = $ cost_of_report * 2;'在if語句之外工作嗎? –

+0

它應該在'if'或其外部沒有任何區別。所以,也許你有不同的價值,或者這是PHP的錯誤,或者這是你的服務器/環境相關 - 請參閱https://github.com/getgrav/grav/issues/928 –

0

確保在計算之前您還沒有將$ cost_of_report轉換爲number_format()。

嘗試轉換爲number_format()

希望這有助於之前做一些計算。

+0

並避免$ cost_of_report =「100」;因爲這將假定100是一個字符串而不是一個整數。 –