Empty variable define in php

ZeroSuf3r picture ZeroSuf3r · Mar 18, 2012 · Viewed 44k times · Source

Before doing something with $error:

$error = NULL;

In some script's saw:

$error = '';
$error = false;
$error = 0;
  • Which method is 'better' or maybe it depends in which situation i use it ?
  • What's your suggestion ?

Answer

Madara's Ghost picture Madara's Ghost · Mar 18, 2012

Depends on your design:

  • Are you setting it as an Object in case of error? Use NULL.
  • Are you setting it to true in case of error? Use false.
  • Are you setting it as a number of some sort in case of error? Use 0.
  • Are you setting it to a string to describe the error? Use ''.

A better way to indicate errors would be throwing Exceptions though, rather than setting a variable and determine the error according to it.