douying2243 2013-03-26 14:16
浏览 33
已采纳

PHP ==比较在循环中不起作用[关闭]

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

  • 写回答

1条回答 默认 最新

  • dosgy00204 2013-03-26 14:23
    关注

    $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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看