我必须创建一个学费计算器,第一部分是使用PHP创建一个函数并包含一个文件,该文件将允许您基于从下拉列表中选择的内容返回一个值。
共有3个选择:
下拉列表位于单独的页面上,我将使用包含文件与之连接。
<?php
function CalcResidency($argResidency){
$residency = "";
if ($residency == "In County") {
return 1500;
}
else if ($residency == "Out County") {
return 2000;
}
else if ($residency == "Out of State") {
return 2500;
}
}
?>
答案 0 :(得分:1)
如果您希望根据下拉值返回三个整数输出,则可以尝试:
/**
* This function returns an integer
* @var residency is: "In County", "Out County", "Out of State"
*/
function CalcResidency($residency)
{
$tuition = 0;
if ($residency == "In County") {
$tuition = 1500;
} else if ($residency == "Out County") {
$tuition = 2000;
} else if ($residency == "Out of State") {
$tuition = 2500;
} else {
echo "Something is not right!";
}
var_dump($tuition);
return $tuition;
}
CalcResidency("In County");
CalcResidency("Out County");
CalcResidency("Out of State");
int(1500)
int(2000)
int(2500)
但是,如果您愿意,this link可能是设计面向对象的学费计算器的一个很好的开始,这可以节省您的时间。也许类似于:
<?php
TutionCalculator::Calculator();
class TutionCalculator
{
const STUDENT = [
'type' => 'undergrad',
'major' => 'comp sci',
'level' => 'fresh',
'in_county_credit_price' => 1500,
'out_county_credit_price' => 2000,
'out_state_credit_price' => 2500,
'residency_status' => 1, // 0 1 2 for in_state, in_county, out_county
'scholarship' => 0,
'scholarship_amount' => 0,
'credit' => 130,
'semesters' => 8,
'summer' => 0,
];
public static function Calculator()
{
$tuition = self::CalcResidency();
// rest of calculations
// ...
// ...
// ...
var_dump($tuition);
return $tuition;
}
public static function CalcResidency()
{
$tuition = 0;
var_dump(self::STUDENT);
switch (self::STUDENT["residency_status"]) {
case 2:
$tuition = self::STUDENT["in_state_credit_price"];
break;
case 1:
$tuition = self::STUDENT["out_county_credit_price"];
break;
default:
$tuition = self::STUDENT["in_county_credit_price"];
break;
}
return $tuition;
}
}
array(12) {
["type"]=>
string(9) "undergrad"
["major"]=>
string(8) "comp sci"
["level"]=>
string(5) "fresh"
["in_county_credit_price"]=>
int(1500)
["out_county_credit_price"]=>
int(2000)
["out_state_credit_price"]=>
int(2500)
["residency_status"]=>
int(1)
["scholarship"]=>
int(0)
["scholarship_amount"]=>
int(0)
["credit"]=>
int(130)
["semesters"]=>
int(8)
["summer"]=>
int(0)
}
int(2000)
答案 1 :(得分:1)
感谢@Emma的方法,我进行了一些调整,以使其“更多”面向对象操作。
现在我们有一个班级学生,它拥有$residency_status
(作为整数)。
class Student {
private $residency_status = 0; // 0 1 2 for in_state, in_county, out_county
public function __construct($residency_status) {
$this->residency_status = $residency_status;
}
public function getResidencyStatus() {
return $this->residency_status;
}
}
此外,我们还有一个TuitionCalculator
的类。
class TuitionCalculator {
private static $prices = [ // 0 1 2 for in_state, out_county, out_county
'in_county_credit_price' => 1500,
'out_county_credit_price' => 2000,
'out_state_credit_price' => 2500,
];
public static function calcResidency($student) {
$tuition = 0;
switch ($student->getResidencyStatus()) {
case 2:
$tuition = self::$prices["out_state_credit_price"];
break;
case 1:
$tuition = self::$prices["out_county_credit_price"];
break;
default:
$tuition = self::$prices["in_county_credit_price"];
break;
}
return $tuition;
}
}
要使用此结构,只需从下拉提交中创建一个Student
。
$student1 = new Student(0);
$student2 = new Student(1); // ...
echo TuitionCalculator::calcResidency($student1); // ...
如果将$prices
数组更改为数字数组,则可以使计算更简单,更灵活,因为您不再需要switch
。
private static $prices = [ // index 0 for in_state etc.
1500,
2000,
2500,
];
public static function calcResidency($student) {
$residency_status = $student->getResidencyStatus();
if(array_key_exists($residency_status, self::$prices)) {
return self::$prices[$residency_status];
} else {
return 0; // or self::$prices[0] or your default value
}
}