SitePoint Sponsor

User Tag List

Page 4 of 4 FirstFirst 1234
Results 76 to 87 of 87
  1. #76
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    '! is for flipping a boolean' - yes...but not always.

    when (!$object) is used, it becomes ($object != null)

    it's just a shorthand method. it is equally as serving, both come to exactly the same answer

  2. #77
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by antirealm View Post
    when (!$object) is used, it becomes ($object != null)

    it's just a shorthand method. it is equally as serving, both come to exactly the same answer
    No - they're not equivalent. For example, if $object is equal to the string "0" these two will give different results.

    PHP Code:
    $object "0";

    var_dump(!$object); // boolean(true)
    var_dump($object != null); // boolean(false) 
    $object != null is a weak comparison of null and $object, and "0" will not match null after type conversion (but "" will).

    !$object casts the object to boolean and then flips it. "0" does match false.

    Off the top of my head I think you would also find that empty($object) and !$object would also be different when $object is a numeric zero string.

    Moral of the story: if you want "0" to count as a non-empty value just like "abc", use empty or null to compare it. If you want it to count as a zero or false value just like the integer 0 or the boolean false, cast it to boolean or integer. This distinction is important when, for example, validating user input where there is a required field (will it mark a single '0' character as invalid or empty?).
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  3. #78
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem with php is that:
    - false, 0 null and "" (empty string) = boolean false
    - anything else = boolean true

    !$object = !(boolean true) = false = 0 = null = ""
    same as
    $object == false = false = 0 = null = ""
    same as
    $object == null = false = 0 = null = ""
    not the same as
    $object === false = false

    Hope this helps

  4. #79
    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)
    That is why you should use ===.

    If you know what you're looking for, you should look for it correctly and unambiguously.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #80
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Type juggling in PHP is a feature, not a bug. If you consider it to be more of a bug, I would say that PHP is not for you.

  6. #81
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vali View Post
    Problem with php is that:
    - false, 0 null and "" (empty string) = boolean false
    - anything else = boolean true
    Not quite - for instance the ascii string containing the single character '0' is boolean false when cast to boolean, but non-null when cast to null.

    The case of $object being the string "0" makes some of your logic invalid, for example:

    $object == false
    is NOT the same as
    $object == null

    It is, if you only consider values like false, 0, null, etc. But not if you consider values like "0".

    Type juggling in PHP is a feature, not a bug. If you consider it to be more of a bug, I would say that PHP is not for you.
    Definitely true.

    And while to some it may seem crazy that "0" behaves differently depending on whether you compare it to zero, false or null, it actually serves a purpose. Depending on the way you use it, "0" could mean the value zero to you, or it could mean a string of one byte.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  7. #82
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look guys here are the facts (IMO):

    // test if we have an object of any class:
    if (is_object($obj))

    // test if we have a valid object of some class or subclass of:
    if ($object instanceof SuperClass)

    // test if a variable is either 0, false, or null:
    if (!$var)

    // test if a variable is an array
    if (is_array($var))

    // test if something is *false* and *false* only (similar deal with true)
    if ($var === false)

    the ! operator can be used to toggle a boolean but it can also be used as a tool to check for 0, false or null

    if (!$boolean)
    if (!is_object($obj))
    if (!$this->checkSomething())
    if (!$count) // if ($count == 0 || $count == null || $count == false)

    i'm sure stricter languages won't allow you to do all of this, but who cares, php was designed to make things pretty simple

    now when someone says they can't do an empty($obj->method()) that's normal because empty() was meant to test a variable, not a value. a better statement would be (!$obj->method())

  8. #83
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can't really simply things in that way. To take an example:

    Quote Originally Posted by antirealm View Post
    // test if a variable is either 0, false, or null:
    if (!$var)
    To say that this "test if a variable is either 0, false, or null" is incorrect.

    This will trigger not only when the variable is 0, false, or null. It'll also trigger when, for example, the variable is "0", or array(), or anything else that when cast to boolean, is false. Maybe you didn't realise it'll include "0" or array() and it introduced a bug into your application.

    The correct way to put it is:

    PHP Code:
    // test if a variable is false when cast to boolean
    // for example 0, false, null, "0", array(), ...
    if (!$var
    the ! operator can be used to toggle a boolean but it can also be used as a tool to check for 0, false or null
    Again, it doesn't check for 0, false or null: it checks whether something when cast to boolean is false. Not just 0, false or null, but also "0", array(), ...

    php was designed to make things pretty simple
    PHP allows you to do pretty powerful things in intuitive ways. It does not, however, make things all that simple. You still need to understand the underlying data types to realise exactly how (!$value) and ($value == null) are different and which one you should use when your users might enter "0" in a form field, or how to know what happens to either when $value is an empty array, etc.

    The is_* functions (apart from is_numeric) are all pretty straightforward, as is the === operator. They all focus on a certain type, and that type only. One you get into the == operator and implicit type casting required, it becomes more complicated to understand because there are a lot of different behaviours for different types.

    Check out these tables, particular the "loose comparisons with ==" table:
    http://au2.php.net/manual/en/types.comparisons.php
    Can you really memorise that whole table and use it in your day to day coding? I would say you really need to know how variables cast to other types.

    Plenty of info in the sub-pages of:
    http://au2.php.net/manual/en/languag...e-juggling.php


    Mods: i realise this is going way off topic - i guess this discussion could be split to a separate thread.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  9. #84
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmj View Post
    The case of $object being the string "0" makes some of your logic invalid
    And that is why I called it a "problem".
    0 = number 0
    '0' = "0" = string of zero

    I typed there the number 0, and some people understood the string 0.

  10. #85
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i didn't know, or forgot that strings and arrays casted that way to a boolean. thanks for the reminder/new info.

    however i would NEVER use "if ($str)" to check for a valid string value. same goes for an empty array().

    i would use either "if (is_string($var))" or "if (strlen($string))" depending on the context of the problem.

    for an array it would simply be: "if (is_array($var) && count($var))". trying to do "if ($array)" to check for the count of an array is obviously prone to errors..

    essentially i would never try and cast a string to a boolean, or cast an array to a boolean, when i can just use string or array functions. i would only cast to boolean if i wanted to check for a numeric value, or to check that a variable intending to hold an object holds an object. as far as i know an object cast to a boolean always returns true, it appears that link doesnt provide information on this.

    if you are in a situation where you don't know whether your *number* will come in as an int or a string, and you definetly want an int, then always cast to an int, it's that simple.

    PHP Code:

    $alwaysAnInt 
    = isset($_COOKIE['value']) ? (int) $_COOKIE['value'] : 0// if we had "123abc" it would be 123 and if we had "abc123" it would be 0. [1]

    if ($alwaysAnInt// int to bool conversion no matter what
       
    $x 10 $alwaysAnInt// never = 0, "0", null, or anything like that 
    i guess it boils down to software engineering pratices. it seems to me that it's better to right deterministic code, code that does it your way and is not fragile.

    [1] http://au2.php.net/manual/en/languag...ing.conversion

  11. #86
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vali View Post
    And that is why I called it a "problem".
    0 = number 0
    '0' = "0" = string of zero

    I typed there the number 0, and some people understood the string 0.
    Actually what I meant was the other way around, I was not complaining because I thought you included the string "0" and shouldn't have; I saw that you didn't include it, but you should have.

    For the purpose of evaluation as boolean, there is no distinction between the string "0" and the integer 0. They both evaluate to false. That is, the part of your post saying "false, 0 null and "" (empty string) = boolean false; anything else = boolean true" is wrong because, because it failed to take into account other values, like the string "0" or an empty array, which will evaluate to boolean false. This distinction is important in some situations. Like when the value you are comparing might actually be the string "0" or the empty array.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  12. #87
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    indeed i was wrong there


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
  •