Venciity

Sum of digits

Aug 20th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <!doctype html>
  2. <?php
  3.     if (isset($_POST['input'])) {
  4.         $input = $_POST['input'];
  5.         $strings =  explode("," , $input);
  6.         $resultTable = "";
  7.         foreach ($strings as $value) {
  8.             if (is_numeric($value)) {
  9.                 $sumOfDigits = array_sum(str_split($value));
  10.                 $resultTable.= "<tr><td>$value</td><td>$sumOfDigits</td></tr>";
  11.             }
  12.             else {
  13.                 $sumOfDigits = "I cannot sum that";
  14.                 $resultTable.= "<tr><td>$value</td><td>$sumOfDigits</td></tr>";
  15.             }
  16.         }
  17.  
  18.     }
  19. ?>
  20. <html lang="en">
  21. <head>
  22.     <meta charset="UTF-8">
  23.     <title>Sum of digits</title>
  24. </head>
  25. <body>
  26.     <form action="" method="post">
  27.         <label for="input">Input String: </label>
  28.         <input type="text" id="input" name="input"/>
  29.         <input type="submit" value="Submit"/>
  30.     </form>
  31.     <table border="1px">
  32.         <?php
  33.         if(isset($resultTable)) {
  34.             echo $resultTable;
  35.         }
  36.         ?>
  37.     </table>
  38. </body>
  39. </html>
Advertisement
Add Comment
Please, Sign In to add comment