PHP函数不执行简单的逻辑任务

PHP函数不执行简单的逻辑任务,php,Php,我得到了这个简单的php脚本。它所做的是: 用户将输入其工资。 用户将从下拉列表中选择其成员类型。 如果用户选择成员类型,php脚本将执行简单的逻辑 在使用时,它将执行并返回适当的答复 请参考我的函数以便更好地理解 如果用户选择自营职业者、自愿会员或OFW,则 将再次执行其分配的功能 我的问题是: 程序正在运行,没有错误。问题是它没有显示任何内容。好像什么都没发生。有什么问题吗 顺便说一句,我8小时前刚学会PHP,我还在尝试这些东西的初级阶段。请帮帮我。在这件事上被困了几个小时。请温柔一点: &

我得到了这个简单的php脚本。它所做的是:

用户将输入其工资。 用户将从下拉列表中选择其成员类型。 如果用户选择成员类型,php脚本将执行简单的逻辑 在使用时,它将执行并返回适当的答复 请参考我的函数以便更好地理解 如果用户选择自营职业者、自愿会员或OFW,则 将再次执行其分配的功能 我的问题是:

程序正在运行,没有错误。问题是它没有显示任何内容。好像什么都没发生。有什么问题吗

顺便说一句,我8小时前刚学会PHP,我还在尝试这些东西的初级阶段。请帮帮我。在这件事上被困了几个小时。请温柔一点:

<form method="POST">
Salary: <input id="salarytext" type="text" name="salary" onkeypress="return isNumberKey(event)"><br>
Membership Type:
<select name="membershiptype">
        <option value="employed">Employed</option>
        <option value="SE">Self Employed</option>
        <option value="VM">Voluntary Member</option>
        <option value="OFW">OFW</option>
</select>

<br/>

<input type="submit" />

</form>

<?php

$a = (isset($_POST['salary'])) ? $_POST['salary'] : '';
$b = (isset($_POST['membershiptype'])) ? $_POST['membershiptype'] : '';

function employed () {

        if (empty ($a)) {echo "";}
        elseif ($a<1000) {echo "Your Salary for the Period is $a. <br> You didn't reach the minimum compensation for the period. No SSS Contribution Deduction.";}
        elseif ($a <1249.99) {echo "Your ER is 80.7 and Your EE is 33.3 sevmofw"; }
        elseif ($a <1749.99) {echo "Your ER is 116 and Your EE is 50";}
        elseif ($a <2249.99) {echo "Your ER is 151.3 and Your EE is 66.7";}
        elseif ($a <2749.99) {echo "Your ER is 186.7 and Your EE is 83.3";}
        elseif ($a <3249.99) {echo "Your ER is 222 and Your EE is 100";}
        elseif ($a <3749.99) {echo "Your ER is 257.3 and Your EE is 116.7";}
        elseif ($a <4249.99) {echo "Your ER is 292.7 and Your EE is 133.3";}
        elseif ($a <4749.99) {echo "Your ER is 328 and Your EE is 150";}
        elseif ($a <5249.99) {echo "Your ER is 363.3 and Your EE is 166.7";}
        elseif ($a <5749.99) {echo "Your ER is 398.7 and Your EE is 183.3";}
        elseif ($a <6249.99) {echo "Your ER is 434 and Your EE is 200";}
        elseif ($a <6749.99) {echo "Your ER is 469.3 and Your EE is 216.7";}
        elseif ($a <7249.99) {echo "Your ER is 504.7 and Your EE is 233.3";}
        elseif ($a <7749.99) {echo "Your ER is 540 and Your EE is 250";}
        elseif ($a <8249.99) {echo "Your ER is 575.3 and Your EE is 266.7";}
        elseif ($a <8749.99) {echo "Your ER is 610.7 and Your EE is 283.3";}
        elseif ($a <9249.99) {echo "Your ER is 646 and Your EE is 300";}
        elseif ($a <9749.99) {echo "Your ER is 681.3 and Your EE is 316.7";}
        elseif ($a <10249.99) {echo "Your ER is 716.7 and Your EE is 333.3";}
        elseif ($a <10749.99) {echo "Your ER is 752 and Your EE is 350";}
        elseif ($a <11249.99) {echo "Your ER is 787.3 and Your EE is 366.7";}
        elseif ($a <11749.99) {echo "Your ER is 822.7 and Your EE is 383.3";}
        elseif ($a <12249.99) {echo "Your ER is 858 and Your EE is 400";}
        elseif ($a <12749.99) {echo "Your ER is 893.3 and Your EE is 416.7";}
        elseif ($a <13249.99) {echo "Your ER is 928.7 and Your EE is 433.3";}
        elseif ($a <13749.99) {echo "Your ER is 964 and Your EE is 450";}
        elseif ($a <14249.99) {echo "Your ER is 999.3 and Your EE is 466.7";}
        elseif ($a <14749.99) {echo "Your ER is 1034.7 and Your EE is 483.3";}
        elseif ($a <=30000) {echo "Your ER is 1090 and Your EE is 500";}
        elseif ($a > 30000) {echo "You reached the maximum. Your ER is 1090 and Your EE is 500";}
}

function  sevmofw () {

        if (empty ($a)) {echo "";}
        elseif ($a<1000) {echo "NOT EMPLOYED. Your Salary for the Period is $a. <br> You didn't reach the minimum compensation for the period. No SSS Contribution Deduction.";}
        elseif ($a <1249.99) {echo "NOT EMPLOYED. Your ER is 80.7 and Your EE is 33.3 sevmofw"; }
        elseif ($a <1749.99) {echo "NOT EMPLOYED. Your ER is 116 and Your EE is 50";}
        elseif ($a <2249.99) {echo "NOT EMPLOYED. Your ER is 151.3 and Your EE is 66.7";}
        elseif ($a <2749.99) {echo "NOT EMPLOYED. Your ER is 186.7 and Your EE is 83.3";}
        elseif ($a <3249.99) {echo "NOT EMPLOYED. Your ER is 222 and Your EE is 100";}
        elseif ($a <3749.99) {echo "NOT EMPLOYED. Your ER is 257.3 and Your EE is 116.7";}
        elseif ($a <4249.99) {echo "NOT EMPLOYED. Your ER is 292.7 and Your EE is 133.3";}
        elseif ($a <4749.99) {echo "NOT EMPLOYED. Your ER is 328 and Your EE is 150";}
        elseif ($a <5249.99) {echo "NOT EMPLOYED. Your ER is 363.3 and Your EE is 166.7";}
        elseif ($a <5749.99) {echo "NOT EMPLOYED. Your ER is 398.7 and Your EE is 183.3";}
        elseif ($a <6249.99) {echo "NOT EMPLOYED. Your ER is 434 and Your EE is 200";}
        elseif ($a <6749.99) {echo "NOT EMPLOYED. Your ER is 469.3 and Your EE is 216.7";}
        elseif ($a <7249.99) {echo "NOT EMPLOYED. Your ER is 504.7 and Your EE is 233.3";}
        elseif ($a <7749.99) {echo "NOT EMPLOYED. Your ER is 540 and Your EE is 250";}
        elseif ($a <8249.99) {echo "NOT EMPLOYED. Your ER is 575.3 and Your EE is 266.7";}
        elseif ($a <8749.99) {echo "NOT EMPLOYED. Your ER is 610.7 and Your EE is 283.3";}
        elseif ($a <9249.99) {echo "NOT EMPLOYED. Your ER is 646 and Your EE is 300";}
        elseif ($a <9749.99) {echo "NOT EMPLOYED. Your ER is 681.3 and Your EE is 316.7";}
        elseif ($a <10249.99) {echo "NOT EMPLOYED. Your ER is 716.7 and Your EE is 333.3";}
        elseif ($a <10749.99) {echo "NOT EMPLOYED. Your ER is 752 and Your EE is 350";}
        elseif ($a <11249.99) {echo "NOT EMPLOYED. Your ER is 787.3 and Your EE is 366.7";}
        elseif ($a <11749.99) {echo "NOT EMPLOYED. Your ER is 822.7 and Your EE is 383.3";}
        elseif ($a <12249.99) {echo "NOT EMPLOYED. Your ER is 858 and Your EE is 400";}
        elseif ($a <12749.99) {echo "NOT EMPLOYED. Your ER is 893.3 and Your EE is 416.7";}
        elseif ($a <13249.99) {echo "NOT EMPLOYED. Your ER is 928.7 and Your EE is 433.3";}
        elseif ($a <13749.99) {echo "NOT EMPLOYED. Your ER is 964 and Your EE is 450";}
        elseif ($a <14249.99) {echo "NOT EMPLOYED. Your ER is 999.3 and Your EE is 466.7";}
        elseif ($a <14749.99) {echo "NOT EMPLOYED. Your ER is 1034.7 and Your EE is 483.3";}
        elseif ($a <30000) {echo "NOT EMPLOYED. Your ER is 1090 and Your EE is 500";}
        elseif ($a > 30000) {echo "You reached the maximum. Your ER is 1090 and Your EE is 500";}
}




if ( $_POST['membershiptype'] == 'employed' ){employed();
} elseif ( $_POST['membershiptype'] == 'SE' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'VM' ){sevmofw();
} elseif ( $_POST['membershiptype'] == 'OFW' ){sevmofw();
}


?>

实际上,您的代码还有更多的问题,包括混合业务和表示逻辑以及职责

但是,具体到这个问题

首先,应该使用switch而不是bunch of if/elseif

第二,

您使用的函数不知道$a是什么。你可以提供它作为一个论点

function employed($a){

   switch(true){

      case $a < 100:

        echo "Your Salary for the Period is $a. <br> You didn't reach the minimum compensation for the period. No SSS Contribution Deduction."   

      break;

      case $a < 1249.99:
         echo "Your ER is 80.7 and Your EE is 33.3 sevmofw";
      break;

      ......
   }
}

这将按预期工作。

事实上,您的代码还有更多的问题,包括混合业务和表示逻辑以及职责

但是,具体到这个问题

首先,应该使用switch而不是bunch of if/elseif

第二,

您使用的函数不知道$a是什么。你可以提供它作为一个论点

function employed($a){

   switch(true){

      case $a < 100:

        echo "Your Salary for the Period is $a. <br> You didn't reach the minimum compensation for the period. No SSS Contribution Deduction."   

      break;

      case $a < 1249.99:
         echo "Your ER is 80.7 and Your EE is 33.3 sevmofw";
      break;

      ......
   }
}
这将按预期工作。

您的$a和$b超出范围。您需要将它们作为参数传递给函数

您的$a和$b超出范围。您需要将它们作为参数传递给函数


你什么也没看到?查看源代码时会发生什么?$a不在函数的作用域中。将其作为参数提供给函数。需要了解变量范围-查看第二个框/示例,因为它专门解决了您的问题。@MadaraUchiha,我可以看到表单、下拉列表和提交按钮。但是它不执行这个函数。。“什么事也没发生。”瑞肯,对不起,我不明白。还是个新手。请讲英语你什么也没看到?查看源代码时会发生什么?$a不在函数的作用域中。将其作为参数提供给函数。需要了解变量范围-查看第二个框/示例,因为它专门解决了您的问题。@MadaraUchiha,我可以看到表单、下拉列表和提交按钮。但是它不执行这个函数。。“什么事也没发生。”瑞肯,对不起,我不明白。还是个新手。请讲英语我在C:\wamp\www\php\u sandbox\switch.php的第120、123、126和129行发现了这个错误:cream:error suppression忽略了未定义的索引:membershiptype。实际上,它已经工作了。问题是当我打开php时,它会显示错误。但是当我开始输入一些数字时,它会返回正确的输出。@KareenLagasca我想您想用$b替换if/elseif块中的所有$_POST['membershiptype'],因为您之前将其设置为$b=isset$_POST['membershiptype']$_POST['membershiptype']:;。然后在页面加载时,它不会给你未定义的索引警告,就像你之前用isset检查的那样。我得到了这个错误:尖叫:未定义索引的错误抑制被忽略:C:\wamp\www\php\u sandbox\switch.php中的membershiptype在第120、123、126和129行实际上,它已经工作了。问题是当我打开php时,它会显示错误。但是当我开始输入一些数字时,它会返回正确的输出。@KareenLagasca我想您想用$b替换if/elseif块中的所有$_POST['membershiptype'],因为您之前将其设置为$b=isset$_POST['membershiptype']$_POST['membershiptype']:;。然后在页面加载时,它不会给您未定义的索引警告,因为您以前使用isset检查过它。