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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:39+00:00 2026-05-26T01:20:39+00:00

I am looking for the correct way to handle a return statement with a

  • 0

I am looking for the correct way to handle a return statement with a bool/string. For example I do all my checking inside the function and return true if it all passes. However if something went wrong I would like to return a string of what went wrong rather than just return false; with a general string. Does php assume false if a var is set to anything besides true? What is the correct way to handle this? Here’s an example of what I’m doing

<?php
$a = 2;

$result = CheckVar($a);
if ($result)
{
    echo 'Correct!';
}
else
{
    echo $result;
}

function CheckVar($var)
{
    if ($var == 1)
    {
        return true;
    }
    else
    {
        return 'This is not the correct answer. You supplied '.$var;
    }
}
?>

It seems this method works, however is this good programming etiquette? Or is there another way I should be doing this? Thank you for your time.

  • 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-26T01:20:40+00:00Added an answer on May 26, 2026 at 1:20 am

    Does php assume false if a var is set to anything besides true?

    Not at all. PHP will return whatever the variable was set to. And actually since you have a non-empty string, that’s a “truthy” value (ie: true in a boolean context). Since you used if ($result) as your check and you return a “truthy” value, the condition is always true. You need to change that check to:

    if ($result === true) {
        ...
    

    What is the correct way to handle this?

    I think it’s a good enough way to handle it. An alternative would be to pass an error string variable by reference, and have the fail part of your code fill that, eg:

    function check($var, &$error) {
        if ($var == 1) {
            return true;
        } else {
            $error = 'This is not the correct answer. You supplied ' . $var;
            return false;
        }
    }
    

    Some native PHP functions behave like this (eg: exec().) Yet another alternative is to return an array with the errors, like Jared suggested. I personally use this option when I expect multiple errors (eg: a form validation routine):

    function check_stuff($stuff) {
        $errors = array();
        if (!$condition1) {
            $errors[] = 'Condition 1 failed';
        }
    
        if (!$condition2) {
            $errors[] = 'Condition 2 failed';
        }
    
        return $errors;
    }
    

    Now you can also take advantage of the fact that empty arrays are falsy:

    $errors = check_stuff($your_stuff);
    if (!$errors) {
        echo 'No errors!';
    } else {
        print_r($errors);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for (arguably) the correct way to return data from a XmlHttpRequest .
Looking for the correct way and control to import and export of out of
I am looking to the fastest and the correct way to check if a
I am looking at singletons and I was curious about the correct way to
I'm looking for the correct way of using wp_get_attachment_image(). The following code: <?php $args
I'm looking for a way to directly get a list of all files within
I'm looking for the correct way to structure the query for a view in
I'm looking for a way to return the value of every field in an
I'm looking for an easy way to enforce the correct implementation of INotifyPropertyChanged i.e.
I've been trawling various sites looking for the correct way to manage switching. I

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.