8 Replies - 5222 Views - Last Post: 05 October 2009 - 01:11 PM Rate Topic: -----

#1 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Using PHP to calculate a paycheck - issues with doing this

Post icon  Posted 04 October 2009 - 08:24 PM

I am very new to PHP. Right now we are working with functions and control structures. I have an assignment where I have to calculate earnings based on wage, hours, and potential overtime over 40 hours at 1.5.

There are no examples in my text at this point that I can follow from for direction and at this point I am going blind.

Can someone please provide assistance or direction on what I should be doing? I have changed the code repeatedly and as of now, all I am getting are errors.

Parse error: syntax error, unexpected T_STRING in C:\xampp\xampp\htdocs\Paycheck.php on line 24


paycheck.html code:


!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paycheck Calculator</title>
<meta http-equiv="Content-Type"
	content="text/html; charset=iso-8859-1" />
</head>
<body>
	<table>
	<tr>
	</tr>
	<tr>
		<td><h1>Paycheck Calculator</h1></td>
	</tr>
	<tr>
		

	</tr>
</table>
<hr />
	
	<form method="get" action="Paycheck.php">
		<p>Hours worked:
		<input type="text" name="hours" id="hours" />
		</p>
		<p>Employee wage:
		<input type="text" name="wage" id="wage" /><br /><br />
		<input type="submit" />
		</p>

	</form>
</body>
</html>




paycheck.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paycheck Results</title>
<meta http-equiv="Content-Type"
	content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Paycheck Calculations</h1>
<?php

$hoursWorked = $_GET["hours"];
$wages = $_GET["wage"];
$x = $hours;
$y = $wage;
$z = $pay;

if ($x is <= 40)
{
	$payCheck = ("$z = $x * $y")
		echo "Your Paycheck is: $payCheck";
}

if ($x is > 40) 
{
	$payCheck = ("$z = ($x *$y) + (($x - 40) * $y * 1.5")
	   echo "Your Paycheck is: $payCheck";
}

?>
</body>
</html>




Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Using PHP to calculate a paycheck - issues with doing this

#2 aNiXtEr  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 08-February 07

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 04 October 2009 - 09:35 PM

Couple of things.
Try removing the quotes around the variables.
For instance: ("$z = $x * $y")

Second, why do you initialize a variable like $pay and then set it equal to $paycheck?
Try: $payCheck = $x * $y"

Do this for the second statement as well

View Postgreenbluekidz, on 4 Oct, 2009 - 07:24 PM, said:

I am very new to PHP. Right now we are working with functions and control structures. I have an assignment where I have to calculate earnings based on wage, hours, and potential overtime over 40 hours at 1.5.

There are no examples in my text at this point that I can follow from for direction and at this point I am going blind.

Can someone please provide assistance or direction on what I should be doing? I have changed the code repeatedly and as of now, all I am getting are errors.

Parse error: syntax error, unexpected T_STRING in C:\xampp\xampp\htdocs\Paycheck.php on line 24


paycheck.html code:


!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paycheck Calculator</title>
<meta http-equiv="Content-Type"
	content="text/html; charset=iso-8859-1" />
</head>
<body>
	<table>
	<tr>
	</tr>
	<tr>
		<td><h1>Paycheck Calculator</h1></td>
	</tr>
	<tr>
		

	</tr>
</table>
<hr />
	
	<form method="get" action="Paycheck.php">
		<p>Hours worked:
		<input type="text" name="hours" id="hours" />
		</p>
		<p>Employee wage:
		<input type="text" name="wage" id="wage" /><br /><br />
		<input type="submit" />
		</p>

	</form>
</body>
</html>




paycheck.php:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paycheck Results</title>
<meta http-equiv="Content-Type"
	content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Paycheck Calculations</h1>
<?php

$hoursWorked = $_GET["hours"];
$wages = $_GET["wage"];
$x = $hours;
$y = $wage;
$z = $pay;

if ($x is <= 40)
{
	$payCheck = ("$z = $x * $y")
		echo "Your Paycheck is: $payCheck";
}

if ($x is > 40) 
{
	$payCheck = ("$z = ($x *$y) + (($x - 40) * $y * 1.5")
	   echo "Your Paycheck is: $payCheck";
}

?>
</body>
</html>




Thanks in advance!

Was This Post Helpful? 0
  • +
  • -

#3 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 06:10 AM

ok, I made some changes to the code. Now when I enter information on Paycheck.html, it will redirect to paycheck.php. It still will not display the $payCheck amount. Where am I going wrong?

Revised code for payCheck.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paycheck Results</title>
<meta http-equiv="Content-Type"
	content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Paycheck Calculations</h1>
<?php

$hoursWorked = $_GET["hours"];
$wages = $_GET["wage"];
$x = $hours;
$y = $wage;

echo "You entered that you worked: $hoursWorked<br>";
echo "You entered that your hourly wage is: $wages<br>";

if ($x <= 40)
{
	$payCheck = ("$x * $y");
		echo "Your Paycheck is: $payCheck";
}

if ($x > 40) 
{
	$payCheck = ("($x *$y) + (($x - 40) * $y * 1.5");
	   echo "Your Paycheck is: $payCheck";
}


?>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#4 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 169
  • Joined: 14-February 09

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 08:52 AM

I think the problem is that the number that the person puts into your form is being taken as a string, not an int. Therefore you are trying to see if a string is less than or equal to, or greater than 40 and the program doesn't know what to do.

Also, there is really no reason to use $x or $y, the $hoursWorked and $wages variables will suffice.

I'm not really sure if this is still good practice, but you might want to do something like this:

$hoursWorked = $_GET['hours'];
$wages = $_GET['wage'];
if (is_int($hoursWorked) && is_int($wages)){
	 //nested if functions that determine whether hours are greater or less than 40, and what to do in each situation
}
else{
	echo "Input was not an integer."
}


Something along those lines, you need to verify that the input was an integer. I hope this helps, if not, I'm happy to answer any more questions :)

This post has been edited by cmwise: 05 October 2009 - 08:57 AM

Was This Post Helpful? 1
  • +
  • -

#5 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 09:55 AM

View Postcmwise, on 5 Oct, 2009 - 09:52 AM, said:

Therefore you are trying to see if a string is less than or equal to, or greater than 40 and the program doesn't know what to do.
That shouldn't present a problem. So long as the strings are numeric, PHP can automatically convert them to their integer or floating point equivalents. So a comparison like "40" <= 40 will return true, with the type conversion being implicit.

Of course, you're right that there should be some form of sanity checking on the input. However, using is_int() on a value straight from $_GET is far too strict, because $_GET, $_POST, and $_REQUEST always return their data as strings. So unless you explicitly cast those values to int's before the check (which kind of defeats the purpose), the is_int() check in your example will always fail. Something like is_numeric(), which works on strings and accounts for decimals, or even just a blind type cast, would be more appropriate.

Quote

Also, there is really no reason to use $x or $y, the $hoursWorked and $wages variables will suffice.

Actually, that's the root of the problem. The assignments to $x and $y are incorrect, which is half the reason the calculation is failing. The GET variables are assigned to $wages and $hoursWorked, but then the undefined variables $wage and $hours get assigned to $x and $y. If the OP had had notices turned on, this would have been immediately obvious.

The second half of the problem is that the arithmetic to compute $payCheck is being done inside a string, which just doesn't make sense. Arithmetic expressions don't get evaluated inside quotes. Unless the OP wants to see the equation for the calculation, rather than the actual result, then the quotes need to go away.
Was This Post Helpful? 1
  • +
  • -

#6 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 169
  • Joined: 14-February 09

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 09:58 AM

Listen to him ^^ I was in class when I responded haha
Was This Post Helpful? 1
  • +
  • -

#7 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 12:09 PM

Ok, I took your advice and made the following changes. Now it displays nothing. I am getting
Parse error: syntax error, unexpected ';' in C:\xampp\xampp\htdocs\Paycheck.php on line 33.

The revised code is follows:

paycheck.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Paycheck Results</title>
<meta http-equiv="Content-Type"
	content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
</head>
<body>
<h1>Paycheck Calculations</h1>
<?php

$hoursWorked = $_GET["hours"];
$wages = $_GET["wage"];


echo "You entered that you worked: $hoursWorked hours.<br>";
echo "You entered that your hourly wage is: $wages per hour.<br>";

if ($hoursWorked <= 40)
{
	$payCheck = ($hoursWorked * $wages);
		echo "Your Paycheck is: $payCheck";
}

if ($hoursWorked > 40) 
{
	$payCheck = (($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
	   echo "Your Paycheck is: $payCheck";
}


?>
</body>
</html>



Part of the problem is that we haven't worked with math functions so if I thought that I was lost before, I definitely am now.
Was This Post Helpful? 0
  • +
  • -

#8 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 12:46 PM

You've got unbalanced parentheses on the calculation line in the second if block. There's one more opening paren than closing paren, so just delete that first one.

Also, you might want to have another look at your math on that second if block. I assume you're trying to calculate standard overtime pay (regular pay for the first 40 yours plus time-and-a-half for every hour thereafter), but you're actually calculating total hours times payrate and adding 1.5 time the rate for every hour over 40. So you're actually counting the hours over 40 twice.
Was This Post Helpful? 1
  • +
  • -

#9 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Re: Using PHP to calculate a paycheck - issues with doing this

Posted 05 October 2009 - 01:11 PM

Removing the extra bracket worked!

How long is it supposed to take to get the hang of this programming stuff??

Guys, your help was greatly appreciated, again thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1