differenza tra $array[]='valore' o array_push($array,'valore')

emanuelevt

Utente Attivo
24 Giu 2009
298
0
0
Codice:
$array=array('emanuele','samantha','giovanni','claudio','pippo');
$testo='giuseppina';

che differenza c'è per aggiungere un elemento alla fine dell'array tra:

array_push($array,$testo);

oppure

$array[]=$testo;?

il risultato non è sempre lo stesso?
Codice:
for($x=0;$x<count($array); $x++){
echo $array[$x].' ';}
 
ciao
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:

<?php
$array[] = $var;
?>
repeated for each var.

però in alcuni casi, secondo me, facilita la lettura dello script

PHP:
<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>

vedi:

http://it.php.net/manual/en/function.array-push.php
 

Discussioni simili