0

I have this code:

print ($myarray['response']['players'][0]['VACBanned'] ? 'BANNED' : 'NOT BANNED');

Where "VACBanned" is boolean. But it doesn´t matter if it's 'true' or 'false'; it always gives out "NOT BANNED".

var_dump result: Case 1: 'VACBanned' => bool(true) Case 2: 'VACBanned' => bool(false) Both cases show "NOT BANNED"

PIC: https://i.stack.imgur.com/Rk7uv.png https://i.stack.imgur.com/o0i9f.png

Jan
  • 27
  • 6
  • What is the result of `var_dump($myarray['response']['players'][0]['VACBanned'])`? – Matt Gibson Dec 22 '14 at 19:16
  • `true` as in the **WORD** `true`, or `true` as in boolean true? There's a difference. the word true is just a string, and will ALWAYS be boolean "true", just as the word `false` would be true as well. remember that in php, non-empty strings are considered boolean true, as long as they don't contain a falsey value. – Marc B Dec 22 '14 at 19:16
  • @MarcB That was my first thought, but if this always outputs "NOT BANNED", that means it's actually falsy, right? – Matt Gibson Dec 22 '14 at 19:17
  • var_dump($myarray['response']['players'][0]['VACBanned']) One times says "true" -> "NOT BANNED" With other input it says "false" -> "NOT BANNED",too – Jan Dec 22 '14 at 19:20
  • `var_dump` should also tell you the *type* of the value (e.g. "string(4)"). Please edit that as well as the value into your question. – Matt Gibson Dec 22 '14 at 19:26
  • Case 1: 'VACBanned' => bool(true) Case 2: 'VACBanned' => bool(false) Both cases show "NOT BANNED" – Jan Dec 22 '14 at 19:52
  • I cannot reproduce your problem at all. Given your arrays (assuming that your `$myarray` maps 'response' to the array you're `var_dumping`) and your code, it works as expected for me. There must be other code that you're not showing us, or you're testing the wrong array. Can you reproduce this with a complete example, that is, a short piece of complete, standalone code that shows up the problem? – Matt Gibson Dec 22 '14 at 20:31
  • (For example: https://gist.github.com/gothick/406a627041a951747afc) – Matt Gibson Dec 22 '14 at 20:38
  • https://gist.github.com/rjan22/0fba107a420caf869641#file-gistfile1-txt Thats the whole code. Hope you understand my problem... – Jan Dec 22 '14 at 20:54

2 Answers2

0

You need to turn error checking on—or check your apache error log—and find the bugs in your code.

For example, your var_dump($myarray); shows an array that doesn't have any keys of 'response'. However, your print statement is:

print ($myarray['response']['players'][0]['VACBanned'] ? 'BANNED' : 'NOT BANNED');

The array expression can never find anything, as there's no key of "response". So it will (a) be throwing an error "Undefined index: response", and (b) always evaluate to false, and therefore always say "NOT BANNED".

Community
  • 1
  • 1
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
-1
print (($myarray['response']['players'][0]['VACBanned'] != 0) ? 'BANNED' : 'NOT BANNED');
Eugen
  • 1,356
  • 12
  • 15
  • Reason for down voting? – Eugen Dec 22 '14 at 19:45
  • Hmm, read php manual for booleans : "-1 is considered TRUE, like any other non-zero (whether negative or positive) number! " http://php.net/manual/en/language.types.boolean.php Test my edited code! – Eugen Dec 22 '14 at 20:15
  • so am I right that even 'VACBanned' => bool(false) means "true" ? How can I "convert" that into a text on my website then? false should show "NOT BANNED" and true "BANNED". Whats the best way to realise that? – Jan Dec 22 '14 at 20:22
  • Hmm, print (bool) "false" and print (bool) "true" result in 1 and is true (exists). Maybe you can convert this in string, like "(string) $myarray['response']['players'][0]['VACBanned']" and use it like print ($myarray['response']['players'][0]['VACBanned'] ? 'BANNED' : 'NOT BANNED'); – Eugen Dec 22 '14 at 20:48