Oct
26
Posted on 26-10-2007
Filed Under (Sample Code) by chintan

There is another popular method to create and manage arrays, and it uses square brackets [ ] to mean “add to array”, earning it the name “the array operator”. Using this functionality, you can both create arrays and add to existing arrays, so this is generally more popular – you will generally only find the array() function being used when several values are being put within the array, as it will fit on one line. Here are some examples of the array operator in action:

<?php
$array[] = “Zoo”;
$array[] = “War”;
$array[] = “Taz”;
var_dump($array);

?>

That should work in precisely the same as using the array() function, except it is much more elastic – we can add to the array whenever we want to. When it comes to working with non-default indices, we can just place our key inside the square brackets, like this:

<?php
$array["a"] = “Zoo”;
$array["b"] = “War”;
$array["c"] = “Taz”;
var_dump($array);
?>

Share These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Live
  • StumbleUpon
  • Technorati
  • YahooMyWeb
(0) Comments    Read More   
Post a Comment

You must be logged in to post a comment.