1. Home
  2. Docs
  3. PHP Style Guide
  4. Comparing Return Values and Typecasting

Comparing Return Values and Typecasting

Some PHP functions return FALSE on failure, but may also have a valid return value of “” or 0, which would evaluate to FALSE in loose comparisons. Be explicit by comparing the variable type when using these return values in conditionals to ensure the return value is indeed what you expect, and not a value that has an equivalent loose-type evaluation.

Use the same stringency in returning and checking your own variables. Use === and !== as necessary.

INCORRECT:

CORRECT:

INCORRECT:

CORRECT:

See also information regarding typecasting, which can be quite useful. Typecasting has a slightly different effect which may be desirable. When casting a variable as a string, for instance, NULL and boolean FALSE variables become empty strings, 0 (and other numbers) become strings of digits, and boolean TRUE becomes “1”:

Was this article helpful to you? Yes No

How can we help?