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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

strpos returns true always in php

From Dev

Condition always true when reached in while loop

From Dev

Why is this if statement always returning true

From Dev

if condition always returns true (Java)

From Dev

SQL Server - ignoring always true condition

From Dev

Python - True part of if condition always executes

From Dev

PHP - If statement always evaluating as true?

From Dev

Why is my while loop infinite? Changing the variable each time, but condition is still always true

From Dev

Compiler: What if condition is always true / false

From Dev

Why does my condition always evaluate as true?

From Dev

Why the if condition is always false?

From Dev

Why is my condition always false?

From Dev

Why is my boolean always true?

From Dev

C++ GUI Condition for if statement always true

From Dev

WIX Operating System Condition Always true

From Dev

Why is this if statement always returning true

From Dev

When I upload an Image in PHP there's a condition that always true

From Dev

Why is this PHP if condition always true?

From Dev

awk condition always TRUE in a loop

From Dev

SQL Server - ignoring always true condition

From Dev

if condition always evaluating true

From Dev

Why is my while loop infinite? Changing the variable each time, but condition is still always true

From Dev

Bash Script if condition is always true

From Dev

Why does this If condition in VB.NET always evaluates to true and the same code in C# does not

From Dev

Why is this condition true?

From Dev

Why a condition that contains a pointer always returns true?

From Dev

Term for an if condition that is always true

From Dev

Why is my "if" condition (string comparison) always true?

From Dev

PHP Why my 'if' statement always gives TRUE?

Related Related

  1. 1

    strpos returns true always in php

  2. 2

    Condition always true when reached in while loop

  3. 3

    Why is this if statement always returning true

  4. 4

    if condition always returns true (Java)

  5. 5

    SQL Server - ignoring always true condition

  6. 6

    Python - True part of if condition always executes

  7. 7

    PHP - If statement always evaluating as true?

  8. 8

    Why is my while loop infinite? Changing the variable each time, but condition is still always true

  9. 9

    Compiler: What if condition is always true / false

  10. 10

    Why does my condition always evaluate as true?

  11. 11

    Why the if condition is always false?

  12. 12

    Why is my condition always false?

  13. 13

    Why is my boolean always true?

  14. 14

    C++ GUI Condition for if statement always true

  15. 15

    WIX Operating System Condition Always true

  16. 16

    Why is this if statement always returning true

  17. 17

    When I upload an Image in PHP there's a condition that always true

  18. 18

    Why is this PHP if condition always true?

  19. 19

    awk condition always TRUE in a loop

  20. 20

    SQL Server - ignoring always true condition

  21. 21

    if condition always evaluating true

  22. 22

    Why is my while loop infinite? Changing the variable each time, but condition is still always true

  23. 23

    Bash Script if condition is always true

  24. 24

    Why does this If condition in VB.NET always evaluates to true and the same code in C# does not

  25. 25

    Why is this condition true?

  26. 26

    Why a condition that contains a pointer always returns true?

  27. 27

    Term for an if condition that is always true

  28. 28

    Why is my "if" condition (string comparison) always true?

  29. 29

    PHP Why my 'if' statement always gives TRUE?

HotTag

Archive