因此,我正在学习PHP,因此,正在尝试创建一个简化分数的程序(至少可以说是一个非常初学者的代码)。它使用HTML表单,从中拉出分子和分母。但是,在表单提交时,函数会吐出输入的数字。我的想法是,它正在超越'因为'在每个函数中循环。任何帮助将不胜感激。
<html>
<head>
<!-- CSS for echoed data -->
<style>
p {
text-size:12px;
text-color:black;
}
</style>
</head>
<body>
<!-- Defines Form -->
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
Numerator: <br />
<input type = "text" name = "numOne"><br />
Denominator: <br />
<input type = "text" name = "numTwo"><br />
<input type = "submit" name = "Submit" value = "Reduce!">
</form>
<?php
//Executes task upon form submission
if (isset($_POST['Submit'])) {
$n1 = $_POST['numOne'];
$n2 = $_POST['numTwo'];
//Defines numerator simplification function
function simplifynum ($n1,$n2) {
$n1 = $_POST['numOne'];
$n2 = $_POST['numTwo'];
if ($n1 < $n2){
//Loops until numerator and denominator are reduced to simplest form
for ($x = 2;$x < $n2;$x++){
$divedn1 = ($n1 / $x);
$divedn2 = ($n2 / $x);
//If all divided values are purely digits (i.e. are whole numbers) and not equal to zero, sets new numerator and denominators variables to the divided value
if (ctype_digit($divedn1) && ctype_digit($divedn2) && ($divedn1 != 0) && ($divedn2 != 0)) {
$n1 = $divedn1;
$n2 = $divedn2;
}
}
//returns numerator data
return $n1;
}
}
//Defines denominator simplification function
function simplifyden ($n1,$n2) {
$n1 = $_POST['numOne'];
$n2 = $_POST['numTwo'];
if ($n1 < $n2){
//Loops until numerator and denominator are reduced to simplest form
for ($x = 2;$x < $n2;$x++){
$divedn1 = ($n1 / $x);
$divedn2 = ($n2 / $x);
//If all divided values are purely digits (i.e. are whole numbers) and not equal to zero, sets new numerator and denominators variables to the divided value
if ( ctype_digit($divedn1) && ctype_digit($divedn2) && ($divedn1 != 0) && ($divedn2 != 0)) {
$n1 = $divedn1;
$n2 = $divedn2;
}
}
//returns denominator data
return $n2;
}
}
?>
<!-- Runs functions and reads respective values -->
<p><?php echo simplifynum($n1, $n2);?><br /> ━<br /><?php echo simplifyden($n1, $n2);?></p>
<?php
//Unsets form submission
unset($_POST['Submit']);
}
?>
</body>
答案 0 :(得分:0)
如果问题是简化分数,因为规则是两个编号的分量和分母必须可以被相同的数字整除才能正确,你不需要两个函数,并且当你是每个函数时也需要通过post方法收集已经发送了诸如参数值之类的函数。
试试这个
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
Numerator: <br />
<input type = "text" name = "numOne"><br />
Denominator: <br />
<input type = "text" name = "numTwo"><br />
<input type = "submit" name = "Submit" value = "Reduce!">
</form>
<?php
if (isset($_POST['Submit'])) {
$n1 = $_POST['numOne'];
$n2 = $_POST['numTwo'];
simplifynum($n1, $n2);//call the function
}
function simplifynum ($n1,$n2) {
$menor = 0;
if ($n1 < $n2){ $menor= $n1;}
else {$menor=$n2;}//I check less variable to find the maximum number of digit in the divisor for and not have decimal results
for ($x = 2;$x < $menor;){ //Start the divisor digit two
if($n1 % $x == 0 && $n2 % $x==0) //Seeking the digit that is divisible for two numbers by modulo operation% to return the waste if zero is because it is divisible
{
$n1 = $n1/$x; //I check with the first number
$n2 = $n2/$x; //I check the second number
}
else
$x= $x+1;//if the two numbers not divisible increase as the variable in this case would be 3, the first iteration
}
echo $n1 . " /" . $n2 . "<br/>";
echo "Result : " . ($n1/$n2);
}
?>