user1046246
user1046246

Reputation: 35

PHP ELSE IF statement

unable to get my else if statement to work, does anyone have any ideas? it works without the else if....

$waveFeet = round("$ar2");
        if ($waveFeet >= 2) {
            echo $waveFeet - 1;
        }
        else if ($waveFeet > 5) {
            echo $waveFeet - 2;
        }

        else 
        {
            echo "$wavefeet";   
        }

also as a side question, can anyone tell me how to change my round() to make it always round (down) instead of rounding up or down...?

Upvotes: 0

Views: 1053

Answers (5)

Throoze
Throoze

Reputation: 4038

PHP buil-in floor() and ceil() functions round a number down and up respectively. I recommend posting the error you get so we can help you faster =)

Try this:

$waveFeet = floor($ar2);
if ($waveFeet >= 2 && $waveFeet <= 5 ) {
    echo $waveFeet - 1;
} else if ($waveFeet > 5) {
    echo $waveFeet - 2;
} else {
    echo $wavefeet;   
}

Note the change in the first condition (added && $waveFeet <= 5)

I think the problem might be that the ranges you use in your first and second conditions are overlapped, and it is very likely that in the case, let's say, $waveFeet == 6 PHP evaluates your first condition (originally $waveFeet >= 2), and it happened to be true, so PHP does not test the else if statement... Whenever it's possible to use disjunct conditions, I recommend you to do it...

Upvotes: 0

outis
outis

Reputation: 77440

Try the statement with a particular value, say $waveFeet = 10;, then step through the code. The first condition succeeds, so the later branches are never checked. In fact, the only time the first branch isn't entered is when $waveFeet < 2, in which case the last branch body will be executed. Thus the middle branch is never executed. The more exclusive case should come first:

if (5 < $waveFeet) {
    ...
} elseif (2 <= $waveFeet) {
    ...
} else {
    # $waveFeet < 2
    ...
}

To be completely safe, you can specify both boundary conditions:

    ...
} elseif (2 <= $waveFeet && $waveFeet <= 5) {
    ...

The inefficiency due to redundancy is minimal and the code is clearer. As you get more experienced, you can leave off this sort of thing.

If you wish to round even negative numbers down, use floor. If you wish to round towards zero (i.e. truncate), cast to an int:

$waveFeet = (int) $ar2;

Upvotes: 1

Vamsi Krishna B
Vamsi Krishna B

Reputation: 11490

Theres nothing wrong in your code ( except that you can pass argument to function without quotes ). The way you are checking it is wrong.

it wont go to else if condition because the condition will be satisfied in the first check itself .

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86416

Using the third argument of round you can round it down

echo $waveFeet = round($ar2, 2,  PHP_ROUND_HALF_DOWN);

and for your if your else if condition will never got true as if the $waveFeet is greater than or equal to 2, the first condition will be true hence your elseif condition will never be true.

You should be changing it to

    if ($waveFeet > 5) {
        echo $waveFeet - 1;
    }
    else if ($waveFeet >= 2) {
        echo $waveFeet - 2;
    }    
    else 
    {
        echo $wavefeet;   
    }

Upvotes: 1

Sonal Khunt
Sonal Khunt

Reputation: 1894

you can use floor for down round

$waveFeet = floor($ar2);                

if ($waveFeet > 5)
    echo $waveFeet - 2;
else if ($waveFeet >= 2) 
    echo $waveFeet - 1;
else 
    echo $wavefeet;   

you have to first check for 5 bcz 5 is big no than 2 and your first condition >= 2 also satisfied if no >5 so control go to first condition rather than second....

Upvotes: 0

Related Questions