Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi 
 
In the book on page 57 it states in the tips "Because PHP is lax with its variable
structures, an array can even use a combination of numbers and strings as its keys. The only important rule is that the keys of an array must each be unique."
 
How can you get duplicate keys? 
 
I tried experimenting wrote the following:

 

<?php
echo "<p>This text was generated by PHP!</p>";
echo "hello word<br>";
$states['IL'] = "hellow";
$states['IM'] = "IM hellow";
$states['IM'] = "IM hellow2";
echo $states['IL'];
echo "<br>";
echo $states['IM'];
echo "<br>";
echo "Test this: {$states['IM']}";
?>

 

It simply assigns the new value to the array and not let me create a duplicate?

Share this post


Link to post
Share on other sites

This is correct. You cannot use duplicate keys, which is why I state each key must be unique. As you've discovered, using a duplicate key results in overwriting a previously stored value at that key.

Share this post


Link to post
Share on other sites

Thank again 

 

Sorry for asking too many questions ... I am enjoying it ... I started learning a while ago then stopped and now started again this time not to quit ... I realised alot of things I simply glossed over ... or didt type out all the code nor experimented much with the code ... this time round I am doing that and its alot more fun and I am enjoying it ... 

 

I think by being curious and trying out different things giving me more confidence to write my own little snippets ... 

Share this post


Link to post
Share on other sites

I'm glad you're learning new things and enjoying it. It's also great to see that you're experimenting and trying other things in order to fully grasp the concepts. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...