Count to 100 with PHP

Asked

Viewed 639 times

0

I need to complete the array $resultado using the parameter $numero up to 100.

For example, if $numero were 45, $resultado should have the numbers from 45 up to 100, the number 100 should also be included.

<?php

function conta100 ($base) {
    $resultado = [];
    $numero = 45;

    while($numero<=100){
        echo $numero;
        $numero++;
    }

    return $resultado;
}

3 answers

3


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

1

I find it much more simple to use a for note the example:

<?php

$numero = 45;
$resultado = [];

for($i = ($numero+1);$i <= 100;$i++){
$resultado[] += $i;
}

print_r($resultado);

not to mention that you do not need to spend resource calling a function.

using the while:

//public function teste($numeroMaximo, $numeroInicial){
$numeroMaximo = 100;
$numeroInicial = 45;
$numeroInicial++;
$resultado = [];
while($numeroInicial<=$numeroMaximo){
 $resultado[] += $numeroInicial++;
}
return $resultado
}
  • 1

    It worked too, but exercise asks it to be with while. Thanks for the tip

  • note that I added the same function but using while.

  • @Mayaramendes you have to put in question that condition that exercise can only be done with while.

  • @Marcosvinicius sorry for the mistake, but thanks for the tip ;) It worked !

1

Notice: Does not specifically resolve the exercise as the questioner clarified later in the comments that the same should be done using loop while.

But the simplest way to create an array containing a continuous or bounced numeric range is by using the function crease() which returns a numeric array starting with the start value up to the end value, including.

print_r( range(45, 100) );

Resulting:

Array
(
    [0] => 45
    [1] => 46
    [2] => 47
    [3] => 48
    [4] => 49
    [5] => 50
    [6] => 51
    [7] => 52
    [8] => 53
    [9] => 54
    [10] => 55
    [11] => 56
    [12] => 57
    [13] => 58
    [14] => 59
    [15] => 60
    [16] => 61
    [17] => 62
    [18] => 63
    [19] => 64
    [20] => 65
    [21] => 66
    [22] => 67
    [23] => 68
    [24] => 69
    [25] => 70
    [26] => 71
    [27] => 72
    [28] => 73
    [29] => 74
    [30] => 75
    [31] => 76
    [32] => 77
    [33] => 78
    [34] => 79
    [35] => 80
    [36] => 81
    [37] => 82
    [38] => 83
    [39] => 84
    [40] => 85
    [41] => 86
    [42] => 87
    [43] => 88
    [44] => 89
    [45] => 90
    [46] => 91
    [47] => 92
    [48] => 93
    [49] => 94
    [50] => 95
    [51] => 96
    [52] => 97
    [53] => 98
    [54] => 99
    [55] => 100
)

Test the example in Repl.it: https://repl.it/repls/PettyMessyArchitects

Browser other questions tagged

You are not signed in. Login or sign up in order to post.