Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot get 'IF/ELSE' condition to work correctly? 1

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I have the following snippet of code. I am absolutely sure the the variable, $type, contains the string "401k". I know this because I echo it out right before this snippet of code. However, when the $rate is displayed when it is looping through the records, it makes all $rate records into '$' amounts including if the $type is "401k". That tells me that even though $type is "401k", it is failing on the first check and going into the 'else' condition.

Any suggestions would be great or if more info is needed, I can provide.


<?if ($type != "401k"){
$rate = odbc_result($s_result_deduction, 3);
$c_rate = '$' . number_format($rate, 2, '.', ',');
echo '<td width="5%" align="right"><font size="2" face="Arial, Helvetica, sans-serif">';
echo "$c_rate";
}
else {
$rate = odbc_result($s_result_deduction, 3);
$c_rate = number_format($rate, 2, '.', '') . '%';
echo '<td width="5%" align="right"><font size="2" face="Arial, Helvetica, sans-serif">';
echo "$c_rate";
}
echo "</font></td>";?>
 
My bad... I have changed up the order of the if/else....

What is happening is that $type is always meeting the first condition and never getting to the 'else' condition even if $type = "401k".

Sorry for the confusion.
 
two things...

1. echo out the value of $type to visually confirm what it is...

2. lcase (or ucase ) the variable to compare them


Code:
echo "<b>$type;</b><br>";
<?if (strtolower($type) != "401k"){

Bastien

I wish my computer would do what I want it to do,
instead of what I tell it to do...
 
Let's try to simplify your code:
Code:
<?
   $rate = odbc_result($s_result_deduction, 3);
   echo '$type: ' . $type . "<br>\n";
   switch ($type) {
       case '401k':
          echo 'type is 401k'."<br>\n";
          $c_rate = number_format($rate, 2, '.', '') . '%';
          break;
       default:
          echo 'type is not 401k'."<br>\n";
          $c_rate = '$' . number_format($rate, 2, '.', ',');
   }
   echo '<td style="width:5%;text-align:right;font-size=80%;font-family:Arial, Helvetica, sans-serif">';
   echo $c_rate."</td>/n";

I replaced your 'if-else' with a switch statement. I find them easier to debug, especially if you have a lot of different options. Since the only thing that changes is the value of $c_rate, I put everything else under the switch statement.

I also put some debugging 'echo' statements in.

Ken
 
Also:
Echo out the length of the variable. It might contain some invisible chars, such as \n etc.
 
Bastien-
No love with that attempt...

kenrbnsn-
Crazy! I pasted in your exact snippet and it returns:

$type: 401k
type is not 401k

DRJ478-
Absolutey correct... $t_type = trim($type) then using $t_type for comparison works.

Thanks to all for the help!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top