Casting arrays as objects (and vice-versa) in PHP
I just learned that it's possible to cast an associative array as an object in PHP.
It works the other way, too—an object can be cast as an array.
This is especially useful when working with the results of database queries. A lot of codebases seem unsure whether to treat database rows as arrays, or as objects. In these environments, it's likely that you'll get one type as the return value from a particular method, but need to pass the other type as an argument to a second method.
So it's nice to have a simple and language-native way to convert between the two. Some simple demo code:
<?php // create an array $array = array( 'prop1' => 'value1', 'prop2' => 'value2' ); // cast it to an object $object = (object) $array; echo 'The value of the property prop1 is: ' . $object->prop1 . '<br />'; echo 'The value of the property prop2 is: ' . $object->prop2 . '<br />'; // cast it back to an array $array2 = (array) $object; echo 'The value for the key prop1 is: ' . $array2['prop1'] . '<br />'; echo 'The value for the key prop2 is: ' . $array2['prop2'] . '<br />'; ?>
Peter Michaux
Douglas Crockford's articles on JavaScript (and his posts at the Yahoo! User Interface Blog) have helped me greatly to understand the more exotic and subtle features of the language, along with the patterns that exploit those features.
Now I've discovered a similar collection of articles by Peter Michaux. They often riff on Crockford's ideas, sometimes offering a fresh perspective and sometimes explaining things in a more accessible way. Well worth a read for aspiring JavaScript ninjas.
Fun with hot dogs!
Check this out:
If you're using any modern browser (that rules out IE6 and IE7), you should
see a picture of a hot dog above.
But no such image file exists on my server—or on any other server. You're looking at a PNG image, but there's no PNG file anywhere.
How is that possible?
Right-click on the hot dog and view the image properties—in particular, note the image's src. You'll see a URI that begins with data:, followed by a very long string of gibberish.
Hiding Wordpress page links
When developing a custom Wordpress theme, it's necessary to provide prev/next navigation wherever there might be more than one page of posts: the main index, search results pages, and archive pages, for example.
posts_nav_link() provides the simplest way to do this—it automatically displays either or both of the links, as needed. If you need more control over the markup, next_posts_link() and previous_posts_link() let you display them independently.
New look! Same great taste!
If you've been here before, you'll notice that things just got a bit spiffier.
There are still some layout quirks in Internet Explorer—I'm working on it.
Let me know what you think!
Check out my new Flash game site
If you find yourself with a few minutes to kill, check out Plastic Lounge, my new Flash gaming site. There are already hundreds of free games available, and I have hundreds more to add as time allows.
Honest feedback is always welcome—it helps me to provide a better site. Hope you find something you like!
The $300,000,000 Button
If you think that usability, information architecture, and user experience design are made-up disciplines for lily-gilding aesthetes, with little practical bearing on your Web site, think again.
Jared Spool explains how changing one button in a site's checkout process increased sales by $300 million per year. (He doesn't name the retailer in the article, but it starts with "A" and ends with "mazon.com".)
Ellipses and interrobangs and em-dashes, oh my!
Typography is one of the most overlooked aspects of Web design—and I'm not talking about fonts, kerning, and x-heights. I'm talking about proper use of glyphs—the individual letters, numerals, and (especially) punctuation marks that make up all written text. There are proper and improper ways to use each glyph—and even large, well-funded websites often get it wrong.
This isn't surprising—even if a site's designer is trained in the specialized discipline of typography (which is unlikely), the person who actually marks up the content (or pastes it into the content management system) probably isn't. But the fact remains that poor typography is one of the most common and most easily avoided blemishes on the web.
Password Security for Web Applications 101
User registration and login is a standard feature on interactive Web sites, and passwords are the standard way to protect those accounts. But a password is only effective as long as it's kept secret—and you might not be doing everything you can to protect yours.
Let's say you've finished building the HTML and the server-side validation code for your registration form, and you're now ready to write the code which stores the user's information (probably in a database). How should you handle the password?
Conditional comments for iPhone
You're probably familiar with IE's conditional comments. They allow us to feed special code to that special browser to overcome its many bugs and deficiencies.
Is there something similar for iPhone? Not exactly, but we can achieve the same end:
<!--[if !IE]><!--> <link media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" rel="stylesheet" /> <!--<![endif]-->
