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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:00+00:00 2026-06-12T09:15:00+00:00

Possible Duplicate: PHP: 'or' statement on instruction fail: how to throw a new exception?

  • 0

Possible Duplicate:
PHP: 'or' statement on instruction fail: how to throw a new exception?

In PHP, especially popular amongst the newbies in various MySQL connection tutorials, you’ve always been able to do something like this…

<?php
foo() or die('foo() failed!');
?>

However if I try something like this it fails…

<?php
foo() or throw new Exception('AHH!');
?>

Like so…

“Parse error: syntax error, unexpected ‘throw’ (T_THROW) in…”

Anybody know how to do something similar to this? Would I have to set a variable after the “or”?

  • 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-12T09:15:01+00:00Added an answer on June 12, 2026 at 9:15 am

    Do it the less “clever” way:

    if(!foo()) {
        throw new Exception('AHH!');
    }
    

    In case you’re wondering why or throw new Exception() doesn’t work, it’s because you’re relying on the short-circuiting of the or operator: If the first argument is true, then there’s no need to evaluate the second to determine if one of them is true (since you already know that at least one of them is true).

    You can’t do this with throw since it’s an expression that doesn’t return a boolean value (or any value at all), so oring doesn’t make any sense.

    If you really want to do this, the deleted answer by @emie should work (make a function that just throws an exception), since even a function with no return value is valid in a boolean statement, but it seems like a bad idea to create a function like that just so you can do clever things with boolean statements.

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

Sidebar

Related Questions

Possible Duplicate: Using same MySQL Connection in different PHP pages This is completely new
Possible Duplicate: Performing simple arithmetic in MySQL statement or in PHP code Im wondering
Possible Duplicate: PHP - reversed order in if statement Checking for null - what
Possible Duplicate: php if statement with multiple conditions $style = get_option('background_style'); if ($style ==
Possible Duplicate: PHP & mySQL: Year 2038 Bug: What is it? How to solve
Possible Duplicate: PHP: Notice: Undefined variable and Notice: Undefined index I am new to
Possible Duplicate: PHP & MySQL Pagination Update Help So, in my site, there are
Possible Duplicate: PHP make if shorter I have an if statement that looks like
Possible Duplicate: php if statement with multiple conditions I have this code: if(x==1 ||
Possible Duplicate: PHP PDO bindParam with html content I'm using PDO prepared statement to

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.