0

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

3
  • 1
    you have $ at the start of array Commented Feb 10, 2011 at 23:31
  • 1
    and you should put the strings in " ". Commented Feb 10, 2011 at 23:32
  • in this form <?php $state = array("Kentucky",....): Commented Feb 10, 2011 at 23:39

7 Answers 7

2

Remove the $ in front of the word array.

<?php $state = array( ...

And put each array item in quotes.

"Kansas","New Mexico"
Sign up to request clarification or add additional context in comments.

1 Comment

Did you put the array items in quotes as well?
2

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 (").

Comments

1

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");
?>

2 Comments

You do not need to use Array(), in fact array() is preferred.
@JacobRelkin: Understood, was more the emphasis on losing the $ than the capitalization. And I go back and forth between .NET/PHP these days, Just whichever I get in the habit of I tend to project in future code. ;-)
1

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.

3 Comments

Except for the undecorated string issues. ;p
@Brad CHristie, to be fair its not really an "issue" other then if E_NOTICE (if they are not constants) is enabled to be logged, the logs will be filled pretty dang quick :) And it may take a hit on performance but not 100% on that. (Yes, I know it is way better to do it properly just saying)
Find it hard to believe Virgin Islands and District of Columbia are constants.
0

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.

Comments

0

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");
?>

Comments

0
  1. Firstly, you should remove the $ before the array keyword.
  2. Secondly, you have to put the array values in quotes. Like

$state = array('x', 'y', 'z', ..........);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.