Your while
is correct, just then add the variable $numero
in the array, for example you can use the function array_push
:
array_push($resultado, $numero);
Or use bracketed syntax:
$resultado[] = $numero;
We also change the received parameter to $numero
and remove the line that forces the start always at 45:
function conta100($numero) {
Your code will then be more or less as follows:
<?php
function conta100($numero ) {
$resultado = [];
while($numero<=100){
//Utilizando array_push
array_push($resultado, $numero);
$numero++;
}
return $resultado;
}
var_dump(conta100(45));
?>
See online: https://repl.it/repls/PoorMadeupCertifications
As you know where your loop starts and ends, an alternative is to use the loop for
:
<?php
function conta100($numero) {
$resultado = [];
for($i = $numero; $i <= 100; $i++) {
//Utilizando colchetes
$resultado[] = $i;
}
return $resultado;
}
var_dump(conta100(75));
?>
See online: https://repl.it/repls/PastelAdeptDriver
Documentations:
https://www.php.net/manual/en/function.array-push.php
https://www.php.net/manual/en/control-structures.while.php
https://www.php.net/manual/en/control-structures.for.php
Worked super well!
– Mayara Mendes