Support » Plugins and Hacks » PHP help

  • Nearly finished with my heirarchical categories hack, and a little PHP question comes up.

    if ( $text == 0 ) $text = “Default Text”;

    always sets $text to ‘Default Text’
    Apparently PHP considers (“any str” == 0) to be TRUE

    Is there a reason for this behavior, and how should I tell if a variable contains a zero or a a string?

Viewing 8 replies - 1 through 8 (of 8 total)
  • heirarchical categories hack

    Aren’t categories hierarchical out of the box?
    (or did I misunderstand something? and sorry, I am not a coder)

    What I mean is heirarhical selection. Currently category selection is done from a flattened list, showing all categories at all levels. In my case this is somewhere around 2000 categories, and the list is very hard to use. I want to chose from a list of just the top level categories, then from just the subcategories of a single category, etc.

    Well, if you have 2000 cats(!) you should talk to this guy:
    http://wordpress.org/support/topic/69129?replies=16
    who is having problems with only 200 something 🙂

    That number defies the whole idea of “categories”…

    I am kind of pushing the boundary 🙂

    The question is why does PHP think that (“str” == 0) is TRUE? And what do I do about it?

    I guess that’s a question for a specialized PHP forum, wouldn’t you agree?

    Possibly. I sort of hoped this forum would include some who were writing plugins and therefor might have run into this sort of thing. I just found an answer — use the is_string() function (presumably added to PHP because of the broken equality operator.)

    grumble, grumble …

    Are you trying to test for an empty string, i.e.
    if (empty($test)) ...

    What’s the purpose of the test? That’ll help define a solution. Testing against zero isn’t a very good one (though I wouldn’t have thought it would resolve the way it did…). Unless the value of the variable IS actually zero, in which case doing ($test===0) should work.

    -d

    I have a variable $var that is initialised to 0 and somtimes set to a string. I than do:
    switch ($var) {
    case ‘good’:
    ….
    case ‘better’:
    ….
    default:
    ….
    }
    Imagine my surprise when $var is set to 0 and the ‘good’ clause executes!!
    A language that allows a variable to change types really ought to do better.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘PHP help’ is closed to new replies.