0

I'm trying to make this code in php. It should receive an input value and print the sum of all numbers smaller than the value, that are divisible by 3 or 5. For example, if value = 10, then 3, 5, 6 and 9 are smaller than 10 and divisible by 3 or 5, so it should print 3 + 5 + 6 + 9 = 23. But when I run "php main.php val=10" I get no outputs

<?php
    // Crio um filtro para a requisição $_GET['val'];
    $GetData = filter_input(INPUT_GET, 'val', FILTER_VALIDATE_INT);
    // Verifico se algo foi passado no $_GET['val']
    if($GetData):
        // Exibo a samo dos valores
        echo sumAll($GetData);
    endif;



    // Função responsável por calcular a soma de todos os números múltiplos por 5 e 3 menores que $val
    // @return integer
    function sumAll($val) {
        // Valor inicial da soma
        $result = 0;

        // Verifico se o valor é inteiro
        if(!is_integer($val)):
            return "{$val} não é um número inteiro";
        endif;

        // Faço um loop até o valor informado
        for($i = 0; $i < $val; $i++):
            // Verifico se o valor é dividendo por 3 ou 5
            if(!($i % 3) || !($i % 5)):
                // Caso o valor seja dividido acrescendo o valor atual do loop
                $result += $i;
            endif;
        endfor;

        // Retorno o resultado da soma
        return $result;
    }
?>
  • What have you done so far to debug this? Have you confirmed that `$GetData` is what you're expecting? Are you sure `sumAll()` is being called? – WOUNDEDStevenJones Aug 15 '22 at 15:26
  • 1
    The command line argument would not be in `$_GET`, that's for web requests. Command line would be [argv](https://www.php.net/manual/en/reserved.variables.argv.php) – aynber Aug 15 '22 at 15:28

0 Answers0