Viewed   135 times

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

$cart = array();

Do I simply add elements to it using the following?

$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;

Don't arrays in PHP have an add method, for example, cart.add(13)?



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();
    $cart[] = $i;  
echo "<pre>";
echo "</pre>";

Is the same as:

$cart = array();
array_push($cart, 13);
array_push($cart, 14);

// Or 
$cart = array();
array_push($cart, 13, 14);
Friday, October 14, 2022

Just a try :)

Test 1

With 5 variables

$myvar1 = 'hello';
$myvar2 = 'hello';
$myvar3 = 'hello';
$myvar4 = 'hello';
$myvar4 = 'hello';


Resut : 618600

Test 2

with 5 array keys

$myvar = array();
$myvar['var1'] = 'hello';
$myvar['var2'] = 'hello';
$myvar['var3'] = 'hello';
$myvar['var4'] = 'hello';
$myvar['var5'] = 'hello';


Resut : 620256

Tuesday, August 16, 2022

The same way you add to an array when the key is not a concern:

$data[$state]['cities'][] = $city;
Tuesday, November 29, 2022

In your foreach loop, call $img->setAttribute('class', 'someclass');. This should do the trick. See more at

Then you need to save the modified document back using $article_header = $doc->saveXml();.

Sunday, October 23, 2022

I don't think you should consider this from a performance standpoint, rather, look at it from a readability standpoint. The second version collects, well, a collection of things, into a single storage mechanism; from a readability standpoint it's superior.

Not that it matters, but from a performance standpoint I can't imagine the second one costs much, if anything, certainly not enough to overcome the readability benefit.

Friday, August 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :