Monday, February 20, 2006

More formatting tips

I just answered an email from a friend who had a problem getting the formatting with printf() to work properly. Using printf() and sprintf() can be a little tricky at times so I've made a page with 3 examples of using printf() to print out some formatted text. I'll give you the URL shortly. For now, let's look at John's problem:

(From the email I received)
"I've written a simple little program that outputs
the following:

Free space on drive C: 103.67 GB
Free space on drive C: 106154.02 MB
Free space on drive C: 108701716 KB
Free space on drive C: 111310557184 bytes

Now what I would like for it to do is look like this:
Free space on drive C: 103.67 GB
106154.02 MB
108701716 KB
111310557184 bytes

... instead of getting the above I get this:

Free space on drive C: 103.67
106154.02 MB
108701716 KB
111310557184 bytes

How, then, can I easily achieve the effect of printing
blank space when it must precede anything else on a
line of PHP output?"


The answer, while simple, isn't obvious at all. Here's what happens: The printf() statements he used were correct but the browser ignored the extra spaces. Why? Because HTML only shows one space where it encounters several together - unlesss you specify that you want things another way.

The <PRE> ... </PRE> does that for you. By default, you'll get small text in a monospace font. If you want to change the appearance (size, etc.) you can put a <FONT> ... </FONT> in between the <PRE> ... </PRE> tags.

Like this: <PRE><FONT size=4> ... </FONT></PRE>

Be careful not to use a proportional font. If you do, the characters will have different widths and the spaces won't be as wide as you expect. You'll see how bad this can look when you go to my example page.

I took the example from php.net and made a page that shows the code and the output in three different variations. Two of them look OK; one looks awful.

OK, I've kept you in suspense long enough. Here's the URL:
www.learntousephpintwohours.com/monkey_printf.php

Happy Coding!

Formatting output

PHP was designed specifically for the World Wide Web and its screen output is formatted as HTML. If there's an exception to that, I haven't seen it yet. But HTML doesn't always appear on the screen the way you want it to. Well, not without a little help, anyway.

Let's say you wanted to put a specific number of blank spaces in your output. (Don't forget that the width of a space isn't always the same unless you specify a monospace font...). The problem with most HTML code is that it converts a string of spaces to a single space unless you tell it otherwise.

There's a tag you can use for "preformatted output" that will override the tendency of HTML to eat the extra spaces. Here's an example where I'm showing a script and I want the indenting:

<PRE>
function runForever() {
if(6 == 9) {
print("Warning! Infinite loop");
}
}</PRE>

Everything between the <PRE> and </PRE> tags has the same spacing and line feeds as it would have coming out of a word processor or typewriter. To be able to show you the <PRE> and </PRE> tags themselves, I had to enter them using HTML entities so they wouldn't be interpreted as tags!

Other ways to format text on the screen include using printf() and sprintf() just like in the C language. You can make numbers and text appear in a field of a certain width, choose a character to pad out the unused spaces and make your numbers appear as integers(binary, decimal, hex or octal), doubles (floating point values such as 3.14159) or doubles in scientific notation.

Text can be right- or left-justified and you can pass ASCII codes for characters and get the characters on the screen. Full documentation and a lot of good examples can be found here:

http://us3.php.net/manual/en/function.sprintf.php

Happy Coding!

Sunday, February 19, 2006

Beware of infinite loops

Loops are a great way to get a repetitve task done in a PHP script. There are several kinds of loops: for-loop, foreach-loops, while-loops and do .. while-loops. The all have one thing in common - a stop condition.

A stop condition is an expression that, when it evaluates to true (or any non-negative number), will stop the loop from executing.

Here's a typical for-loop:

for($c=0; $c<10; $c++) {
# do something here
}

This loop will run ten times while $c goes from 0 to 9. Now what would happen if you wrote a stop condition that was never reached? You guessed it. The loop would run forever. Here's an example:

for($c=0; $c<0; $c++) {
# do something here
}

Here, $c gets bigger with each pass through the loop but the stop condition is that $c must be less than zero. That's never going to happen and the loop never quits. Why? Because $c starts at 0 and gets increased by 1 on each pass through the loop.

What about a while-loop? They look like this:

$c = 0;
while($c < 10) {
# do something
$c++;
}

This loop will stop when $c gets to 10. Now suppose you made this subtle typo:

$c = 0;
while($c < 10) {
# do something
$C++;
}

$c never changes! $C keeps getting bigger but this loop doesn't check for that.  So the loop never stops.

If a script seems to be "stuck" and not doing anything - or just not doing what it should be doing - force it to stop, then look for infinite loops. If you see one you're not sure is messed up, try putting an echo() line in the loop body so it prints something to the screen each time the loop executes. You'll see right away if you've found the problem because your screen will fill up and keep growing.

Happy coding!