rcleon
asked on
PHP calculation
Hi here is my code, very simple
if ($Type1 == "ISO" || $Type1 == "NQ") { $Total_SO1 = $Shares1 * $Price1; } else { $Total_SO1 = 0; }
if ($Type2 == "ISO" || $Type2 == "NQ") { $Total_SO2 = $Shares2 * $Price2; } else { $Total_SO2 = 0; }
if ($Type3 == "ISO" || $Type3 == "NQ") { $Total_SO3 = $Shares3 * $Price3; } else { $Total_SO3 = 0; }
if ($Type4 == "ISO" || $Type4 == "NQ") { $Total_SO4 = $Shares4 * $Price4; } else { $Total_SO4 = 0; }
if ($Type5 == "ISO" || $Type5 == "NQ") { $Total_SO5 = $Shares5 * $Price5; } else { $Total_SO5 = 0; }
if ($Type6 == "ISO" || $Type6 == "NQ") { $Total_SO6 = $Shares6 * $Price6; } else { $Total_SO6 = 0; }
if ($Type7 == "ISO" || $Type7 == "NQ") { $Total_SO7 = $Shares7 * $Price7; } else { $Total_SO7 = 0; }
$Total_SO = $Total_SO1 + $Total_SO2 + $Total_SO3 + $Total_SO4 + $Total_SO5 + $Total_SO6 + $Total_SO7;
But it dose not work, can anyone see why. I have echo each of the variables and they all have the correct value except the total of each line os zero. out side of the if statement they work properly, please help.
Thanks
Rafael
if ($Type1 == "ISO" || $Type1 == "NQ") { $Total_SO1 = $Shares1 * $Price1; } else { $Total_SO1 = 0; }
if ($Type2 == "ISO" || $Type2 == "NQ") { $Total_SO2 = $Shares2 * $Price2; } else { $Total_SO2 = 0; }
if ($Type3 == "ISO" || $Type3 == "NQ") { $Total_SO3 = $Shares3 * $Price3; } else { $Total_SO3 = 0; }
if ($Type4 == "ISO" || $Type4 == "NQ") { $Total_SO4 = $Shares4 * $Price4; } else { $Total_SO4 = 0; }
if ($Type5 == "ISO" || $Type5 == "NQ") { $Total_SO5 = $Shares5 * $Price5; } else { $Total_SO5 = 0; }
if ($Type6 == "ISO" || $Type6 == "NQ") { $Total_SO6 = $Shares6 * $Price6; } else { $Total_SO6 = 0; }
if ($Type7 == "ISO" || $Type7 == "NQ") { $Total_SO7 = $Shares7 * $Price7; } else { $Total_SO7 = 0; }
$Total_SO = $Total_SO1 + $Total_SO2 + $Total_SO3 + $Total_SO4 + $Total_SO5 + $Total_SO6 + $Total_SO7;
But it dose not work, can anyone see why. I have echo each of the variables and they all have the correct value except the total of each line os zero. out side of the if statement they work properly, please help.
Thanks
Rafael
There is nothing obviously wrong with your code. The fragment supplied above works OK for me when supplied with the appropriate values.
Turn on error checking with
ini_set('display_errors',1 ); error_reporting(E_ALL);
at the start of your script. Also remember that PHP is case senstive with variable names so $Price and $price are not the same.
Turn on error checking with
ini_set('display_errors',1
at the start of your script. Also remember that PHP is case senstive with variable names so $Price and $price are not the same.
@AriMc - it might be simpler to use trim
http://www.php.net/trim
$Type1 = $trim( $Type1 );
and so on...
http://www.php.net/trim
$Type1 = $trim( $Type1 );
and so on...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
bportlock: True, that would've been my next suggestion if the first debugging showed the values include spaces. :)
@AriMc - never hurts to do it anyway, better safe than sorry.
Really this piece of code should have been written with arrays then used array_sum() to do all the dirty work, but we are where we are and I suspect that there is a lot of other code that would need alteration.
Really this piece of code should have been written with arrays then used array_sum() to do all the dirty work, but we are where we are and I suspect that there is a lot of other code that would need alteration.
ASKER
Thank you ALL it works great.
Try debugging the values like this:
echo "<".$Type1.">";
... to see any spaces.