如何简化循环?

时间:2016-02-25 20:11:01

标签: php

这就是我的php循环:

<?php
$VarC = 6;
$VarD = 2;
$varA = 1;
$varB = 2;
$VarAns = 0;
$ct = 1;
$ct2 = 1;

$VarAns = $varA * ($varB + $VarC) / $VarD;

while ($ct <= 10) {
    $VarAns = $varA * ($varB + $VarC) / $VarD;
    echo "X = $varA * ($varB + 6) / 2 is equal to: $VarAns<br>";
    $varA = $varA + 1;
    ++$ct;
}

echo '<br>';

$varB = $varB + 2;
$varA = 1;

while ($ct2 <= 10) {
    $VarAns = $varA * ($varB + $VarC) / $VarD;
    echo "X = $varA * ($varB + 6) / 2 is equal to: $VarAns<br>";
    $varA = $varA + 1;
    $ct2 = $ct2 + 1;
}

echo '<br>';

$varB = $varB + 2;
$varA = 1;
$ct2 = 1;

while ($ct2 <= 10) {
    $VarAns = $varA * ($varB + $VarC) / $VarD;
    echo "X = $varA * ($varB + 6) / 2 is equal to: $VarAns<br>";
    $varA = $varA + 1;
    $ct2 = $ct2 + 1;
}

echo '<br>';

$varB = $varB + 2;
$varA = 1;
$ct2 = 1;

while ($ct2 <= 10) {
    $VarAns = $varA * ($varB + $VarC) / $VarD;
    echo "X = $varA * ($varB + 6) / 2 is equal to: $VarAns<br>";
    $varA = $varA + 1;
    $ct2 = $ct2 + 1;
}

echo '<br>';

$varB = $varB + 2;
$varA = 1;
$ct2 = 1;

while ($ct2 <= 10) {
    $VarAns = $varA * ($varB + $VarC) / $VarD;
    echo "X = $varA * ($varB + 6) / 2 is equal to: $VarAns<br>";
    $varA = $varA + 1;
    $ct2 = $ct2 + 1;
}

?>

我要做的是将$varA增加1,直到达到10,'$ varB'从2开始,增加2到10 ...... 任何想法,我知道应该有一种简化事情的方法,我只是想不出来。

1 个答案:

答案 0 :(得分:0)

一个包含5个值的循环存储结果:

insert into table (stage_nm, p_name) 
values('The Cage', 'Nicolas Cage');