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 7600915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:02:08+00:00 2026-05-30T23:02:08+00:00

I’m working in PHP (but in this case I think the programming language doesn’t

  • 0

I’m working in PHP (but in this case I think the programming language doesn’t matter), and in my class methods I usually meet the following situations:

  1. Method has to return true or false
  2. Method has to return true or error message
  3. Method has to return true + success message or false + error message
  4. Method has to return true + success results (object, array, whatever) or false
  5. Method has to return true + success results (object, array, whatever) or false + error message
  6. etc.

My problem is, that when I use this class methods in my code somewhere I always have to come back to the class, and check what is the method actually returning: simply true or false, true or error message, etc.

Is a good idea to standardize the returning values? If yes, how?

My idea is:

  1. if function has to return true or false then simply return true or false
  2. if function has to return true or error message then:

    if (success)
    {
        return array(
            TRUE,
            null
        );
    }
    else
    {
        return array(
            FALSE,
            $error_message
        );      
    }
    
  3. if function has to return true + success message or error message then:

    if (success)
    {
        return array(
            TRUE,
            $success_message,
        );
    }
    else
    {
        return array(
            FALSE,
            $error_message
        );      
    }
    
  4. etc.

I hope you understand guys my problem, even thought my explanation wasn’t so good 🙂
What are your suggestions, or best practices? How should I handle this?

UPDATE:
Let’s take a simple example:

function login($username, $password) 
{
    // Login logic here ..
    if ($logged_in) 
    {
        return TRUE;
    }
    {
        return $error_message;
    }
}

So the correct way to do this will be: return true, or throw exception, and when calling the login method do in withing a try catch. So, when somethong goes wrong (validation fails, etc) I should use exceptions.

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-30T23:02:10+00:00Added an answer on May 30, 2026 at 11:02 pm

    I’d say the premise of returning a boolean and something else is misguided.

    A function should have a clear purpose with a clear result. If this result can be achieved, the result is returned. If the result cannot be achieved, the function either returns false or throws an exception. Which is better depends on the situation and your general error-handling philosophy. Either way, it’s not typically useful to have a function return an error message. That message is not useful to the code that called the function.

    PHP has its own mechanism to output error messages in addition to returning false results: trigger_error. It’s purely a tool to aid debugging though, it doesn’t replace the standard return value. It can be a good fit for cases where you want to display error messages purely to aid the developer though.

    If a function is complex enough to possibly result in several different types of errors that need to be handled differently, you should use exceptions to do so.

    For example, a very simple function with a clear purpose that only needs to return true or false:

    function isUserLoggedIn() {
        return $this->user == 'logged in';
    }
    

    A function with a purpose that may fail to fulfill that purpose:

    function convertToFoo($bar) {
        if (!is_int($bar)) {
            return false;
        }
        // here be dragons
        return $foo;
    }
    

    The same function that also triggers a message, useful for debugging:

    function convertToFoo($bar) {
        if (!is_int($bar)) {
            trigger_error('$bar must be an int', E_USER_WARNING);
            return false;
        }
        // here be dragons
        return $foo;
    }
    

    A function that may legitimately run into several different kinds of errors that the calling code needs to know about:

    function httpRequest($url) {
        ...
    
        if (/* could not connect */) {
            throw new CouldNotConnectException('Response code: ' . $code);
        }
    
        ...
    
        if (/* 404 */) {
            throw new PageNotFoundException('Page not found for ' . $url);
        }
    
        return true;
    }
    

    And I’ll paste this comment here as well:

    It should not be the responsibility of the function to prepare, return
    or display an end-user error message. If the purpose of the function
    is to, say, fetch something from the database, then displaying error
    messages is none of its business. The code that called the
    fetch-from-database function merely needs to be informed of the
    result; from here there needs to be code whose sole job it is to
    display an error message in case the database function cannot get the
    required information. Don’t mix those two responsibilities.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.