1

I have an array that stores numeric values. The issue is when I want to store the value 0:

.... 
$gradePoints = 0;
$students[$var1][$var2] = $gradePoints;
....

To check the value has been stored:

echo $students[$var1][$var2];

returns 0 ... so the value 0 has been successfully stored in the array.

However later when I search the array:

$value= isset($students[$var1][$var2]) ? $students[$var1][$var2] : '';

if ($value <> '')
{
do something;
}

If $value is anything but 0 it is fine but php seems to overlook the value 0. If I change

$gradePoints = 0;

to

$gradePoints = 0.1;

all is well. Can anyone explain why $value <> '' works for all values other than 0?

RGriffiths
  • 5,722
  • 18
  • 72
  • 120
  • 'If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.' http://nl1.php.net/manual/en/language.operators.comparison.php p.s. so i guess that empty string is converted to 0? – sinisake Sep 04 '15 at 21:37
  • Forget about `isset`. Just try `$value = 0; if ($value <> '') { do something }` – Barmar Sep 04 '15 at 21:38
  • 2
    Don't use a string comparison when testing for numeric values. Set $value to null, or false, and then test for that condition. – devlin carnate Sep 04 '15 at 21:40
  • 2
    And for an explanation of why, see here: http://stackoverflow.com/questions/28739397/php-compare-equality-emtpy-string-0-and-0 – devlin carnate Sep 04 '15 at 21:42

2 Answers2

3

Change:

$value= isset($students[$var1][$var2]) ? $students[$var1][$var2] : '';

if ($value <> '')

To:

$value = isset($students[$var1][$var2]) ? $students[$var1][$var2] : false;

if ($value !== false)

Course there are other ways to do it as well. Bottom line you want to avoid comparing zeros and empty strings, they will get evaluated as the same. Instead use === and !== for a precise comparison.

See here for more details: http://nl1.php.net/manual/en/language.operators.comparison.php

jszobody
  • 28,495
  • 6
  • 61
  • 72
  • Although I have ticked this I am afraid this does not work - if ($value !== '') as Nevermind suggests does though. – RGriffiths Sep 06 '15 at 09:42
  • If you set the value to false, and then check for `!== false`, it absolutely works. Show me your code if you are having issues. – jszobody Sep 06 '15 at 11:26
  • The issue it seems with !== false is that it picked up every record ... as it included the '' ones (ie empty as opposed to null) – RGriffiths Sep 06 '15 at 16:06
  • Then you have an issue somewhere that I can't see. Using concrete null or false is better practice than setting and checking for empty strings. Glad you got it working, but you should revisit how you are solving this. – jszobody Sep 06 '15 at 17:28
  • I agree and thanks for the help. I think it is down to $value= isset($students[$var1][$var2]) ? $students[$var1][$var2] : ''; as this sets the value to be empty if not already set. Perhaps it is better to leave it as null? – RGriffiths Sep 06 '15 at 19:04
  • Did you see both lines of code I provided to you? My solution changed that line to use false instead of an empty string. – jszobody Sep 06 '15 at 19:31
0

Use this operator (not identical, it will check type of variable, too):

$value=0;



if ($value !== '')
{
//do something
}

You have issues because of type juggling™, as described here: http://php.net/manual/en/language.operators.comparison.php

sinisake
  • 11,240
  • 2
  • 19
  • 27
  • LOL mr Down Voter, please provide explanation... If you didn't noticed, i have actually provided an answer in FIRST comment to this post, BEFORE any other answer (mine included)... I think that i explained problem, and did what should be done, in this case... If OP wants to compare value with empty string (for its own reasons), if $value is 0 - this will work. You little egoistic... :) – sinisake Sep 06 '15 at 19:57