0

Okay so here's my code:

function payCalc($hours, $wage, $overtime, $paycheck) {
if ($hours <= 40) {
    $paycheck = $hours * $wage;
}
else {
    $paycheck = $hours * $overtime;
}
return $paycheck;

}

$name = $_POST['name'];
$dept = $_POST['dept'];
$hours = $_POST['hours'];
$wage = $_POST['wage'];

$paycheck = payCalc($hours, $wage, $overtime, $paycheck);

$overTime = $wage * 1.5;
print "Your paycheck for this period is:"; 
print $paycheck; 
print ".";

The problem is I keep getting undefined variable errors on $paycheck. As far as I know, I have the if/else set up properly, but it's not returning $paycheck. Which means that on the "print $paycheck;" line, it's not able to print. What am I missing?

EDIT: There IS more to this, but it's just echo lines to spit out the $name and $dept variables.

3
  • 2
    $hours, $wage and $overtime don't exist within the scope of your function - pass them as arguments; nor are you calling the payCalc() function anywhere; and in that function, you're not doing anything to update the value of $paycheck Commented Feb 18, 2013 at 9:40
  • 2
    you don't seem to be calling the payCalc() function anywhere? Commented Feb 18, 2013 at 9:41
  • Read PHP: Variable scope - functions don't close over variables like that. Commented Feb 18, 2013 at 9:41

7 Answers 7

3

You need to declare

$paycheck = payCalc();

so that you get the result coming from the function.

Also take in consideration:

if ($hours <= 40) {
    $paycheck = $hours * $wage;
}
else {
    $paycheck = $hours * $overtime;

that is, populate $paycheck.

And finally, take into account what has been advised by other users: if you want to use external variables, you need to send them to the function:

function payCall($hours, $wage, $overtime) {
   ...
}

and then use them when calling the function:

$paycheck = payCalc($hours, $wage, $overtime);
Sign up to request clarification or add additional context in comments.

8 Comments

I did all that and now I'm getting 'undefined index' and 'undefined variable' errors for all of my variables.
Please update your code in the question so we can review what can be wrong now.
OK @user2082686, what you have to do is to define the function previously to calling it. That is, all the block starting on function payCalc($hours, $wage, $overtime, $paycheck) { and finishing in return $paycheck; } has to be moved to the top of the file.
Did that. Updated code at the top. Same errors. Undefined index error on $name, $dept, $hours, $wage and undefined variable for $overtime and $paycheck.
Also, $paycheck is returning now at the bottom (with the print line) but as 0. @fedorqui
|
0

In a function, you cannot call variables from outside it.

There's no way your function knows $paycheck.

do it like this:

function payCall($hours, $wage, $overtime, $paycheck) {
 //DO STUFF
return $paycheck;
}

and call it like this:

echo payCall($hours, $wage, $overtime, $paycheck);

Comments

0

Well, you haven't really set the variable "paycheck" in your function.

Set the variable $paycheck before your $hours * $overtime and $hours * $wage.

However, before being able to access those, you have to set them as parameters.

The code should then be something like this:

$name = $_POST['name'];
$dept = $_POST['dept'];
$hours = $_POST['hours'];
$wage = $_POST['wage'];

$overTime = $wage * 1.5;

function payCalc($hours, $wage, $overtime) {
    if ($hours <= 40) {
        $paycheck = $hours * $wage;
    }
    else {
        $paycheck = $hours * $overtime;
    }
    return $paycheck;
}

print "Your paycheck for this period is:"; 
print payCalc($hours, $wage, $overTime); 
print ".";

The function now returns the variable "paycheck", which is set within the if-statement and the else-statement.

Does this make any sense to you?

Best,

Nikolaj Jepsen

Comments

0

Try this:

 if( isset($_POST['name']) && isset($_POST['dept']) && isset($_POST['hours']) && isset($_POST['wage']){
    $paycheck = '';
    $name = $_POST['name'];
    $dept = $_POST['dept'];
    $hours = $_POST['hours'];
    $wage = $_POST['wage'];

    $overTime = $wage * 1.5;

    function payCalc() {
        if ($hours <= 40) {
        $paycheck =$hours * $wage;
    }
    else {
        $paycheck = $hours * $overtime;
    }
    return $paycheck;
}

print "Your paycheck for this period is:"; 
print payCalc(); 
print ".";
}

Comments

0
/// COPY AND PASTE THE BELOW CODE

$paycheck = '';

$name = $_POST['name'];

$dept = $_POST['dept'];

$hours = $_POST['hours'];

$wage = $_POST['wage'];

$overTime = $wage * 1.5;

function payCalc($paycheck,$hours,$wage,$overTime) {

    if ($hours <= 40) {

       $paycheck = $hours * $wage;

    }

    else {

       $paycheck =  $hours * $overTime;

    }

    return $paycheck;

}


print "Your paycheck for this period is:"; 

print payCalc($paycheck,$hours,$wage,$overTime); 

print ".";

Comments

-1

Try using global keyword.

$paycheck = '';
$name = $_POST['name'];
$dept = $_POST['dept'];
$hours = $_POST['hours'];
$wage = $_POST['wage'];

$overTime = $wage * 1.5;

function payCalc() {
    global $hours,$wage,$overtime,$paycheck;
    if ($hours <= 40) {
        $hours * $wage;
    }
    else {
        $hours * $overtime;
    }
    return $paycheck;
}

Comments

-1

You need to use global keyword to refer to global variables :

$paycheck = '';
$name = $_POST['name'];
$dept = $_POST['dept'];
$hours = $_POST['hours'];
$wage = $_POST['wage'];

$overTime = $wage * 1.5;

function payCalc() {
    global $hours, $wage, $overtime;
    $paycheck = 0;
    if ($hours <= 40) {
        $paycheck = $hours * $wage;
    }
    else {
        $paycheck = $hours * $overtime;
    }
    return $paycheck;
}


$paycheck = payCalc();
print "Your paycheck for this period is:"; 
print $paycheck; 
print ".";

2 Comments

This is clearly a new user of PHP - let's not advise such bad practise from the start.
@cbuckley I am indeed new to this. I wouldn't know how to handle global variables at this point and I'd rather learn what's wrong (which I am through reading all this) rather than just put that (global) in without knowing what it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.