SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Returning a Boolean from a Function

    If I want to return a BOOLEAN from a Function, which is the proper format...

    Choice #1:
    PHP Code:
        return array("FALSE"$errors[$articleSurveyQuestionID]); 

    Choice #2:
    PHP Code:
        return array(FALSE$errors[$articleSurveyQuestionID]); 

    Or does it not matter?

    Thanks,


    Debbie

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    124
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The latter choice would be the correct one. A boolean is a predefined constant in PHP, so it must not be encased inside any sort of quotation marks. The former choice has a value of "FALSE", but it's cast as a string, and so you'll have to cater for that when wanting to see if the function returns FALSE or not.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    You will note that

    PHP Code:
    if ("FALSE") {
        echo 
    "FALSE is true";

    will output "FALSE is true", so "FALSE" is not really false; false is, so use the second option.
    No need to put it all in caps though (but you can if you want to).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you really mean that you want to return boolean from a function then you should return either true of false, not an array.
    Cheers
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    You will note that

    PHP Code:
    if ("FALSE") {
        echo 
    "FALSE is true";

    will output "FALSE is true", so "FALSE" is not really false; false is, so use the second option.
    No need to put it all in caps though (but you can if you want to).
    Okay, thanks!


    Debbie

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    If you really mean that you want to return boolean from a function then you should return either true of false, not an array.
    Cheers
    It was implied that I want to return an array where the first value is a BOOLEAN...


    Debbie

  7. #7
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, by the way typing boolean in all upper case does not make them more boolean.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,934
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    OK, by the way typing boolean in all upper case does not make them more boolean.
    The convention in most programming languages is to make TRUE and FALSE upper-case...


    Debbie


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •