Thursday, 13 August 2015

creating dynamic arrays and dynamic variables example in php

<?php

$test = array();                                           //  The Dynamic array declaration
$i=0;
$nl = array("abc","yzc","ijk");                  // Static array

for($i=0;$i<3;$i++){
echo $nl[$i]."\n";                                // Displaying static array
}

/* Gives out put as
abc
yzc
ijk


*/

for($i=0;$i<3;$i++){
$test[$nl[$i]]="number:".$i + 1;      
${"file" . $i} = "test $i";

}

var_dump($test);

/* Gives output as

array(3) { ["abc"]=> int(1) ["yzc"]=> int(1) ["ijk"]=> int(1) } 

*/


echo "<br/>".$test["yzc"];     // Output is one i.e., 1

for($i=0; $i<=2; $i++) {
echo "<br/>".${"file".$i};
}

/* Gives output as

test 0
test 1
test 2


*/

?>

No comments:

Post a Comment