Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 39411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:51:38+00:00 2026-05-10T14:51:38+00:00

I’m kind of interested in getting some feedback about this technique I picked up

  • 0

I’m kind of interested in getting some feedback about this technique I picked up from somewhere.

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.

Basically the technique involves returning true for success, and something which equates to false for failure. Here’s an example to show what I mean:

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 } 

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:

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

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

$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. } 

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.

The downside is that you can only have 7 'error' return values: false, 0, 0.0, '0', null, '', and (object) null. 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'.


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

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:51:39+00:00Added an answer on May 10, 2026 at 2:51 pm

    I agree with the others who have stated that this is a little on the WTFy side. If it’s clearly documented functionality, then it’s less of an issue, but I think it’d be safer to take an alternate route of returning 0 for success and integers for error codes. If you don’t like that idea or the idea of a global last error variable, consider redefining your function as:

    function createUser($username, &$error) 

    Then you can use:

    if (createUser('fred', $error)) {     echo 'success'; } else {     echo $error; } 

    Inside createUser, just populate $error with any error you encounter and it’ll be accessible outside of the function scope due to the reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.