How to add elements to an empty array in PHP?

后端 未结 8 583
天命终不由人
天命终不由人 2020-11-22 12:07

If I define an array in PHP such as (I don\'t define its size):

$cart = array();

Do I simply add el

8条回答
  •  渐次进展
    2020-11-22 12:22

    Both array_push and the method you described will work.

    $cart = array();
    $cart[] = 13;
    $cart[] = 14;
    // etc
    
    //Above is correct. but below one is for further understanding
    $cart = array();
    for($i=0;$i<=5;$i++){
        $cart[] = $i;  
    }
    echo "
    ";
    print_r($cart);
    echo "
    ";

    Is the same as:

    
    

提交回复
热议问题