Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--Write a PHP script SumOfDigits.php which receives a comma-separated list of integers from an input form and creates a two-column table. The first column should contain each of the values from the input. The second column should contain the sum of the digits of each value. If the value is not an integer number, print "I cannot sum that". Styling the page is optional.-->
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8"/>
- <title>Sum Of Digits</title>
- <style type="text/css">
- table, td {
- border: 1px solid #000;
- }
- </style>
- </head>
- <body>
- <form method="post">
- <p>
- <label for="input-text">Input string:</label>
- <input type="text" name="input_text" id="input-text" required/>
- <input type="submit" value="Submit"/>
- </p>
- </form>
- <table>
- <tbody>
- <?php
- if (!array_key_exists('input_text', $_POST)) {
- die('The Input Form can\'t been Empty!!!');
- }
- $numbers = preg_split("/[,\s]+/", $_POST['input_text'], -1, PREG_SPLIT_NO_EMPTY);
- foreach ($numbers as $el) {
- ?>
- <tr>
- <td><?= htmlspecialchars($el) ?></td>
- <td>
- <?php
- $result = 'I cannot sum that';
- if ((intval($el) !== 0 || $el === '0') && (string)((int)$el) === $el) {
- $count = strlen($el);
- $result = 0;
- for ($i = 0; $i < $count; $i++) {
- $result += (int)$el[$i];
- }
- }
- echo $result;
- ?>
- </td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement