简体   繁体   中英

Won't go to else/elseif statement

Php newbie here, troubleshooting a native php shopping cart. For some reason this code doesn't go to the elseif $item['type'] >= 7. tried using just else but still no go. Here's the bit with the conditional statements. Thanks in advance guys. Also any advice you can give how to make this code neat is very welcome as well, a guy 10+ years ago wrote it.

I've echoed the value of $item['type'] as well and it indeed goes to values greater than 7 just in case you guys think that's the problem.

<?php if($item['type']<7)
        { 
        //combination
            $countChilli = 0;
            $surcharge = 0.0;
            echo 'Combination (';
            echo $comboTypes[$item['type']][$item['rice']]['name'];
            echo ')';
            ?> </div>
            <div class="comboItems not">

            <?php foreach($item['items'] as $ingredient)
            {
                echo $ingredient['itemName'].'<br />';
                if($ingredient['id']==133){ $countChilli++; }
            }
            if($countChilli==1)
            {
                if(count($item['items'])==1)
                {
                    $surcharge=300;//CHANGEED FROM 2.00
                }
                elseif(count($item['items'])==2)
                {
                    $surcharge=150;
                }
                else
                {
                    $surcharge=100;
                }
            }
            elseif($countChilli>1)
            {
                $surcharge=200;
            }
        }
        elseif($item['type']>=7)
        { //sauce only

            echo 'testing';
            echo $comboTypes[$item['type']][$item['rice']]['name'];
            ?> </div>
            <div class="comboItems sauce">
            <?php foreach($item['items'] as $ingredient)
            {
                echo $ingredient['itemName'].'<br />';
            }
        }

Change

elseif($item['type']>=7)

To

else

You may also want to change

if($item['type']<7)

To

if(intval($item['type'])<7)

Cleaner code might actually solve a lot of problems. Also see PHP comparison operators

<?php
if($item['type'] < 7)
{ 
    //combination
    $countChilli = 0;
    $surcharge = 0.0;
    echo 'Combination (';
    echo $comboTypes[$item['type']][$item['rice']]['name'];
    echo ')';
    echo '</div>
    <div class="comboItems not">';

    foreach($item['items'] as $ingredient)
    {
        echo $ingredient['itemName'].'<br />';
        if($ingredient['id'] === 133){ $countChilli++; }
    }

    if($countChilli === 1)
    {
        if(count($item['items']) === 1)
        {
            $surcharge = 300;//CHANGEED FROM 2.00
        }
        elseif(count($item['items']) === 2)
        {
            $surcharge = 150;
        }
        else
        {
            $surcharge = 100;
        }
    }
    elseif($countChilli > 1)
    {
        $surcharge = 200;
    }
}
else
{
    //sauce only
    echo 'testing';
    echo $comboTypes[$item['type']][$item['rice']]['name'];
    echo '</div>
    <div class="comboItems sauce">';

    foreach($item['items'] as $ingredient)
    {
        echo $ingredient['itemName'].'<br />';
    }
}
?>

You can just cast there:

if((int)$item['type']<7)

It is faster in most cases. Plus you do not need to make a function call for such a simple conversion where it is not needed to specify a base.

Another option is the "+" operator:

if(+$item['type']<7)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM