If Else Statement Wrong

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TKB
    New Member
    • Nov 2006
    • 8

    #1

    If Else Statement Wrong

    Could anyone tell me what's wrong with this code?

    Thank you in advance for your effort.

    <?php
    If ($CategoryName == 'U-100') {
    ($UnitPrice = 500);
    } else if ($CategoryName == 'U-130') {
    ($UnitPrice = 400);
    } else if ($CategoryName == 'U-105') {
    ($UnitPrice = 350);
    } else if ($CategoryName == 'U-95') {
    ($UnitPrice = 200);
    }else if($CategoryNam e == 'U-85') {
    ($UnitPrice = 445);
    $Total = $ProductUnits*U nitPrice
    echo $Total
    ?>
  • TheMadMidget
    New Member
    • Oct 2006
    • 98

    #2
    You probably need

    ($UnitPrice = 445); }

    I added the }
    Also in this type of situation you might be interested in using a switch/case structure. It's easier to look at (cleaner) and therefore makes it easier to add to in the future, especially by a different person if that is to occur.

    Note* the ElseIf should work perfectly fine, this is just a consideration

    Comment

    • TheMadMidget
      New Member
      • Oct 2006
      • 98

      #3
      Oh, I forgot something big.
      If Else in php is used by elseif , note that it is one word, you used two.

      Comment

      • steven
        New Member
        • Sep 2006
        • 143

        #4
        Originally posted by TheMadMidget
        Oh, I forgot something big.
        If Else in php is used by elseif , note that it is one word, you used two.
        http://us2.php.net/manual/en/control...res.elseif.php
        Actually both work. There is little difference between the two, however, elseif (the single word) is slightly faster.

        The problem is that you're missing the ending curly parenthesis of your if / else statement. I assume it should go after ($UnitPrice = 445);

        Also, it is unnecessary to place the variable assignments within brackets.

        Comment

        • seangates
          New Member
          • Dec 2006
          • 19

          #5
          Originally posted by steven
          Actually both work. There is little difference between the two, however, elseif (the single word) is slightly faster.

          The problem is that you're missing the ending curly parenthesis of your if / else statement. I assume it should go after ($UnitPrice = 445);

          Also, it is unnecessary to place the variable assignments within brackets.
          To sum up here is the fixed code:
          Code:
          <?php
          If ($CategoryName == 'U-100') {
          $UnitPrice = 500;
          } elseif ($CategoryName == 'U-130') {
          $UnitPrice = 400; 
          } elseif ($CategoryName == 'U-105') {
          $UnitPrice = 350; 
          } elseif ($CategoryName == 'U-95') {
          $UnitPrice = 200; 
          } elseif($CategoryName == 'U-85') {
          $UnitPrice = 445;
          }
          $Total = $ProductUnits*UnitPrice;
          echo $Total;
          ?>
          Cut and paste as needed.
          The alternative, using a case statement would be:
          Code:
          <?php
          switch($CategoryName){
          
          case 'U-100':
          $UnitPrice = 500;
          break;
          
          case 'U-130':
          $UnitPrice = 400;
          break;
          
          case 'U-105':
          $UnitPrice = 350; 
          break;
          
          case 'U-95':
          $UnitPrice = 200; 
          break;
          
          case 'U-85':
          $UnitPrice = 445;
          break;
          
          }
          
          $Total = $ProductUnits*UnitPrice;
          echo $Total;
          ?>
          Hope that helps,
          Sean

          Comment

          Working...