Reputation: 93
Simple array but I am getting an error. Even when create it in its own PHP page I still get an error. I am not experienced at this so be kind.
<?php $state = $array(Kentucky, New Mexico, New York, Alabama, Nebraska, Alaska, American Samoa, Arizona,
Arkansas, California, Colorado, Connecticut, Delaware, District of Columbia, Florida, Guam, Hawaii, Idaho,
Illinois, Indiana, Iowa, Kansas, Louisiana, Maryland, Massachusetts, Minnesota, Mississippi, Missouri, Montana,
Nebraska, Nevada, New Hampshire, New Jersey, North Carolina, North Dakota, Northern Marianas Islands, Ohio,
Oklahoma, Oregon, Pennsylvania, Puerto Rico, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah,
Vermont, Virginia, Virgin Islands, Washington, West Virginia, Wisconsin, Wyoming, Georgia, Maine, Michigan);
?>
Any have any ideas as to the reason why?
Regards, MIke
Upvotes: 0
Views: 366
Reputation: 141
$
before the array keyword.$state = array('x', 'y', 'z', ..........);
Upvotes: 0
Reputation: 490173
Your current code says...
Execute a function stored in
$array
with a large number of global constants (and syntax errors, such as Virgin Islands).
Drop the $
sigil from $array
, and quote your strings, either with single quote ('
) or double quote ("
).
Upvotes: 2
Reputation: 1339
First, I think it's pretty subjective, but when you make an array of 'states', you might want to use plural, eg: $states.
I think it's a good habit.
Let's say you use a foreach($state in $states), it won't be confusing.
Second, if the variables in the array are strings, you must put them betwen "quotes".
Third, dollar sign ($) is used for variables, and array() is not a variable, it's a function, so we can remove the $.
Then you have your array, I recommend you to read some php documentation.
<?php
$states = array("Kentucky", "New Mexico", "New York", "Alabama", "Nebraska", "Alaska",
"American Samoa", "Arizona, Arkansas", "California", "Colorado", "Connecticut", "Delaware",
"District of Columbia", "Florida", "Guam", "Hawaii", "Idaho, Illinois", "Indiana", "Iowa",
"Kansas", "Louisiana", "Maryland", "Massachusetts", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "North Carolina",
"North Dakota", "Northern Marianas Islands", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
"Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
"Vermont", "Virginia", "Virgin Islands", "Washington", "West Virginia", "Wisconsin", "Wyoming",
"Georgia", "Maine", "Michigan");
?>
Upvotes: 0
Reputation: 163228
You have a $
character at the beginning of the array
construct, remove it so it should be like this:
$state = array(...);
Each element in the array should be surrounded by quotes to denote that they are strings.
Upvotes: 1
Reputation: 101604
String needs to be enclosed in single or double quotes, otherwise PHP will think they are a constant or keyword. Also, you need to use array()
($array
would be a variable name, not a type) Try this:
<?php
$state = array("Alabama","Alaska","Arizona","Arkansas","California","Colorado",
"Connecticut","Delaware","District of Columbia","Florida","Georgia",
"Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky",
"Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota",
"Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire",
"New Jersey","New Mexico","New York","North Carolina","North Dakota",
"Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina",
"South Dakota","Tennessee","Texas","Utah","Vermont","Virginia",
"Washington","West Virginia","Wisconsin","Wyoming");
?>
Upvotes: 1
Reputation: 145482
If you don't want to rewrite a whole lot, then just do:
<?php $state = str_getcsv("Kentucky, New Mexico, New York, Alabama, Nebraska, Alaska, American Samoa, Arizona,
Arkansas, California, Colorado, Connecticut, Delaware, District of Columbia, Florida, Guam, Hawaii, Idaho,
Illinois, Indiana, Iowa, Kansas, Louisiana, Maryland, Massachusetts, Minnesota, Mississippi, Missouri, Montana,
Nebraska, Nevada, New Hampshire, New Jersey, North Carolina, North Dakota, Northern Marianas Islands, Ohio,
Oklahoma, Oregon, Pennsylvania, Puerto Rico, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah,
Vermont, Virginia, Virgin Islands, Washington, West Virginia, Wisconsin, Wyoming, Georgia, Maine, Michigan");
This will turn your "string, list" into an proper array.
Upvotes: 0
Reputation: 1963
Remove the $ in front of the word array.
<?php $state = array( ...
And put each array item in quotes.
"Kansas","New Mexico"
Upvotes: 2