Testing (x == 0) - Should x=null Result in True?

I had a test in a server action if statement for (x==0) and the if statement was entered when x was null.

Is this what I should expect?

Should I have used === instead?

Six year Wappler user feeling confused! :face_with_spiral_eyes:

It seems the behaviour is different for JavaScript and PHP:

JS:

null == 0 // false

PHP:

<?php
echo null == 0; // 1

Maybe, it's a good skill!

But Wappler sometimes likes to play tricks, so my === failed miserably:
For example, sometimes I inserted expression false, and it converted to "false", but this is now fixed
(just saying you should double-check in case it tries to do anything funny)

Your question is very confusing, meaning is unclear but if you mean is 0 and null the same for the purpose of equality tests, no.
Null basically means has no value.

You could check for both conditions in the test...

( x == 0 ) || ( x == NULL)

something like that if x = 0 OR x = null etc

2 Likes

Another trick is doing !x

We could run the same tests in a sandbox as I did in my first post, to see the outcomes of !null:

JS:

!null // true

PHP:

<?php
echo !null; // 1

This seems like the better option

Thanks for your help @Apple and @baub !

@Teodor , I’d love to hear the official Wappler response to this…

In php the condition x == 0 will be true for both 0 and null. If you want to check the data type also, use === instead of ==

1 Like