0

Hello all...New to the group here. Glad to be part of the site as I just found it and am excited about learning!

Have a hopefully easy question for anyone who has the time to assist. Yes, it is for homework, so I hope I don't break any rules here. I just need assistance and it would be greatly appreciated...

I am taking an old script I had to calculate payroll taxes and I would like to turn the calculation loop I am using into its own function, function taxcalc().

After uploading to my server, I am getting errors, starting on line 20 where the first "else" comes in.

<html>
<body>
<head>
<title>taxwagefunction.php[</title>
</head>
<?php
    $hours = $_POST['enter_hours'];
    $payrate = $_POST['enter_RPay'];
    $name = $_POST['enter_name'];

    function taxcalc() {

    if ($hours > 40) 
        $gross = $payrate * 40;
        $ot = ($hours - 40) * ($payrate * 1.5);
        $TotGross = $gross + $ot;

     {

    else 
        $gross = $payrate * $hours;
    }

    if ($gross <= 850) {
        $tax = $gross * .15;
    }
    else if ($gross > 850 && $gross <=2060) {
        $tax = $gross * .28;
    }
    else if ($gross > 2060 && $gross <= 3900) {
        $tax = $gross * .32;
    }
    else {
        $tax = $gross * .40;
    }

    $net = $gross - $tax;

    echo "<p>";
    echo $name, " worked ", $hours, " hours, and earned $", $TotGross, ". <br />";
    echo $name, " was taxed $", $tax, " for a net pay total of $", $net, ".";
    echo "</p>";
?>
</body>

</html>

My question is, how do I group the whole If/else statement as it's own function? Do I have the brackets in the wrong place or am I so far off I should scrap it and start over?

Thank you for your assistance and time.

Edited by Reverend Jim: Fixed formatting

3
Contributors
5
Replies
18
Views
7 Years
Discussion Span
Last Post by diafol
0

My apologies. I hope this worked correctly.

<html>
<body>
<head>
<title>taxwagefunction.php[</title>
</head>
<?php
$hours = $_POST['enter_hours'];
$payrate = $_POST['enter_RPay'];
$name = $_POST['enter_name'];

function taxcalc() {

if ($hours > 40) 
$gross = $payrate * 40;
$ot = ($hours - 40) * ($payrate * 1.5);
$TotGross = $gross + $ot;

{

else 
$gross = $payrate * $hours;
}

if ($gross <= 850) {
$tax = $gross * .15;
}
else if ($gross > 850 && $gross <=2060) {
$tax = $gross * .28;
}
else if ($gross > 2060 && $gross <= 3900) {
$tax = $gross * .32;
}
else {
$tax = $gross * .40;
}

$net = $gross - $tax;

echo "<p>";
echo $name, " worked ", $hours, " hours, and earned $", $TotGross, ". <br />";
echo $name, " was taxed $", $tax, " for a net pay total of $", $net, ".";
echo "</p>";
?>
</body>
</html>
0

My apologies. I hope this worked correctly.

<html>
<body>
<head>
<title>taxwagefunction.php[</title>
</head>
<?php
$hours = $_POST['enter_hours'];
$payrate = $_POST['enter_RPay'];
$name = $_POST['enter_name'];

function taxcalc() {

if ($hours > 40) 
$gross = $payrate * 40;
$ot = ($hours - 40) * ($payrate * 1.5);
$TotGross = $gross + $ot;

{

[B]else [U]{[/U]
$gross = $payrate * $hours;
}[/B]

if ($gross <= 850) {
$tax = $gross * .15;
}
else if ($gross > 850 && $gross <=2060) {
$tax = $gross * .28;
}
else if ($gross > 2060 && $gross <= 3900) {
$tax = $gross * .32;
}
else {
$tax = $gross * .40;
}

$net = $gross - $tax;

echo "<p>";
echo $name, " worked ", $hours, " hours, and earned $", $TotGross, ". <br />";
echo $name, " was taxed $", $tax, " for a net pay total of $", $net, ".";
echo "</p>";
?>
</body>
</html>

on the else I think you have missed a bracket. I have bolded it and put in the extra bracket and I have underlined the extra bracket.

Edited by piers: n/a

0

I appreciate the reply. For some reason it still doesn't like something in the code. Is it my loop structure by chance? I have else, else if, if- I just wonder if I should keep it all elses or all else ifs?

0

I don't know where you call the function. Also the function doesn't return anything. I can't see where it ends either. Your braces look a little messed up. Without proper indenting, your code is pretty unreadable. Sorry.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.