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.  
  3. $valuta = floatval($argv[1]);
  4. $rest = 0;
  5.  
  6. if (!$valuta) {
  7.     exit("Geen wisselgeld");
  8. }
  9.  
  10. define("GELDEENHEDEN", array(50, 20, 10, 5, 2, 1));  ( ) hoort [ ] te zijn!
  11. $restbedrag = $valuta;
  12.  
  13. foreach (GELDEENHEDEN as $valuta) {
  14.     if ($restbedrag >= $valuta) {
  15.         $aantal = floor($restbedrag / $valuta);
  16.         $restbedrag = $restbedrag % $valuta;
  17.         echo "$aantal x $valuta euro" . PHP_EOL;
  18.     }
  19. }
  20.  
  21. $centen = $restbedrag;
  22.   
  23. foreach (GELDEENHEDEN as $cent) {
  24.     if ($centen >= $cent / 100) {
  25.         $aantal = floor($centen / ($cent / 100));
  26.         $centen = $centen % $cent; <-- % veranderd float in INT --> gebruik fmod
  27.         $centen = round(fmod($centen($cent / 100)));
  28.         echo "$aantal x $cent cent" . PHP_EOL;
  29.     }
  30. }
  31.  
  32. ?>
  33.  
  34.  
File Description
  • wisselgeld
  • PHP Code
  • 13 Oct-2021
  • 759 Bytes
You can Share it: