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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:55:54+00:00 2026-05-15T22:55:54+00:00

Often in PHP, I see: $result = mysql_query($query) or die(); Coming from python, I

  • 0

Often in PHP, I see:

$result = mysql_query($query) or die();

Coming from python, I know why this should work, because or returns the first value if it is true in a boolean context, and the second value otherwise (see this).

But when I try the above technique in PHP in another context, for example something like:

$name = "John Doe";
echo $name or "Anonymous";

The or doesn’t return the first value (“John Doe”), it returns 1.

Why does this work in the mysql_query() result case, but not in other cases? Is it bad to use in a mysql_query() case (ignore the fact that I am not returning a useful error to the user)?

  • 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-15T22:55:55+00:00Added an answer on May 15, 2026 at 10:55 pm

    In PHP, variable assignment (the equals sign) and functions both take precedence over the or operator. That means a function gets executed first, then the return value of the function is used in the or comparison. In turn when you use two values/variables together with an or operator, it compares the two values first then returns a Boolean value.

    Therefore, the order of evaluation in this example is:

    $result = mysql_query($query) or die();
    
    1. mysql_query($query)
      Returns either a result set for DQL queries such as SELECT, or a Boolean value for DDL, DML or DCL queries such as CREATE, DROP, INSERT, UPDATE, DELETE and ALTER.

    2. $result = mysql_query($query)
      The result of this query execution is assigned to the variable $result.

    3. $result /* = ... */ or die();
      If it’s either a result set or true, it’s considered true (aka “truthy”) so the or condition is satisfied and the statement ends here. Otherwise the script would die() instead.


    echo is a language construct and therefore doesn’t actually return a value, so it doesn’t run like a function before the or comparison is made.

    As $name or "Anonymous" is always true because the string "Anonymous" is non-empty and therefore truthy, the echo implicitly converts true to 1, hence that output.

    The order of evaluation in this example is:

    $name = "John Doe";
    echo $name or "Anonymous";
    
    1. $name = "John Doe";
      Pretty straightforward — assigns the string John Doe to $name.

    2. $name or "Anonymous"
      PHP discovers that $name contains the string John Doe, so what ends up being evaluated is the following:

    3. "John Doe" or "Anonymous"
      Since at least one string is non-empty here, it’s considered truthy and the condition is satisfied. This evaluation then returns true.

    4. echo true /* $name or... */;
      Converts true to 1 and prints the number 1.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I often see this idiom when reading php code: public function __construct($config) { if
Where I work, I often see code like that : public void Test(Models.User.UserInfo userInfo,
I have 3 tables (A,B,C) and I need quite often to query the result
Is there a place where we can see PHP <=> C++ equilavent libraries/function for
I often use the top command to see what is taking up resources. Mostly
In PHP, I often use the conditional operator to add an attribute to an
I noticed that a lot of tutorial instructions often have this code: $sql=SELECT *
I'm working from inside an ipython shell and often need to reload the script
A question of particular interest about python for loops. Engineering programs often require values
I have a php web application where certain data changes on a weekly basis

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.