PHP Strangeness

Okay, so I’m using PHP Version 4.3.8 and I’m happily coding along until I get to this:


$PICS_IMG = 0;
// Special icon
if ($forum_id == 14)
$PICS_IMG = ( $unread_topics ) ? "http://www.googlecommunity.com/f/14n.gif" : "http://www.googlecommunity.com/f/14.gif";
// Default icon
if ($PICS_IMG == 0)
$PICS_IMG = ( $unread_topics ) ? "http://www.googlecommunity.com/f/1n.gif" : "http://www.googlecommunity.com/f/1.gif";

What do you expect to happen if $forum_id is 14? I would expect either 14n.gif or 14.gif, because once that is set, $PICS_IMG will not be 0.

But no! That’s not the case!

The expected behavior (to me) only occurs if I use if ($PICS_IMG === 0) — that is, I add an extra = sign. More info at PHP language comparison operators.

3 thoughts on “PHP Strangeness

  1. if ($forum_id == 14)
    { // Bracket.
    $PICS_IMG = ( $unread_topics ) ? “http://www.googlecommunity.com/f/14n.gif” : “http://www.googlecommunity.com/f/14.gif”;
    } // Another one.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>