SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Check boolean value in control statement: == true or without ==true?

    Hi friends

    When programer do checking boolean value, they use two way:

    PHP Code:

    if ($value == true)
    {
    //do something

    or
    PHP Code:
    if ($value)
    {
    //do something

    I know 2 methor is same and sometime I use a both in my script. But I don't know witch 1th or 2th better than or standard...

    Please give your experience. Thanks so much!

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    it doesn't matter which one you use whatsoever - an if() statment checks if the statement within the brackets is true, so putting a boolean variable in the brackets will do the same.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See these examples:
    Code php:
    $val = "dddd"; // try with some values like 1,0,"dd","",
    if($val){ //will satisfy if any of the value $val holds
    	echo "True";
    }else{
    	echo "False";
    }
    echo "<br />";
    if($val === true){ //only will satisfy for the boolean type true
    	echo "True";
    }else{
    	echo "False";
    }

    But
    PHP Code:
    if($val == true
    is the same as first one as far as i know. You can find lots of discussions before in this forum.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks @rajug and @arkinstall

    With $value is boolean, or function return boolean value, 2 methor is the same. For this reason,i want to asking what is diffirent between them, not give $variable is other value.

    To summarize, i will use short statement. Hihi.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    When you use

    PHP Code:
    if($val == true
    it compares the two values first to determine whether they are both the same. If they are then the expression evaluates as true and the following code is run.

    PHP Code:
    if($val
    removes the compare step so that it only needs to see if $val is true or not.

    This does away with the

    (true == true) -> true

    evaluation step and is therefore fractionally faster as well as being shorter code.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •