0

Getting an undefined variable: pay error.

It still puts out the paycheck amount... I just want the notice to go away. Provide new code if any please :)

$hours = (int) $_GET["hours"]; 
$wages = (int) $_GET["wage"]; 

$overtime = max($hours - 40, 0); 
$pay += $overtime * $wages * 1.5; 
$pay += ($hours - $overtime) * $wages; 

echo "Hours Worked: " . $hours . "<br>"; 
echo "Pay rate (per hour): $" . number_format($wages, 2) . "<br>"; 
echo "Overtime Hours: " . $overtime . "<br>"; 
echo "Your Paycheck is: $" . number_format($pay, 2) . "<br>";

If you need any more info than what I have provide please let me know. I dont think I need to post the html stuff etc. Thanks again!

reformed
  • 4,505
  • 11
  • 62
  • 88
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Rizier123 Feb 26 '15 at 06:44

2 Answers2

1

Either initialize e.g., $pay = 0; above the += assignment or else change the first to =. For example:

$overtime = max($hours - 40, 0); 
$pay = $overtime * $wages * 1.5; // <--- change += to =
$pay += ($hours - $overtime) * $wages;

The reason you got the error was because the += operator means "add the value on the right to the value of the existing variable on the left, then assign the sum to the variable on the left. So the $pay variable needs to be initialized first to use the += operator.

reformed
  • 4,505
  • 11
  • 62
  • 88
0

Add this line (undefined variable is raised because $pay is not inicialized):

$pay = 0;

after:

$hours = (int) $_GET["hours"]; 
$wages = (int) $_GET["wage"];
teo
  • 801
  • 6
  • 14