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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:01:55+00:00 2026-05-19T17:01:55+00:00

I was reading this question and saw this line: if ($a == $b) {

  • 0

I was reading this question and saw this line:

if ($a == $b) { return true } else { return false }

And it led me to wondering, what is the best way to cast a variable of unknown type (could be string, could be int; who knows? who cares?) to a boolean?

Of course, if ($var) { return true; } else { return false; } would do the trick, but I think return $var ? true : false; is probably better.

For that matter:

  • return $var && true
  • return $var || false
  • return !empty($var)

are all probably better, but is there a best way to cast to bool? More importantly, what makes it best?

Edit to clarify:

This wasn’t written with the intention of being a comprehensive list of the ways to cast to a boolean. My question is specifically on explicit casting. Before I learned about empty I used isset($var) && $var as it would prevent errors from being thrown on undeclared variables. Now i use !empty($var) as it’s faster to type.

!empty has the (dis)advantage of not throwing any E_NOTICE errors when the variable isn’t defined. This could be considered good if you’re checking $_GET or $_SESSION variables, for the majority of other variables, I suppose this may be considered bad as it would hide issues where a variable is uninitialized where it should have been initialized.

I was curious as to whether other developers have another way of doing things that I hadn’t
known about.

  • 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-19T17:01:56+00:00Added an answer on May 19, 2026 at 5:01 pm

    The two ways are:

    1. Explicit cast:

      This is when you explicitly cast the variable using the literal cast operator (bool).

      return (bool) $expression;
      
    2. Implicit cast (using operators):

      This is where the type is inferred from the expression. In PHP, this includes logical operators and comparison operators, and any function/language construct that expects a boolean such as if and while:

      return !!$expression;
      

      or

      return $expression == true;
      

      or

      return $a == $b;
      

      or

      return $a > 1;
      

      or

      if ($a)
      

    All methods will fall into one of those two categories.

    My suggestion is that if you use any operators (==, !=, >, etc), don’t bother casting. But if you’re just returning a variable, then cast explicitly…

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

Sidebar

Related Questions

After reading this question , i saw the answer by Naveen containing a link
I was reading through some of the responses in this question and saw that
Thanks for reading my question. I saw Gizmodo & Lifehacker has this feature. Take
I was reading another question , and I saw this: var basketModule = (function()
After reading this question, I need to clear up some things. IQueryable<Customer> custs =
I was reading this question, but didn't understand some part of the aix's answer
I was reading this question (which you do not have to read because I
Now that it's answered: Don't bother reading this question, it's a bit lengthy and
I was reading over this question and wondered if the accepted answer might also
So, I was reading this earlier question for ideas on how to allow me

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.