Friday, July 29, 2005

 

PHP Pop Quiz

Quick! What is the output of this PHP script?

[jason@jpc ~]$ cat example.php 
<?php
        $strings = array("/bin/baz", "/usr/bin/foo", "/etc/goober.conf");

        foreach($strings as $string) {
                if(strpos($string, "/bin") == 0) {
                        echo "'$string' begins with /bin\n";
                }
        }

        for($i = 0; i < 5; ++$i) {
                echo "Number: $i\n";
                sleep(1);
        }
?>
[jason@jpc ~]$ php example.php 
'/bin/baz' begins with /bin
'/etc/goober.conf' begins with /bin
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
^C
[jason@jpc ~]$

What happened?

If you read the documentation for strpos, you see that it returns boolean false when no match is found. Due to PHP rather loose ideas of truth, the statement "false == 0" evaluates to true. You have to use the stricter "===" operator.

But what's wrong with the loop? The problem is the condition, "i < 5". This obviously should be "$i < 5". PHP sees the "i", looks for a constant named "i", and when it doesn't find one, it assumes you meant a string containing the name of a the constant. PHP would issue a warning when doing this, if E_NOTICE warnings were enabled, which aren't by default. So have have the string "i" in a comparision, when PHP then converts to a number. And what does PHP do with strings that obviously aren't numbers? It just uses a zero. So PHP tests for "0 < 5", and we have an infinite loop.

Moral: Always have all warnings enabled in php.ini, and read the docs carefully. (Or, if the option presents itself, don't use PHP.)


Sunday, July 17, 2005

 

The right tools

When I first installed Linux, I cast about for a good email client. The text-mode ones seemed too complicated. I just wanted to read mail. I can't remember exactly what I tried first. It may have been Mozilla Mail. At some point, I tried Evolution. But they all seemed too slow. Then I found sylpheed, which is pretty snappy compared to Mozilla Mail and Evolution. I used that for about two years, then, due to the way life works, I was without a Linux box for about a month, so I got my mail using gmail's web interface. The best thing you can say about gmail's interface is that it's better or at least as good as other web interfaces. The worst thing you can say about gmail's web interface is that it's a web interface.

So, once again finding myself in a real OS, decided it was time to try mutt. After throwing fetchmail, procmail and msmtp I was ready to go. Well, not quite. Ready to configuring everything, figure out why msmtp isn't sending, get procmail sorting the way I want, and then ready to go. Having only used mutt for an hour or two, I'm obviously not as adept at reading mail in it as I would be in sylpheed, or some other graphical mailer. But I can see how powerful it is, and I can see how, in the very near future, the time I spent setting it up will begin to pay itself back.

A similar thing happened to me when it came to choosing an editor. Emacs and vim were scary, so I used mcedit, a little "just press keys and they go onto the screen" type editor bundled with Midnight Commander. And that was okay. But then I took the time to learn vim, and I can edit much faster now than I could mcedit.

All this to say, the best tools usually have the steepest learning curves. Be nice if they didn't, but that seems to be the way it is. So don't be afraid to learn.


Saturday, July 16, 2005

 

Linux and the greater good

There's yet another story about how there's too many Linux distributions, too many toolkits, too many packaging systems, and if people don't start cooperating, Linux will never succeed on the desktop, etc.

Notice how people talk about this: "Linux" (ie, the tens of thousands of developers worldwide writing the software that together make up what we call a Linux system.) should have this, "Linux" should have that. Fewer, more powerful distros, tools to do what I want. People who talk likes this are essentially saying to people working for free: "Write my code". If you don't like it, shut up and code.

Secondly, is having lots of distributions really all that bad? For broad view of this, see Rick Moen's excellent essay Fear of Forking. But in the case of distributions, do you ever hear anyone talking about how great it was back when you only could use SLS? I wasn't using Linux then, so I googled to make sure I was right about SLS really being the only game in town early on. I found this LWN article on early Linux distributions. Notice this bit:

SLS dominated the market until the developers made a decision to change the executable format (if you remember the a.out to ELF conversion you'll remember this). This was not well received by the user base. Just around the time this happened Patrick Volkerding had taken SLS and adapted, modified, tweaked and cleaned it up making it a different thing all together. He called it Slackware. With the unpopular direction SLS had taken, Slackware quickly replaced it and became the dominant distribution used by nearly everyone. In fact it's still in use today.

What was, in essense, the first distribution, forked because people didn't like it. People didn't like it, so they changed it, and made it their own. So, in what for the lack of a better word I shall call the "mind" of a Slashdotter, what should have been done? Indeed, would they have supported Linux in the first place? Or would they have said, "There's enough Unixes. Don't make another."?


Monday, July 11, 2005

 

The other Jason Creighton

I did an ego search on Google and found this guy. He's an aspiring musician with a frame-based website.

 

Monitor specifications and common sense

So I'm trying to find the model name/number on the el cheapo 15" monitor I'm using right now. It says "Mag Innovision" on the front, and the back of the monitor is completely blank. Nothing about FCC compliance, no manufacture date, nothing. They decided to, in a moment of brilliance, put the serial numbers and whatnot on the bottom of the monitor. So to see the model number, you have to turn the monitor around, and tilt it at about a 45 degree angle to read it. Turns out it's a "DX15F". Great, I google for it, find the specs no problem, enter them into xorg.conf, and away I go.

Then it hit me. Why didn't they just print the horizontal and vertical sync ranges on the back? Heck, why not the front? Didn't anybody think of this? Did somebody say "Hey, you know what would be good? If we printed the specs right on the monitor!" and then they didn't do it? This isn't rocket science.

I realize this is a non-issue with modern monitors, as they have some magic way to tell the video card what they support. Come to think of it, I don't even know what that standard is called. It's invisible. It just works. In my experience, anyway. I'm sure lots of people have horror stories to tell. But it's a lot better than having to plug in the numbers yourself, or else use some flickering VESA mode.


This page is powered by Blogger. Isn't yours?