Solved

PHP calculation

Posted on 2011-03-08
7
252 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:rcleon
7 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 35074284
Are you checking the values of $TypeX correctly with your echo? There might be some additional spaces in the values that screw your tests, especially if you read them from a database.

Try debugging the values like this:

   echo "<".$Type1.">";

... to see any spaces.





0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35074323
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.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35074352
@AriMc - it might be simpler to use trim

http://www.php.net/trim

$Type1 = $trim( $Type1 );

and so on...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 4

Accepted Solution

by:
gizmola earned 500 total points
ID: 35074382
My best guess is that your $Type.. variables don't match either of the 2 strings.you are testing for. PHP variable names are case sensitive so double check that.

Also, if you really want to have the if-then-else on one line like that you can replace:

if ($Type1 == "ISO" || $Type1 == "NQ") { $Total_SO1 = $Shares1 * $Price1; } else { $Total_SO1 = 0; }

Open in new window


with the ternary operator

$Total_SO1 = ($Type1 == "ISO" || $Type1 == "NQ") ? $Shares1 * $Price1 : 0;

Open in new window


Yes that is equivalent ;)
0
 
LVL 9

Expert Comment

by:AriMc
ID: 35074402
bportlock: True, that would've been my next suggestion if the first debugging showed the values include spaces. :)

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35074450
@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.

0
 

Author Closing Comment

by:rcleon
ID: 35075808
Thank you ALL it works great.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now