Weak typing in PHP: why use isset at all?

前端 未结 9 837
孤街浪徒
孤街浪徒 2020-12-01 12:38

It seems like my code works to check for null if I do

if ($tx) 

or

if (isset($tx))

why would I do the se

9条回答
  •  Happy的楠姐
    2020-12-01 13:27

    Put simply, those comparison are not the same. The first will cast the variable to a boolean, which will fail the check on empty arrays, non-numeric strings and other checks, in which the variable is definitely set. The second check (isset()) tests whether the variable exists at all.

    I would never perform the first check (boolean cast) without checking the type of the variable as well.

    Not to mention, the first check will throw an E_NOTICE, and I always run with at the maximum error reporting level.

提交回复
热议问题