Multiple return values to indicate success/failure
<p>I'm kind of interested in getting some feedback about this technique I picked up from somewhere.</p>

<p>I use this when a function can either succeed or fail, but you'd like to get more information about why it failed. A standard way to do this same thing would be with exception handling, but I often find it a bit over the top for this sort of thing, plus PHP4 does not offer this.</p>

<p>Basically the technique involves returning true for success, and <em>something</em> which <em>equates</em> to false for failure. Here's an example to show what I mean:</p>

<pre><code>define ('DUPLICATE_USERNAME', false);
define ('DATABASE_ERROR', 0);
define ('INSUFFICIENT_DETAILS', 0.0);
define ('OK', true);

function createUser($username) {
    // create the user and return the appropriate constant from the above
}
</code></pre>

<p>The beauty of this is that in your calling code, if you don't care WHY the user creation failed, you can write simple and readable code:</p>

<pre><code>if (createUser('fred')) {
    // yay, it worked!
} else {
    // aww, it didn't work.
}
</code></pre>

<p>If you particularly want to check why it didn't work (for logging, display to the user, or do whatever), use identity comparison with ===</p>

<pre><code>$status = createUser('fred');
if ($status) {
    // yay, it worked!
} else if ($status === DUPLICATE_USERNAME) {
    // tell the user about it and get them to try again.
} else {
    // aww, it didn't work. log it and show a generic error message? whatever.
}
</code></pre>

<p>The way I see it, the benefits of this are that it is a normal expectation that a successful execution of a function like that would return true, and failure return false.</p>

<p>The downside is that you can only have 7 "error" return values: false, 0, 0.0, "0", null, "", and <code>(object) null</code>. If you forget to use identity checking you could get your program flow all wrong. Someone else has told me that using constants like an enum where they all equate to false is "ick".</p>

<p><hr /></p>

<p>So, to restate the question: how acceptable is a practise like this? Would you recommend a different way to achieve the same thing? </p>
