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.
Result of php executing
Full code of wisselgeld.php
- <?php
- define("GELDEENHEDEN", [50, 20, 10, 5, 2, 1]);
- if (count($argv) != 2) {
- exit("Geen wisselgeld" . PHP_EOL);
- } else {
- $rest = float($argv[1]);
- }
- if ($rest == 0) {
- exit("Geen wisselgeld" . PHP_EOL);
- } else {
- foreach (GELDEENHEDEN as $valuta) {
- if ($rest >= $valuta) {
- $aantal_valuta = floor($rest / $valuta);
- $rest = fmod($rest, $valuta);
- echo $aantal_valuta . " x " . $valuta . " euro" . PHP_EOL;
- echo "restant = " . $rest . PHP_EOL;
- }
- }
- }
- echo "restant = " . $rest . PHP_EOL;
- $rest = $rest * 100;
- if ($rest >= 1) {
- foreach (GELDEENHEDEN as $centen) {
- $aantal_centen = floor($rest / $centen);
- $rest = fmod($rest, $centen);
- $centen = round($centen);
- if ($aantal_centen >= 0) {
- echo $aantal_centen . " x " . $centen . " cent" . PHP_EOL;
- }
- }
- }