开发者

Simple Array PHP

开发者 https://www.devze.com 2023-02-10 07:38 出处:网络
Simple array but I am getting an error.Even when create 开发者_如何学Goit in its own PHP page I still get an error.I am not experienced at this so be kind.

Simple array but I am getting an error. Even when create 开发者_如何学Goit 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


Remove the $ in front of the word array.

<?php $state = array( ...

And put each array item in quotes.

"Kansas","New Mexico"


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


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


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.


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.


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


  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', ..........);

0

精彩评论

暂无评论...
验证码 换一张
取 消