wisselgeld - 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 wisselgeld.php

  1. <?php
  2. define("GELDEENHEDEN", [50, 20, 10, 5, 2, 1]);
  3.  
  4. if (count($argv) != 2) {
  5.     exit("Geen wisselgeld" . PHP_EOL);
  6. } else {
  7.     $rest = float($argv[1]);
  8. }
  9.  
  10. if ($rest == 0) {
  11.     exit("Geen wisselgeld" . PHP_EOL);
  12. } else {
  13.     foreach (GELDEENHEDEN as $valuta) {
  14.         if ($rest >= $valuta) {
  15.             $aantal_valuta = floor($rest / $valuta);
  16.             $rest = fmod($rest, $valuta);
  17.             echo $aantal_valuta . " x " . $valuta . " euro" . PHP_EOL;
  18.             echo "restant = " . $rest . PHP_EOL;
  19.         }
  20.     }
  21. }
  22. echo "restant = " . $rest . PHP_EOL;
  23. $rest = $rest * 100;
  24. if ($rest >= 1) {
  25.     foreach (GELDEENHEDEN as $centen) {
  26.         $aantal_centen = floor($rest / $centen);
  27.         $rest = fmod($rest, $centen);
  28.         $centen = round($centen);
  29.         if ($aantal_centen >= 0) {
  30.             echo $aantal_centen . " x " . $centen . " cent" . PHP_EOL;
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36.  
File Description
  • wisselgeld
  • PHP Code
  • 13 Oct-2021
  • 893 Bytes
You can Share it: