I have the following code. It is a collection of If statements which are inside of a loop. The if statements compare a string (obtained from an API's JSON) against pre-defined text. The problem is that the "If ==" comparison only works on the first time through the loop. It doesn't work on an subsequent times through the loop. When I Echo the text it looks identical to what I'm comparing it to.
$vstate = (string)$vstate;
$vstate = trim($vstate);
//THERE IS A PROBLEM HERE ON THE SECOND LOOP
if($vstate == "Alabama") {$fstate = "01";}
if($vstate == "Alaska") {$fstate = "02";}
if($vstate == "Arizona") {$fstate = "04";}
if($vstate == "Arkansas") {$fstate = "05";}
if($vstate == "California") {$fstate = "06";}
if($vstate == "Colorado") {$fstate = "08";}
if($vstate == "Connecticut") {$fstate = "09";}
if($vstate == "Delaware") {$fstate = "10";}
if($vstate == "District of Columbia") {$fstate = "11";}
if($vstate == "Florida") {$cstate = "12";}
if($vstate == "Georgia") {$cstate = "13";}
if($vstate == "Hawaii") {$cstate = "15";}
if($vstate == "Idaho") {$cstate = "16";}
if($vstate == "Illinois") {$cstate = "17";}
if($vstate == "Indiana") {$cstate = "18";}
if($vstate == "Iowa") {$cstate = "19";}
if($vstate == "Kansas") {$cstate = "20";}
if($vstate == "Kentucky") {$cstate = "21";}
if($vstate == "Louisiana") {$cstate = "22";}
if($vstate == "Maine") {$cstate = "23";}
if($vstate == "Maryland") {$cstate = "24";}
if($vstate == "Massachusetts") {$cstate = "25";}
if($vstate == "Michigan") {$cstate = "26";}
if($vstate == "Minnesota") {$cstate = "27";}
if($vstate == "Mississippi") {$cstate = "28";}
if($vstate == "Missouri") {$cstate = "29";}
if($vstate == "Montana") {$cstate = "30";}
if($vstate == "Nebraska") {$cstate = "31";}
if($vstate == "Nevada") {$cstate = "32";}
if($vstate == "New Hampshire") {$cstate = "33";}
if($vstate == "New Jersey") {$cstate = "34";}
if($vstate == "New Mexico") {$cstate = "35";}
if($vstate == "New York") {$cstate = "36";}
if($vstate == "North Carolina") {$cstate = "37";}
if($vstate == "North Dakota") {$cstate = "38";}
if($vstate == "Ohio") {$cstate = "39";}
if($vstate == "Oklahoma") {$cstate = "40";}
if($vstate == "Oregon") {$cstate = "41";}
if($vstate == "Pennsylvania") {$cstate = "42";}
if($vstate == "Rhode Island") {$cstate = "44";}
if($vstate == "South Carolina") {$cstate = "45";}
if($vstate == "South Dakota") {$cstate = "46";}
if($vstate == "Tennessee") {$cstate = "47";}
if($vstate == "Texas") {$cstate = "48";}
if($vstate == "Utah") {$cstate = "49";}
if($vstate == "Vermont") {$cstate = "50";}
if($vstate == "Virginia") {$cstate = "51";}
if($vstate == "Washington") {$cstate = "53";}
if($vstate == "West Virginia") {$cstate = "54";}
if($vstate == "Wisconsin") {$cstate = "55";}
if($vstate == "Wyoming") {$cstate = "56";}
if($vstate == "Puerto Rico") {$cstate = "72";}
if($vstate == "American Samoa") {$cstate = "60";}
if($vstate == "Federated States of Micronesia") {$cstate = "64";}
if($vstate == "Guam") {$cstate = "66";}
if($vstate == "Marshall Islands") {$cstate = "68";}
if($vstate == "Commonwealth of the Northern Mariana Islands") {$cstate = "69";}
if($vstate == "Palau") {$cstate = "70";}
if($vstate == "Puerto Rico") {$cstate = "72";}
if($vstate == "U.S. Minor Outlying Islands") {$cstate = "74";}
if($vstate == "U.S. Virgin Islands") {$cstate = "78";}
if($vstate == "Baker Island") {$cstate = "81";}
if($vstate == "Howland Island") {$cstate = "84";}
if($vstate == "Jarvis Island") {$cstate = "86";}
if($vstate == "Johnston Atoll") {$cstate = "67";}
if($vstate == "Kingman Reef") {$cstate = "89";}
if($vstate == "Midway Islands") {$cstate = "71";}
if($vstate == "Navassa Island") {$cstate = "76";}
if($vstate == "Palmyra Atoll") {$cstate = "95";}
if($vstate == "Wake Island") {$cstate = "79";}
echo("The vstate is " . $vstate . "<br>");
echo("The cstate is " . $cstate . "<br>");
How do I make this work? I've already tried adding Trim() but that did not solve the problem.
Thank you
$fstate and $cstate needs to be defined before entering the "loop". They are both created en an encapsulated space and gets removed when the if block ends.
What do you get if you dump it? Is it propperly casted to a string as expected?