SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Else statement did not work

    Hi

    I encountered a problem in my if,elseif and else statement.

    I have this code:
    Code:
    $HDMF = $_POST['HDMF'];
    
    if($TotEarn <= 1500){
        $HMDF = round(($TotEarn * 0.01), 2);
    }
    elseif ($TotEarn >= 1501) {
        $HDMF = round(($TotEarn * 0.02), 2);
    }
    else {
        $HDMF = (0);
    }
    $smarty->assign('HDMF', $HDMF);
    I have $TotEarn = 893.5 so that my $HDMF will be 0 right?

    But it become blank or empty.

    What's wrong in my condition.

    Thank you

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    I have $TotEarn = 893.5
    Are you sure? Did you echo $TotEarn?
    Quote Originally Posted by newphpcoder View Post
    so that my $HDMF will be 0 right?
    Why?
    If $TotEarn = 893.5 , what part of your if-elseif-else should be executed? Put some echoes in there to watch the flow.
    Quote Originally Posted by newphpcoder View Post
    But it become blank or empty.
    What becomes blank? You don't see anything on the screen? Maybe you're telling Smarty to do zero surpression (a zero values is displayed as blank) ? I don't know Smarty, so I'm just guessing here.
    Do an echo of $HDMF after the if-elseif-else to check its value.

  3. #3
    Non-Member
    Join Date
    Jun 2009
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if $TotEarn = 893.5 it means that it less than 1500, so first condition should work.
    and if $TotEarn defined in the code somehow?

  4. #4
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried to var_dump the $TotEarn and $HDMF and the result is $TotEarn = 893.5 and $HDMF = Null

    I want to execute is the else condition because it is <= 1500 and also it is not >= 1500 right?

    So it should be zero...

  5. #5
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this code:

    Code:
    $HDMF = $_POST['HDMF'];
    
    if($TotEarn >= 100 && $TotEarn <= 1500){
        $HMDF = round(($TotEarn * 0.01), 2);
    }
    elseif ($TotEarn >= 1501) {
        $HDMF = round(($TotEarn * 0.02), 2);
    }
    else {
        $HDMF = (0);
    }
    if I used this code the 893.5 will be used the if condition right????
    but now the output is an empty data

    Thank you

    The computation is like this:

    if $TotEarn is 1500 and below the share is 1% of $TotEarn
    and if $TotEarn is above 1500 the share is 2% of $TotEarn


    Whats wrong in my code why does the below 1500 did not get the 1% of the $TotEarn but the output is empty data
    Thank you

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Maybe because you keep spelling $HDMF differently?

    If this is the case, then the lesson is to name your variables with a bit more thought.

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    The computation is like this:

    if $TotEarn is 1500 and below the share is 1% of $TotEarn
    and if $TotEarn is above 1500 the share is 2% of $TotEarn
    If this is the computation, then why don't you code it like that?
    PHP Code:
    if ($TotEarn <= 1500) {
        
    $HDMF round(($TotEarn 0.01), 2);
    } else {
        
    $HDMF round(($TotEarn 0.02), 2);

    And like Cups says, pay attention to "little" details like variable names.

  8. #8
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, Thank you...
    I change my code from this:
    Code:
    $HDMF = $_POST['HDMF'];
    
    if($TotEarn <= 1500){
        $HDMF = round(($TotEarn * 0.01), 2);
    }
    elseif ($TotEarn > 1500) {
        $HDMF = round(($TotEarn * 0.02), 2);
    }
    else {
        $HDMF = (0);
    }
    is it right for if $TotEarn = 1500 and below

    and $TotEarn = above 1500

    Thank you

  9. #9
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay i try your code, it also work thank you

    Thank you

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Yeah, didn't mean to sound so preachy there, just saying its fine to make errors like that as long as you address the underlying cause -- and sometimes there is a bonus to naming variables longhand -- yes, they seem to take up more space but reading

    $highDensityMoneyFactor = 0;

    might be more:

    readable
    meaningful to someone else
    meaningful to you in three months time

    and if you have a good IDE (or you train your text editor, like Vim) it will code complete for you.

    Then again you might simplify it to $bonus.

    Just sayin'

    EDIT

    If you find that way of thinking to be in any way at all cool or helpful then you might like to get hold of a copy of "Clean Code" by Uncle Bob Martin as your xmas treat (nods to Anthony Sterling ).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •