2016-02-09 32 views
0

我想給數組添加偶數,然後將其回顯出來,這裏是我的代碼,但是當我打印時它只顯示array()...我做錯了什麼?PHP中的基本數組

<?php 
    $x =88; 
    $numbers = array(); 
    while ($x % 2 == 0 && $x <= 99) { 
     $numbers[] = "$x"; 
     $x++; 
    } 
    print_r($numbers); 
?> 
+2

'$ numbers = range(88,99,2);' –

回答

3

您應該從while循環移動「均勻度」的考驗,並在while環路內移動到一個條件:

<?php 
    $x = 88; 
    $numbers = array(); 
    while ($x <= 99) { 
     if ($x % 2 == 0) { 
      $numbers[] = $x; 
     } 
     $x++; 
    } 
    print_r($numbers); 
?> 

由於當前已寫入的while循環,它結束如果這個數字不是偶數。添加到$numbers陣列時,還應刪除$x中的引號。