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

  • Home
  • SEARCH
  • 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 9159367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:28:31+00:00 2026-06-17T13:28:31+00:00

In PHP, you can handle errors by calling or die to exit when you

  • 0

In PHP, you can handle errors by calling or die to exit when you encounter certain errors, like this:

$handle = fopen($location, "r") or die("Couldn't get handle");

Using die() isn’t a great way to handle errors. I’d rather return an error code so the parent function can decide what to do, instead of just ending the script ungracefully and displaying the error to the user.

However, PHP shows an error when I try to replace or die with or return, like this:

$handle = fopen($location, "r") or return 0;

Why does or die() work, but not or return 0?

  • 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-06-17T13:28:32+00:00Added an answer on June 17, 2026 at 1:28 pm

    I want to thank you for asking this question, since I had no idea that you couldn’t perform an or return in PHP. I was as surprised as you when I tested it. This question gave me a good excuse to do some research and play around in PHP’s internals, which was actually quite fun. However, I’m not an expert on PHP’s internals, so the following is a layman’s view of the PHP internals, although I think it’s fairly accurate.


    or return doesn’t work because return isn’t considered an “expression” by the language parser – simple as that.

    The keyword or is defined in the PHP language as a token called T_LOGICAL_OR, and the only expression where it seems to be defined looks like this:

     expr T_LOGICAL_OR { zend_do_boolean_or_begin(&$1, &$2 TSRMLS_CC); } expr { zend_do_boolean_or_end(&$$, &$1, &$4, &$2 TSRMLS_CC); }
    

    Don’t worry about the bits in the braces – that just defines how the actual “or” logic is handled. What you’re left with is expr T_LOGICAL_OR expr, which just says that it’s a valid expression to have an expression, followed by the T_LOGICAL_OR token, followed by another expression.

    An expr is also defined by the parser, as you would expect. It can either be a r_variable, which just means that it’s a variable that you’re allowed to read, or an expr_without_variable, which is a fancy way of saying that an expression can be made of other expressions.

    You can do or die() because the language construct die (not a function!) and its alias exit are both represented by the token T_EXIT, and T_EXIT is considered a valid expr_without_variable, whereas the return statement – token T_RETURN – is not.

    Now, why is T_EXIT considered an expression but T_RETURN is not? Honestly, I have no clue. Maybe it was just a design choice made just to allow the or die() construct that you’re asking about. The fact that it used to be so widely used – at least in things like tutorials, since I can’t speak to a large volume of production code – seems to imply that this may have been an intentional choice. You would have to ask the language developers to know for sure.


    With all of that said, this shouldn’t matter. While the or die() construct seemed ubiquitous in tutorials (see above) a few years ago, it’s not really recommended, since it’s an example of “clever code”. or die() isn’t a construct of its own, but rather it’s a trick which uses – some might say abuses – two side-effects of the or operator:

    • it is very low in the operator precedence list, which means practically every other expression will be evaluated before it is
    • it is a short-circuiting operator, which means that the second operand (the bit after the or) is not executed if the first operand returns TRUE, since if one operand is TRUE in an or expression, then they both are.

    Some people consider this sort of trickery to be unfavourable, since it is harder for a programmer to read yet only saves a few characters of space in the source code. Since programmer time is expensive, and disk space is cheap, you can see why people don’t like this.

    Instead, you should be explicit with your intent by expanding your code into a full-fledged if statement:

    $handle = fopen($location, "r");
    if ($handle) {
      // process the file
    } else {
      return 0;
    }
    

    You can even do the variable assignment right in the if statement. Some people still find this unreadable, but most people (myself included) disagree:

    if ($handle = fopen($location, "r")) {
      // process the file
    } else {
      return 0;
    }
    

    One last thing: it is convention that returning 0 as a status code indicates success, so you would probably want to return a different value to indicate that you couldn’t open the file.

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

Sidebar

Related Questions

I need to modify a php search script so that it can handle multiple
how to make php can read log file in /var/log/.... I tried this code
I was wondering if PHP can do this as there seems to be no
How can PHP cause memory leaks, buffer overflows, stack overflows and any other errors
In php i can get today date/day by using : $today = date('D, Y-m-d');
In php how can I read a text file and get each line into
Can anybody tell me how are exceptions handled in PHP? I have this code
I am using PHP to process a form and handle errors. The errors work
Not everyone knows that php can handle variables that have first characters as numbers,
I used to use this when I wanted to trigger errors in PHP, coming

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.