1

好的,我已经编写了这种 pythagoras-solver 之类的代码,我想知道有什么方法可以改进它,或者让它更有效率吗?

<?php
    $sides = array('1' => 'Hypotenuse',
        '2' => 'Adjacent',
        '3' => 'Opposite');

    function createSideDropdown($name) {
        global $sides;
        $option = "<select name='".$name."'>";
            if(!empty($sides)) {
                foreach($sides as $id => $sideDesc) {
                    $option .= "<option value='".$id."'>".$sideDesc."</option>";
                }
            } else {
                die("Error fetching sides!");
            }
        $option .= "</select>";
        echo $option;
    }

    try {
        if(!empty($_POST['submit'])) {
            if(empty($_POST['val1']) || empty($_POST['val2'])) {
                throw new Exception("Please enter an integer in both boxes.");
            }
            if(!is_numeric($_POST['val1']) || !is_numeric($_POST['val2'])) {
                throw new Exception("One of the numbers you entered is not a valid integer.");
            }

            $val1 = $_POST['val1'];
            $val2 = $_POST['val2'];
            $val1numtype = $_POST['val1type'];
            $val2numtype = $_POST['val2type'];
            $val1nametype = $sides[$val1numtype];
            $val2nametype = $sides[$val2numtype];

                if($val1numtype == $val2numtype) {
                    throw new Exception("The two sides of the triangle must be different");
                }

                if($val1nametype == "Hypotenuse" || $val2nametype == "hypotenuse") {
                    // work out a small side
                    $bignum = max($val1, $val2);
                    $smallnum = min($val1, $val2);
                    $sqTotal = ($bignum * $bignum) - ($smallnum * $smallnum);
                    $total = sqrt($sqTotal);
                    echo $bignum."&sup2; - ".$smallnum."&sup2; = ".$sqTotal."<br />
                    &radic;".$sqTotal." = ".$total.$_POST['mes'];
                } else {
                    // work out the hypotenuse
                    $sq1 = $val1 * $val1;
                    $sq2 = $val2 * $val2;
                    $sqTotal = $sq1 + $sq2;
                    $total = sqrt($sqTotal);
                    echo $val1."&sup2; + ".$val2."&sup2; = ".$sqTotal."<br />
                    &radic;".$sqTotal." = ".$total.$_POST['mes'];
                }       
            echo "<br /><br />"; // Seperate the working out from the input
        }
    } catch(Exception $e) {
        echo $e->getMessage()."<br/><br/>";
    }


?>
<form method='POST' action='index.php'>
Value 1: <input type='text' name='val1' /> 
<?php createSideDropdown("val1type"); ?>
<br /><br />

Value 2: <input type='text' name='val2' /> 
<?php createSideDropdown("val2type"); ?>
<br />
<select name="mes">
<option name="mm">mm</option>
<option name="cm">cm</option>
<option name="m">m</option>
<option name="cm">km</option>
</select>
<br /> 

<input type="submit" name="submit" />
</form>
?>
4

1 回答 1

1

好吧,您当然可以做的一件事是:

单独的 HTML 和单独的 PHP - 将其分开。然后,我将继续查看其余部分。

此外,您可以使用 JavaScript 处理异常——我的意思是,使用 JavaScript 解析文本字段并使用 JavaScript 编写错误。而不是总是提交表单。- 你仍然应该解析 PHP 中的字段。

然后,从中创建一个类并对其进行适当的记录,例如

/**
 * This method does bla
 * @param Int a
 */

不要使用全局变量 - 可以使用类属性来完成。

于 2011-04-30T23:46:53.330 回答