0

While reviewing a pull request, I noticed my colleague is using a lot of truthy checks in the following manner:

if ($var === 1)

This doesn't work as expected for one condition in particular:

if(!empty($myArr) === 1) {  } //

I know this won't work as expected in javascript, upon consulting php documentation, I am not 100% sure if this would work properly in PHP either.

My question is, would this work OK in PHP? Which one is a better way of checking for comparisons? true or 1

Using php 7.2

Anon
  • 1,307
  • 2
  • 20
  • 30
  • Depends on what you actually want... the "===" operator checks if value _and type_ equal. So a boolean value _can never equal_ a number using that operator. Why can't you simply use the simple variant for your example: `if (!empty($myArr)) {...}` ? – arkascha Nov 30 '19 at 04:05
  • 1
    `$var === 1` means `$var` is an **integer** equal to `1`. `$var === true` means `$var` is a **boolean** equal to `true`. They are not the same, and as you have seen, are not interchangeable in PHP either. – Nick Nov 30 '19 at 04:05
  • Gotchya @Nick that's what my understanding was but the code I am reviewing is from a much 'senior' developer in title so I was a bit confused for a moment. – Anon Nov 30 '19 at 04:08
  • 2
    @Anon just goes to show `"senior" !== "smarter"` :-) – Nick Nov 30 '19 at 04:10

0 Answers0