sample code - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of sample code.php

  1. <?php
  2.  
  3.  function solve(array $problem) {
  4.     $length = $problem['number'];
  5.     $datas = $problem['datas'];
  6.     $cost = 0;
  7.     for ($i=0; $i < $length-1; $i++) {
  8.         echo "i=$i;";
  9.         $k = $j = i;
  10.         while ($k < $length) {
  11.             echo "j=$j;";
  12.             if ($datas[$k] > $datas[$i]) {
  13.                 $j = $k;
  14.             }
  15.             $j++;
  16.         }
  17.         $iteration_cost = (($j - $i) + 1);
  18.         $cost += $iteration_cost;
  19.         for ($k = $i; $k < ($i + $iteration_cost/2); $k++) {
  20.             echo "k=$k;";
  21.             $ech = $datas[$k];
  22.             $datas[$k] = $datas[($j - ($k - $i))];
  23.             $datas[($j - ($k - $i))] = $ech;
  24.         }
  25.         exit();
  26.     }
  27.     return $cost;
  28.  }
  29.  echo solve(['number' => 4, 'datas' => [4,2,1,3]]);
File Description
  • sample code
  • PHP Code
  • 28 Mar-2021
  • 766 Bytes
You can Share it: