scanreg Posted July 11, 2009 Share Posted July 11, 2009 I have the following: $monthName = array(1=> "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); The above seems to be a shortcut way to write this: $monthName = array(1=> 'January', 2=> 'February', 3=> 'March', 4=> 'April', 5=> 'May', 6=> 'June', 7=> 'July', 8=> 'August', 9=> 'September', 10=> 'October', 11=> 'November', 12=> 'December'); 1. Do I understand it right? 2. How does php know to insert the key numbers for the other months in the first example above ? 3. Also, does it matter whether double quotes or single quotes are used? I've tried both and they both work but I'm just wondering if there is a difference (depending on the data content for the array). Super thanks Link to comment https://forums.phpfreaks.com/topic/165643-array-shortcut-is-this-just-an-easier-way-to-write-the-array/ Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 php automatically creates numerical keys. It uses the next available number, based on the last one created, if no key is specified. Since you made the first one 1, the next one will be 2, etc..if you do not specify any, it starts at 0. There are a couple "shortcuts" to array creation, but not for months of the year. For example, you can use range to auto-create an array with values as a range of numbers (or letters). In this situation, single vs. double quotes do not matter. The difference between single and double quotes is that php treats single quoted values as a literal string (it does not parse it), and double quoted values as something to be parsed (It will parse variables and shortcut char classes inside double quotes. Example: $foo = "bar"; $string = '$foo \n'; echo $string; // will output a literal dollar sign, foo and a literal backslash and n $string = "$foo \n"; //will output bar and a newline Link to comment https://forums.phpfreaks.com/topic/165643-array-shortcut-is-this-just-an-easier-way-to-write-the-array/#findComment-873796 Share on other sites More sharing options...
scanreg Posted July 12, 2009 Author Share Posted July 12, 2009 Thanks so much I heard about single versus double quotes but I wasn't sure if it applied within an array Thanks for the shortcut info, too, very interesting that php auto-creates the numerical keys, saves time for sure Very cool, thanks again Link to comment https://forums.phpfreaks.com/topic/165643-array-shortcut-is-this-just-an-easier-way-to-write-the-array/#findComment-873821 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.