Why is this PHP if condition always true?

user1651663

So right now I'm pulling data from a json API (battle.net) with this request

https://us.api.battle.net/wow/realm/status?locale=en_US&realms=runetotem&apikey=xxxxxxxxxxxxxxx

API key hidden intentionally, this returns the following

{
  "realms": [{
    "type": "pve",
    "population": "medium",
    "queue": false,
    "wintergrasp": {
      "area": 1,
      "controlling-faction": 1,
      "status": 0,
      "next": 1433468324674
    },
    "tol-barad": {
      "area": 21,
      "controlling-faction": 1,
      "status": 2,
      "next": 1433465638814
    },
    "status": true,
    "name": "Runetotem",
    "slug": "runetotem",
    "battlegroup": "Vengeance",
    "locale": "en_US",
    "timezone": "America/Los_Angeles",
    "connected_realms": ["uther", "runetotem"]
  }]
}

Which I am putting into an array using php and then attempting to call upon values from said array in an if statement, however the if statement is always firing true for my check. I don't understand why at all, if someone could please help that'd be great. Code below.

<?php 
$json_url = "https://us.api.battle.net/wow/realm/status?locale=en_US&realms=runetotem&apikey=xxxxxxxxxxx";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
?>

<tr class="latestthreads_portal">

<td width="230px" class="trow3">
<strong>Realm Status</strong>
<span class="smalltext"><br />
<?php if ($data['realms'][0]['status'] == 2)
{
    echo "up";
    }
    else{
    echo "Down?";
    }
?>
    <br />

  <br /> <div class="border"></div>
</span>
</td>
</tr>

Please excuse crappy formatting, in a rush.

Jeremy Anderson

You have comparison issues and data nesting issues, I think. It looks to me like you are comparing if (2 == true) which evaluates to true in php. http://php.net/manual/en/types.comparisons.php

Also you are only checking the status of the "realms" array, not the status of "tol-barad" and "wintergrasp" objects, not sure what you wanted, exactly.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does my condition always evaluate as true?

분류에서Dev

Why the hash command in bash always returns true for ./<something>

분류에서Dev

Nullable Decimal If Is Always True

분류에서Dev

PHP Why does the DateTime diff function always return zero?

분류에서Dev

Why is this Angular expression in an ng-class inside an ng-repeat always treated as true?

분류에서Dev

Why is `True and ~True` -2?

분류에서Dev

If either or both condition true

분류에서Dev

AHK if statment with variables not working always true

분류에서Dev

Logical in my R function always returns "TRUE"

분류에서Dev

if(!buydat.empty()) validation fails (always true)

분류에서Dev

PropertyInfo.GetValue on Boolean is always True

분류에서Dev

Function always returning true no matter input

분류에서Dev

Return results in join even when condition is not true

분류에서Dev

Why are Fortran pointers always variable

분류에서Dev

Why PrinterState always returns null?

분류에서Dev

Why not always Quad-pump?

분류에서Dev

Why is ArrayAdapter always null in this example?

분류에서Dev

PHP function if function == true

분류에서Dev

Redundant condition in PHP code or not?

분류에서Dev

PHP condition not being read

분류에서Dev

php eval condition understanding

분류에서Dev

Why is if True slower than if 1?

분류에서Dev

PHP Assign php variable into another php condition

분류에서Dev

Map function applied on tuple always inexplicably evaluates to True

분류에서Dev

Is T(n) >= T(n-1) always true?

분류에서Dev

Does the double-exclamation (!!) trick always produce true or false in JavaScript?

분류에서Dev

assertText in Selenium IDE: always true, due to target being display:none

분류에서Dev

Comparing two string iterators always comes out true C++

분류에서Dev

Return only true or false (PHP)

Related 관련 기사

뜨겁다태그

보관