簡體   English   中英

PHP如果語句沒有顯示正確的結果

[英]PHP If statements not showing correct results

我有這些PHP if語句,當我回顯$percentage它顯示100但它的回聲

echo 'Over Allowance';
echo '<br>INSERT into customer_billing';

並不是

echo 'Reached Allowance';

if($percentage >= '80' and $percentage <= '100') {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Approaching Allowance';
            echo 'check customer_billing and remove';
        }
        //if its 100%
        elseif($percentage == '100') {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Reached Allowance';
            echo 'check customer_billing and remove';
        }
        //if its more than 100%
        elseif($percentage > '100') {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Over Allowance';
            echo '<br>INSERT into customer_billing';
        }
        //or if the limit is less than the percentage
        //this does nothing at all
        elseif($percentage < $result["soundfiles_max"]) {
            //do nothing
            echo 'check customer_billing and remove';
        }

我究竟做錯了什么?

你可以刪除第一個相同的法則,

if($percentage >= '80' && $percentage < '100') {

因為你使用的是> =運算符,你應該使用整數而不是文本,並刪除那些數字上的'',所以從'80'到80只

if($percentage >= 80 && $percentage <= 100) {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Approaching Allowance';
            echo 'check customer_billing and remove';
        }
        //if its 100%
        elseif($percentage == 100) {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Reached Allowance';
            echo 'check customer_billing and remove';
        }
        //if its more than 100%
        elseif($percentage > 100) {
            echo '<strong>'.$customer["company"].'</strong><br>';
            echo 'Over Allowance';
            echo '<br>INSERT into customer_billing';
        }
        //or if the limit is less than the percentage
        //this does nothing at all
        elseif($percentage < $result["soundfiles_max"]) {
            //do nothing
            echo 'check customer_billing and remove';
        }

嘗試刪除數字值周圍的引號,例如:

if($percentage >= '80' and $percentage <= '100') {

變為:

if($percentage >= 80 and $percentage <= 100) {

省略百分比值的引號,因為它們是整數。 喜歡

if($percentage >= 80 and $percentage <= 100) 

如果$ percentage值為100,則不會轉到elseif語句,因為您在1st語句中檢查的值大於等於100。

參考浮點數

永遠不要將浮點數結果信任到最后一位,並且不要直接比較浮點數是否相等。 如果需要更高的精度,可以使用任意精度數學函數和gmp函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM