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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:00:53+00:00 2026-05-31T04:00:53+00:00

After playing with PHP, I discovered that true is returned as 1 and false

  • 0

After playing with PHP, I discovered that true is returned as 1 and false as null.

echo (5 == 5) // displays 1
echo (5 == 4) // displays nothing

When writing functions that return true or false, what are the best practices for using them?

For example,

function IsValidInput($input) {
  if ($input...) {
    return true;
  }
  else {
    return false;
  }
}

Is this the best way to use the function?

if (IsValidInput($input)) {
  ...
}

How would you write the opposite function?

IsBadInput($input) {
  return ! IsValidInput($input);
}

When would you use the === operator?

  • 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-31T04:00:54+00:00Added an answer on May 31, 2026 at 4:00 am

    After playing with PHP, I discovered that true is returned as 1 and false as null.

    That is not true (no pun intended). PHP, like many other languages, has “truthy” and “falsy” values, which can behave like TRUE or FALSE when compared to other values.

    It is so beause PHP uses weak typing (vs. strong typing). It automatically converts different types of values when comparing them, so it can eventually compare two values of the same type. When you echo TRUE; in PHP, echo will always output a string. But you passed it a boolean value, that has to be converted to a string before echo can do its job. So TRUE is automatically converted to the string "1", while FALSE is converted to "".

    When would you use the === operator?

    This weak, or loose, typing is the reason PHP uses two equality operators, == and ===. You use === when you want to make sure both values you are comparing are not just “equal” (or equivalent), but also of the same type. In practice:

    echo 1 == TRUE; // echoes "1", because the number 1 is a truthy value
    echo 1 === TRUE; // echoes "", because 1 and TRUE are not the same type (integer and boolean)
    

    When writing functions that return true or false, what are the best practices for using them?

    Be precise when you can, returning the actual boolean TRUE or FALSE. Typical cases are functions prefixed by is, like isValidInput. One usually expects such functions to return either TRUE or FALSE.

    On the other hand, it’s useful to have your function return a “falsy” or “truthy” values in some cases. Take strpos, for example. If it finds the substring in position zero, it returns 0 (int), but if the string is not found, it returns FALSE (bool). So:

    $text = "The book is on the table";
    echo (strpos($text, "The") == FALSE) ? "Not found" : "Found"; // echoes "Not found"
    echo (strpos($text, "The") === FALSE) ? "Not found" : "Found"; // echoes "Found"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After a little testing/scripting/playing with PHP 5.3 PHAR archives, I discovered some problems. glob
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
After playing quite a bit of Bad Company 2 over the last month, I'm
Update: After playing around with this for a few hours, went with a multi-query
I just found /n softwares free Powershell NetCmdlets, and after playing with them I
So i'm trying out MVC after only playing with it some time ago. Need
After installing Monobjc and playing around with the Monobjc Application Project under C# in
I've been playing this flash game and after I'd gotten over the initial ('lol,
Alright, so after a few hours of me playing around to no avail, I
Hey guys how can I make an app keep playing an mp3 after pressed

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.