Getting an error on a simple percentage formula

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Getting an error on a simple percentage formula

Post by rad1964 »

Hello,
My first post please don't skewer me! j/k However I am only a beginner.

I have what should be a simple percentage formula, but upon running it I get this error:
[text]Parse error: syntax error, unexpected '=' in /math.php on line 6[/text]
This is line 6: $totalTeamH / 15 = $percentageTeamH;

Here is my code:

Code: Select all

//A League Team Percentages
$percentageTeamH = '';
$percentageTeamV = '';
$totalTeamH / 15 = $percentageTeamH;
$totalTeamV / 15 = $percentageTeamV;
echo $teamH . "'s Win/Loss percentage is " . $percentageTeamH . "<br>";
echo $teamV . "'s Win/Loss percentage is " . $percentageTeamV . "<br>";
Where;
totalTeamH is the home teams total wins out of 15 games played.
totalTeamV is the visiting teams total wins out of 15 games played.
$teamH is the home teams name.
$teamV is the visiting teams name.

Not sure why I get the error, I am sure my syntax is the issue.

rad1964
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Getting an error on a simple percentage formula

Post by Christopher »

PHP, like most computer lanugages, assign from right to left. Instead of this:

Code: Select all

$percentageTeamH = '';
$percentageTeamV = '';
$totalTeamH / 15 = $percentageTeamH;
$totalTeamV / 15 = $percentageTeamV;
do this:

Code: Select all

$percentageTeamH = $totalTeamH / 15;
$percentageTeamV = $totalTeamV / 15;
(#10850)
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: Getting an error on a simple percentage formula

Post by rad1964 »

Thanks Chris, I am no longer getting an error, however my result is 0. Should I be getting 0 from 9 / 15?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Getting an error on a simple percentage formula

Post by Christopher »

You may want to var_dump() the numerator variables to see what their type and value actually is.
(#10850)
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: Getting an error on a simple percentage formula

Post by rad1964 »

Ok, my first time using var_dump()...seems like a good tool :)

input: var_dump($totalTeamH,$totalTeamV,$teamH,$teamV,$percentageTeamH,$percentageTeamV);
output: NULL NULL string(22) "Abbey\'s Wrecking Crew" string(10) "New Eagles" float(0) int(0)
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: Getting an error on a simple percentage formula

Post by rad1964 »

Here is my newbie interpretation of the var_dump:

Both NULL values, which represent $totalTeamH and $totalTeamV are not being passed their variable data. (Could it be because I am including the math.php using require_once? Because the same values, when echoed on the main process.php page display the correct values.)

string(22) "Abbey\'s Wrecking Crew" string(10) "New Eagles" represent $teamH and $teamV (Both are correct)

So, float(0) and int(0) must represent $percentageTeamH and $percentageTeamV. (although I am not sure why they have different values?)


Any clues as to how I suss this out?

Thanks,
rad1964
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: Getting an error on a simple percentage formula

Post by rad1964 »

for the float I remember I am trying out round()
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: Getting an error on a simple percentage formula

Post by rad1964 »

OK, wow, chalk most of this up to bad coding 101...
I named things religiously teamH this and TeamV that (for home and visitors)
but.. in the begininng it was teamA and teamB... so $totalTeamH is really $totalTeamA AND $totalTeamV is really $totalTeamB !!

So now seeing as I am doing sports percentage I need it to display at MAX 1.000 and MIN .001, so I need 3 decimal places after the division happens.
$percentageTeamH = round(($totalTeamA / 15),3);
is what I tried... but what I am getting is:
Abbey\'s Wrecking Crew's Win/Loss percentage is 0.6

How would I set the decimal places?

thank you for all your help!

rad1964
rad1964
Forum Newbie
Posts: 12
Joined: Sun Jan 22, 2012 10:51 pm

Re: Getting an error on a simple percentage formula

Post by rad1964 »

Woohoo.. all formatted and working!

Code: Select all

<?php
//A League Team Percentages
$percentageTeamH = '';
$percentageTeamV = '';
$percentageTeamH = ($totalTeamA / 15);
$percentageTeamV = ($totalTeamB / 15);
echo stripslashes($teamH) . "'s Win/Loss percentage is " . ltrim(number_format($percentageTeamH,3),"0") . "<br>";
echo stripslashes($teamV) . "'s Win/Loss percentage is " . ltrim(number_format($percentageTeamV,3),"0") . "<br>";
//var_dump($totalTeamA,$totalTeamB,$teamH,$teamV,$percentageTeamH,$percentageTeamV);
?>
OUTPUT
Abbey's Wrecking Crew's Win/Loss percentage is .533
New Eagles's Win/Loss percentage is .467

Thanks for pointing me in the right direction Chris. Be prepared.. I'll be back :)
In fact I will start a new thread so we don't muddle this one up, but it will be about pulling in a percentage stored in a MySQL database and then adding this to it... now that I say it, it sounds easy enough :)


rad1964
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Getting an error on a simple percentage formula

Post by Christopher »

rad1964 wrote:Thanks for pointing me in the right direction Chris. Be prepared.. I'll be back :)
You were awesome at keeping after the problem. Well done! :)
rad1964 wrote:In fact I will start a new thread so we don't muddle this one up, but it will be about pulling in a percentage stored in a MySQL database and then adding this to it... now that I say it, it sounds easy enough :)
Good idea. I will look for it.
(#10850)
Post Reply