好的,这是我的代码:
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 ".";
问题是我在$ paycheck上一直收到未定义的变量错误。据我所知,我已正确设置if / else,但它没有返回$paycheck
。这意味着在“print $paycheck;
”行上,它无法打印。我错过了什么?
编辑:还有更多内容,但只是回显一行来吐出$name
和$dept
变量。
答案 0 :(得分:3)
您需要声明
$paycheck = payCalc();
这样你就可以得到函数的结果。
还要考虑:
if ($hours <= 40) {
$paycheck = $hours * $wage;
}
else {
$paycheck = $hours * $overtime;
即填充$ paycheck。
最后,考虑一下其他用户建议的内容:如果你想使用外部变量,你需要将它们发送给函数:
function payCall($hours, $wage, $overtime) {
...
}
然后在调用函数时使用它们:
$paycheck = payCalc($hours, $wage, $overtime);
答案 1 :(得分:0)
在函数中,不能从外部调用变量。
你的功能无法知道$paycheck.
这样做:
function payCall($hours, $wage, $overtime, $paycheck) {
//DO STUFF
return $paycheck;
}
并将其称为:
echo payCall($hours, $wage, $overtime, $paycheck);
答案 2 :(得分:0)
试试这个:
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 ".";
}
答案 3 :(得分:0)
好吧,你还没有在你的函数中设置变量“paycheck”。
在$ hours * $加班和$ hours * $ wage之前设置变量$ paycheck。
但是,在能够访问它们之前,您必须将它们设置为参数。
代码应该是这样的:
$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 ".";
该函数现在返回变量“paycheck”,该变量在if语句和else语句中设置。
这对你有意义吗?
最佳,
Nikolaj Jepsen
答案 4 :(得分: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 ".";
答案 5 :(得分:-1)
尝试使用全局关键字。
$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;
}
答案 6 :(得分:-1)
您需要使用全局关键字来引用全局变量:
$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 ".";