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?

4
  • '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.' nl1.php.net/manual/en/language.operators.comparison.php p.s. so i guess that empty string is converted to 0? Commented Sep 4, 2015 at 21:37
  • Forget about isset. Just try $value = 0; if ($value <> '') { do something } Commented Sep 4, 2015 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. Commented Sep 4, 2015 at 21:40
  • 2
    And for an explanation of why, see here: stackoverflow.com/questions/28739397/… Commented Sep 4, 2015 at 21:42

2 Answers 2

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: https://www.php.net/manual/en/language.operators.comparison.php

Sign up to request clarification or add additional context in comments.

6 Comments

Although I have ticked this I am afraid this does not work - if ($value !== '') as Nevermind suggests does though.
If you set the value to false, and then check for !== false, it absolutely works. Show me your code if you are having issues.
The issue it seems with !== false is that it picked up every record ... as it included the '' ones (ie empty as opposed to null)
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.
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?
|
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

1 Comment

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... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.