The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Unexpected variable
Discuss Unexpected variable in the PHP Development forum on Dev Shed. Unexpected variable PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 7th, 2012, 06:49 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
|
Unexpected variable
I'm getting an unexpected variable error on line 14 of this code. I don't see the problem:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
02 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
03 <html xmlns="http://www.w3.org/1999/xhtml">
04 <head>
05 <title>Paycheck Results</title>
06 <meta http-equiv="Content-Type"
07 content="text/html; charset=iso-8859-1" />
08 <link rel="stylesheet" href="php_styles.css" type="text/css" />
09 </head>
10 <body>
11 <h1>Paycheck Calculations</h1>
12 <?php
14 $hoursWorked = $_GET["hours"];
15 $wages = $_GET["wage"];
16
17
18 echo "You entered that you worked: $hoursWorked hours.<br>";
19 echo "You entered that your hourly wage is: $wages per hour.<br>";
20
21 if ($hoursWorked <= 40)
22 {
23 $payCheck = ($hoursWorked * $wages);
24 echo "Your Paycheck is: $payCheck";
25 }
26
27 if ($hoursWorked > 40)
28 {
29 $payCheck = (($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
30 echo "Your Paycheck is: $payCheck";
31 }
32
33
34 ?>
35 </body>
36 </html>
|

October 7th, 2012, 07:33 PM
|
|
|
What does the URL look like that loads this script? To make sure $_GET contains what you expect put this first:
PHP Code:
echo "<pre>";
print_r($_GET);
echo "</pre>";
From a programming practice standpoint you should be checking to make sure the variable you want exists before you try to use it ('isset'). Also you should consider using post rather than get method for your form. This data looks like it could be sensitive and showing it in the URL may not be a good idea. It also lends itself to abuse since it is easy to modify a URL.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

October 7th, 2012, 07:38 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
|
What I am really trying to do is accept the input from the user. Hours and hourly wage. Is there a better way? I still dont understand the variable error.
|

October 7th, 2012, 08:25 PM
|
|
|
The variable error is explicit. That $_GET value you are trying to use does not exist. That is why I asked for the URL. That must contain the variables you are requesting. Presumably you are getting input from the user via a form. Rather then use type=get, use type=post. Access the variables the same way except use $_POST rather than $_GET. You can read up on it here.
|

October 7th, 2012, 08:26 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
Ok, i have almost got it working i think. I am getting an unexpected ; on line 80 in the code below. I know it should be there. I i take that off, the I get an unexpected } on line 81.
PHP 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>
<?php
$errorcount=0;
$wages=$_POST['wages'];
$hours=$_POST['hours'];
if (isset($_POST['submit']))
{ //begins if1//
//echo "<p>The submit button has been pressed.</p>\n";
if (is_numeric($wages))
{//begins if #2
if ($wages >0)
{//begins if #3
$weekly_wages = $wages;
}//ends if #3
else
{//goes with if #3
++$errorcount;
echo "<p>Weekly wage must to be greater than 0</p>";
}//ends else #3
}//ends if #2
else
{//goes with if #2
++$errorcount;
echo "<p>You must enter a number for the wage</p>\n";
//echo "<p>Error Count: " . $errorcount . "</p>\n";
}//ends else #2
} //closes if#1 wage isset
if (isset($_POST['submit']))
{ //begins if1//
//echo "<p>The submit button has been pressed.</p>\n";
if (is_numeric($hours))
{//begins if #1 hours
if($wages >= 0)
{//begins if #2
if ($hours > 40)
{//begins if #3
$bonus = true;
echo "<p>Overtime: " . $bonus . "</p>\n";
}//ends if #3
}//ends if #2
else
{//begins else #2
++$errorcount;
echo "<p>The number of hours must be 0 or higher</p>\n";
}//ends if #2
}//ends if #1
else
{//begins else #1
++$errorcount;
echo "<p>The value for hours must be a number</p>\n";
}//ends else #1
} //closes if#1 isset submit
if ($errorcount == 0 && isset($_POST['submit']))
{//begins if calculations
if ($bonus == 1)
{
(($hours * $wages) + (($hours - 40) * $wages * 1.5);
}
else
{
$bonus_amt = 0;
}
$weekly_pay = $weekly_wages + $bonus_amt;
echo "<p>Weekly Salary= $" . number_format($weekly_salary, 2) . ".</p>\n";
echo "<p>Overtime Pay = $" . number_format($bonus_amt, 2) . ".</p>\n";
echo "<p>Your total Weekly Pay is $" . number_format($weekly_pay + $bonus_amt, 2) . ".</p>\n";
echo "<p><a href = 'PaycheckCalc.html'>Calculate another paycheck?</a></p>\n";
}//ends if 1//
else
{//begins else for calculation
?>
<h2 style="text-align:center">Paycheck Calculation</h2>
<form action=
"PaycheckCalc.php" method="post">
<p>Weekly Wage: <input type="text" name="wages" /></p>
<p>Number of Hours Worked: <input type="text" name="hours" /></p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
<?php
}//ends else to display form
?>
</body>
</html>
|

October 7th, 2012, 08:38 PM
|
|
|
|
Without knowing which is line 80 it is tough to say what is wrong. You still need to check your variables before using them or it will crash your script like before.
|

October 7th, 2012, 08:42 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
this is line 80:
PHP Code:
(($hours * $wages) + (($hours - 40) * $wages * 1.5);
|

October 7th, 2012, 08:47 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
i fixed that with a right ). now i get undefined index:
PHP Code:
$wages=$_POST["wages"];
$hours=$_POST['hours'];
|

October 7th, 2012, 11:06 PM
|
|
Contributing User
|
|
Join Date: Sep 2012
Posts: 112
  
Time spent in forums: 14 h 27 m 57 sec
Reputation Power: 5
|
|
still encountering the undefined index on these lines:
PHP Code:
$wages = $_POST['wages'];
$hours = $_POST['hours'];
of this code:
PHP 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>
<?php
$errorcount=0;
$wages = $_POST['wages'];
$hours = $_POST['hours'];
if (!isset($_POST['submit']))
{ //begins if1//
//echo "<p>The submit button has been pressed.</p>\n";
if (is_numeric($wages))
{//begins if #2
if ($wages >0)
{//begins if #3
$weekly_wages = $wages;
}//ends if #3
else
{//goes with if #3
++$errorcount;
echo "<p>Weekly wage must to be greater than 0</p>";
}//ends else #3
}//ends if #2
else
{//goes with if #2
++$errorcount;
echo "<p>You must enter a number for the wage</p>\n";
//echo "<p>Error Count: " . $errorcount . "</p>\n";
}//ends else #2
} //closes if#1 wage isset
if (!isset($_POST['submit']))
{ //begins if1//
//echo "<p>The submit button has been pressed.</p>\n";
if (is_numeric($hours))
{//begins if #1 hours
if($wages >= 0)
{//begins if #2
if ($hours > 40)
{//begins if #3
$bonus = true;
echo "<p>Overtime: " . $bonus . "</p>\n";
}//ends if #3
}//ends if #2
else
{//begins else #2
++$errorcount;
echo "<p>The number of hours must be 0 or higher</p>\n";
}//ends if #2
}//ends if #1
else
{//begins else #1
++$errorcount;
echo "<p>The value for hours must be a number</p>\n";
}//ends else #1
} //closes if#1 isset submit
$ot_amt = 0;
if ($errorcount == 0 && isset($_POST['submit']))
{//begins if calculations
if ($hours > 40)
{
$ot_amt = (($hours - 40) * ($wages * 1.5));
$weekly_pay = ($hours * $wages) ;
}
else
{
$weekly_pay = ($hours * $wages) ;
}
//$weekly_pay = $weekly_wages + $bonus_amt;
echo "<p>Weekly Salary= $" . number_format($weekly_pay, 2) . ".</p>\n";
echo "<p>Overtime Pay = $" . number_format($ot_amt, 2) . ".</p>\n";
echo "<p>Your total Weekly Pay is $" . number_format($weekly_pay + $ot_amt, 2) . ".</p>\n";
echo "<p><a href = 'PaycheckCalc.html'>Calculate another paycheck?</a></p>\n";
}//ends if 1//
else
{//begins else for calculation
?>
<h2 style="text-align:center">Paycheck Calculation</h2>
<form action=
"PaycheckCalc.php" method="post">
<p>Weekly Wage: <input type="text" name="wages" /></p>
<p>Number of Hours Worked: <input type="text" name="hours" /></p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
<?php
}//ends else to display form
?>
</body>
</html>
I also get this on another script i have. What causes this? Thanks
|

October 8th, 2012, 07:01 AM
|
|
|
|
I've already explained it. See my previous responses.
|

October 8th, 2012, 03:37 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 4

Time spent in forums: 58 m 26 sec
Reputation Power: 0
|
|
Quote: | if (!isset($_POST['submit'])) |
only returns true when $_POST['submit'] is not set... you know that right?
|

October 9th, 2012, 03:23 AM
|
 |
Confused badger
|
|
Join Date: Mar 2009
Location: West Yorkshire
|
|
Quote: | Originally Posted by seandisanti only returns true when $_POST['submit'] is not set... you know that right? |
Well spotted ...
PHP Code:
if (!isset($_POST['submit']))
{ //begins if1//
//echo "<p>The submit button has been pressed.</p>\n";
reads: if $_POST['submit'] is not set then echo "The submit button has been pressed".
Remove the ! before the isset to make it work the right way around; as for the undefined variable, try moving the two lines causing this error to after the if statement above, like so:
PHP Code:
if (isset($_POST['submit'])) {
$wages = $_POST['wages'];
$hours = $_POST['hours'];
echo "<p>The submit button has been pressed.</p>\n";
...
Good luck!
__________________
The number for UK Emergencies is changing, the new number is 0118 999 881 999 119 7253
"For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
- George Orwell, 1984
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|