duanhui1185 2019-07-04 14:28
浏览 53

使用临时变量在php中排序没有数组

I'm a beginner and our prof told us that we need to explain what's the purpose of this code. I know that it is for sorting but I want to explain it briefly. Can someone help me pls?

<?php
function descending(){
    $num1 = $_GET['num1'];
    $num2 = $_GET['num2'];
    $num3 = $_GET['num3'];

    if ($num1 < $num2) {
        $temp = $num1;
        $num1 = $num2;
        $num2 = $temp;
    }
    if ($num1 < $num3) {
       $temp = $num1;
       $num1 = $num3;
       $num3 = $temp;
    }
    if ($num2 < $num3) {
       $temp = $num2;
       $num2 = $num3;
       $num3 = $temp;
    }
    echo "<br>";
    echo $num1a . " , " . $num2a . " , " . $num3a;
    echo "<br>";

?>
  • 写回答

1条回答 默认 最新

  • dpg78570 2019-07-04 19:26
    关注

    The concept of descending 3 number is by compairing first number with other two numbers and first number should be greater than other so we compare first number with second number if second number greater than swap to first number same goes for first number with third number after this process the first number will be greater than two. Remaining two variable is checked and again compare assign greater number in second variable.This way it is descending.

    <?php 
    function descending(){
        $num1 = $_GET['num1'];    // Let suppose $num1 = 1
        $num2 = $_GET['num2'];    // Let suppose $num2 = 2
        $num3 = $_GET['num3'];    // Let suppose $num3 = 3
    
    
        if ($num1 < $num2) {     // here $num1 (1) is less than $num2(2)
            $temp = $num1;       // $temp(2) 
            $num1 = $num2;       // copy $temp(2) to $num1(2)
            $num2 = $temp;       // this process swap $num1(2) and $num(1)
        }
        if ($num1 < $num3) {     // here $num1(2) and $num3(3) which is less so swap
            $temp = $num1;
            $num1 = $num3;
            $num3 = $temp;       //After swap $num1(3) & $num3(2)      
    
        }
    
        //now at this time $num1 greatest among other two variable
    
        if ($num2 < $num3) {   //$num2(1) and $num3(2) so swap
            $temp = $num2;
            $num2 = $num3;
            $num3 = $temp;     // this way $num2 will be greater
        }
    
        echo "<br>";
        //first error: After descending value store in $num1, $num2, and $num3
        //not in $num1a, $num2a, and $num3a these variable.
        echo $num1 . " , " . $num2 . " , " . $num3; 
        echo "<br>";
     }  // second error missing this curly bracket.
    ?>
    

    Hope you understand and ignore my bad english. Happy Coding :)

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line