I have javascript code some thing like this —
var count=3;
var pl=new Array(count);
var il=new Array(count);
pl.push('<?php echo $var1; ?>');
il.push('<?php echo $var2; ?>');
Why is this not working?
But if I try some thing like this
pl[0]='<?php echo $var1; ?>';
It works. Can some one point out the problem?
Thanks
By using the
Arrayconstructor you are creating an array with a number of “slots” already defined. In your case, the arrays both have 3 elements when you create them (the value of each element isundefined.)When you use
push, you add an extra element to that array. When you set the element at a specific index, you just set the value of that element.I would suggest not using the
Arrayconstructor, and instead creating array literals:This time, the arrays are created with no elements, so when you use
pushyou are pushing an element into index 0, instead of 4.