简体   繁体   English

PHP 时间表创建

[英]PHP Times Table creation

Hi I'm trying to figure out some code in PHP, I've been given the task to make a basic times table input, I have that part figured out.嗨,我正在尝试找出 PHP 中的一些代码,我被赋予了制作基本时间表输入的任务,我已经弄清楚了那部分。 I have also to reverse the order the table is being output.我还必须颠倒表格的顺序是 output。 So instead of the example running from 1 x 10=10 down I have to reverse that order from 10 x 10=100 down instead.因此,不是从 1 x 10=10 向下运行的示例,我必须从 10 x 10=100 向下反转该顺序。 This part I can't figure out.这部分我想不通。 output output

if (isset($_POST['calculate'])) {

$start = $_POST['startNumber'];
$end = $_POST['endNumber'];
$times = $_POST['times'];

for($start; $start <= $end; $start++) {

    $answer = $start * $times;
    echo $start, " x ", $times, " = ", $answer, "<BR>";
    }
}

You can just push to an array instead, reverse the array after the loop, join the array into a string, then echo the output.您可以改为推送到数组,在循环后反转数组,将数组加入字符串,然后回显 output。

Also be careful with user submitting input.还要小心用户提交输入。 In this case I believe you're expecting ints so I typecasted them for you.在这种情况下,我相信您期待整数,因此我为您进行了类型转换。

if (isset($_POST['calculate'])) {

$start = (int)$_POST['startNumber'];
$end = (int)$_POST['endNumber'];
$times = (int)$_POST['times'];

$out = [];
for(; $start <= $end; $start++) {

    $answer = $start * $times;
    $out[] =  "$start x $times = $answer<br>";
    }
}
$out = array_reverse($out);
echo implode('',$out);

Alternatively, you can just walk the loop in reverse:或者,您可以反向走循环:

if (isset($_POST['calculate'])) {

$start = (int)$_POST['startNumber'];
$end = (int)$_POST['endNumber'];
$times = (int)$_POST['times'];

for(; $end >= $start; $end--) {

    $answer = $end * $times;
    echo "$end x $times = $answer<br>";
    }
}

The easiest way I can think of is to create an array with range and then reverse it using array_reverse .我能想到的最简单的方法是创建一个具有range的数组,然后使用array_reverse反转它。 With that, you can simply iterate over the numbers and do your calculations.有了它,您可以简单地迭代数字并进行计算。

Demo, http://sandbox.onlinephpfunctions.com/code/98ab6864cffb764325ea6b4a2f2f74178e13c62b演示, http://sandbox.onlinephpfunctions.com/code/98ab6864cffb764325ea6b4a2f2f74178e13c62b

<?php

$start = 1;
$end = 10;
$multiplier = 10;

$reversed = array_reverse(range($start, $end));

foreach ($reversed as $input) {
    echo sprintf('%s x %s = %s %s', $input, $multiplier, $input * $multiplier, "\n");
}

You could tweak your original code, but rather than output it straight away add it to the current output at the start.您可以调整您的原始代码,但不是 output 而是直接将其添加到当前的 output 开始。 The output the result at the end output 结果在最后

if (isset($_POST['calculate'])) {

    $start = $_POST['startNumber'];
    $end = $_POST['endNumber'];
    $times = $_POST['times'];

    $output = '';
    for($start; $start <= $end; $start++) {
        $answer = $start * $times;
        $output =  $start . " x " . $times ." = " . $answer . "<BR>" . $output;
    }
    
    echo $output;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM