Jump to content

General question on arrays


SmokingMan
 Share

Recommended Posts

As I strive to learn PHP, I see two operators used in assigning values to arrays. They are "=" and "=>". I can't seem to find any explanation as to the differences between them and when one or the other should be used. Can someone clear this up for me?I know this is probably VERY simple, but to a newbie to PHP like myself...not so much. :)

Link to comment
Share on other sites

I'm guessing the syntaxes you see these both in are as followed:

$arrayOne = array();// array defined, no values;$arrayOne['something'] = 'value';$arrayOne['something2']= 'other value';$arrayOne[] = 'Value appended with a numerical index instead of a string';$arrayTwo = array('key1'=>'someValue','keyTwo'='OtherValue');$arrayTwo['meow'] = 'cat';

The first is just an equals, nothing very different about it. You will access the values through the given key names or indexes.Now the second is saying that we create an array, obviously, but this array (sort of) has same base values. Say i wanted to print out someValue, i would callecho $arrayTwo['key1']. the only difference between a '=' and an '=>' is that the '=' is used when doing the first example, the '=>' is used when putting values directly into an array. This is good if its an array of things you don't need to remember(such as parser data). if you will have a variable for the array, you could use either. It doesn't matter

Link to comment
Share on other sites

Thanks, I'm learning this in my spare time at home and sometimes the tutorials don't go into the depth I would like. So I'll probably have my fair share of stoopid questions for a while until I get a little more comfortable with PHP :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...