English flagChinese (Simplified) flagChinese (Traditional) flagDutch flagFrench flagGerman flagGreek flagItalian flagJapanese flagKorean flagPortuguese flagRussian flagSpanish flag
By N2H

Archive for February, 2010

PHP Oddity: Variable Assigned in Expression Cast as Bool

When blog posts here slow down as they have over the last few weeks, it’s a good bet I have my head down building something.

I ran across an issue today that I’d worked around in the past, but had never explicitly investigated. It appears that when assigning a variable within a multiple condition check in PHP, if you don’t explicitly look for a value the first item in the expression is cast as a bool.

I particularly use this technique a lot in CodeIgniter, for example when pulling data from $this->input->post('foo') – a method that will either return semi-sanitized data from $_POST['foo'] or will return false.

I tested this in PHP 5.3 (built-in version on Snow Leopard).

Here is some sample code that demonstrates this issue, and the workaround.

function foo() {
	return 'foo';
}

function bar() {
	return 'bar';
}

if ($foo = foo()) {
	echo '<p>Condition <code>$foo = foo()</code> passed</p>';
}

var_dump($foo);

echo '<hr />';

if ($foo = foo() && $bar = bar()) {
	echo '<p>Condition <code>$foo = foo() && $bar = bar()</code> passed</p>';
}

var_dump($foo);

echo '<hr />';

if ($foo = foo() && $bar = bar()) {
	echo '<p>Condition <code>$foo = foo() && $bar = bar()</code> passed</p>';
}

echo '<p>$foo:<br />';
var_dump($foo);
echo '<p>$bar:<br />';
var_dump($bar);

echo '<hr />';

if (false !== ($foo = foo()) && $bar = bar()) {
	echo '<p>Condition <code>false !== ($foo = foo()) && $bar = bar()</code> passed</p>';
}

echo '<p>$foo:<br />';
var_dump($foo);
echo '<p>$bar:<br />';
var_dump($bar);

Here is the output of this code:

Condition $foo = foo() passed

string(3) “foo”


Condition $foo = foo() && $bar = bar() passed

bool(true)


Condition $foo = foo() && $bar = bar() passed

$foo:
bool(true)

$bar:
string(3) “bar”


Condition false !== ($foo = foo()) && $bar = bar() passed

$foo:
string(3) “foo”

$bar:
string(3) “bar”

Hopefully this will be useful to someone else when they run across this issue.

Know why PHP works this way? Let me know in the comments.

UPDATE: wrapping each expression in parenthesis works around this. See comments for more detailed explanation.

Dougal on WordPress Community Podcast

I was the guest tonight on the WordPress Community Podcast, hosted by Joost de Valk and Frederick Townes. We talked about upcoming features in WordPress 3.0, my past work as a WordPress core developer, my WordPress plugins, microformats, and a smattering of other topics. My interview should be up later tonight.

Joost and Frederick do a great job with the interviews, so if you haven’t already, you should subscribe to the WordPress Community Podcast and give it a listen!

UPDATE 9:30 p.m. EST: The show notes are up now!

Related posts:

  1. How to podcast with an iPhone
  2. WordPress Packages?
  3. Easy Gravatars with Google Analytics Plugin

Thunderbird in 2010

Thunderbird in 2010 by project lead David Ascher, who I met with a few days ago. I’m sticking with Thunderbird for this year, just hoping for some kick-butt Gravatar integration.

Please stop using the same passwords!!!

I strongly agree and have made the same argument numerous times (though haven’t actually done any harvesting). In my opinion the iPhone has been a huge blow to internet password security due to the keyboard set-up. No one wants to go three screens deep to get a special character to log in. I recommend KeyGrinder if you need a PwdHash solution for the iPhone.

# | Visit Site »

HP G62t Series

The HP G62t is a low-price notebook laptop built for everyday use. The operating system here is Genuine Windows 7 Home Premium (64-bit), which is to be expected. There are some choices to make when picking out your G62t, particularly in how powerful you want your IntelCore Dual Core i3, i5 or i7 processor, with GHz ranges from 2.13 to 3.33 (w/ turbo boost). The G62t has built-in Wi-Fi (10/100 BASE-T Ethernet LAN network card w/ RJ-45 connector), and you have the option of getting a built-in 65K modem. The 15.6" LED screen displays at 1366×768 resolution, optimized for watching HD content. Computrace LoJack technology provides theft protection on your G62t. The concensus among reviewers is that the HP G62t offers a nice array of features for the low price.          

Features

  • 1-year limited warranty
  • Optional webcam
  • Graphics card: Intel HD
  • Microphone
  • Bluetooth support
  • 6-cell lithium ion battery
  • Altec Lansing speakers
  • Internet Explorer 8
  • Full-size keyboard
  • Matte surface to minimize fingerprints
  • DVD/CD burner
  • 2 memory slots
  • 8GB maximum memory expansion
  • VGA, RJ-45, HDMI ports
  • 65W AC adapter
  • Norton Internet Security

Feature Choices

  • Dual Core Processor: IntelCore i3-330M (2.13 GHz, 3MB L2 Cache), IntelCore i3-350M (2.26 GHz, 3MB L2 Cache), IntelCore i5-430M (2.26 GHz w/ turbo boost up to 2.53 GHz, 3MB L2 Cache), IntelCore i5-520M (2.40 GHz w/ turbo boost up to 2.93 GHz, 3MB L2 Cache), IntelCore i5-540M (2.53 GHz w/ turbo boost up to 3.06 GHz, 3MB L2 Cache), IntelCore i7-620M (2.66 GHz w/ turbo boost up to 3.33 GHz, 4MB L3 Cache)
  • System Memory: 2GB DDR3 or 3GB DDR3
  • Hard Drive: 250GB 7200RPM SATA, 160GB 7200RPM SATA, 500GB 7200RPM SATA
 

HP Pavilion dv4-2167sb

The dv4-2160us is an entertainment notebook from HP’s mid-range dv4i Series.  The laptop features a 2.26GHz Intel Core i5-430M processor with Turbo Boost technology, 4GB DDR3 memory, and a 320GB hard drive.  For more information see the full HP Pavilion dv4i Series report.

Features/Specs

  • 14.1" Diagonal High-Definition WXGA BrightView Widescreen Display
  • 1280 x 800 Pixel Resolution
  • Intel Core i5-430M Processor (2.26GHz) with Turbo Boost Technology
  • 4GB DDR3 Memory
  • 320GB Hard Drive
  • Intel HD Graphics
  • LightScribe SuperMulti 8X DVD-R/RW with Double Layer Support
  • Altec Lansing Speakers with SRS Premium Sound
  • Connectivity: Ethernet, WLAN 802.11b/g/n
  • Inputs/Outputs: 4 x USB, 1 x HDMI, 1 x VGA, 5-in-1 Card Reader, 2 x Headphone, 1 x Microphone
  • OS: Windows 7 Home Premium
  • 6-Cell Lithium Ion Battery
  • Starting Weight: 5.2 lbs.
  • HP Imprint Digital Plaid Design in High Gloss Mocha
  • 1 year limited warranty
 

HP Pavilion dv4-2160us

The dv4-2160us is an entertainment notebook from HP’s mid-range dv4i Series.  The laptop features a 2.26GHz Intel Core i5-430M processor with Turbo Boost technology, 4GB DDR3 memory, and a 320GB hard drive.  For more information see the full HP Pavilion dv4i Series report.

 

Features/Specs

  • 14.1" Diagonal High-Definition WXGA BrightView Widescreen Display
  • 1280 x 800 Pixel Resolution
  • Intel Core i5-430M Processor (2.26GHz) with Turbo Boost Technology
  • 4GB DDR3 Memory
  • 320GB Hard Drive
  • Intel HD Graphics
  • LightScribe SuperMulti 8X DVD-R/RW with Double Layer Support
  • Altec Lansing Speakers with SRS Premium Sound
  • Connectivity: Ethernet, WLAN 802.11b/g/n
  • Inputs/Outputs: 4 x USB, 1 x HDMI, 1 x VGA, 5-in-1 Card Reader, 2 x Headphone, 1 x Microphone
  • OS: Windows 7 Home Premium
  • 6-Cell Lithium Ion Battery
  • Starting Weight: 5.2 lbs.
  • HP Imprint Digital Plaid Design in High Gloss Mocha
  • 1 year limited warranty
 

WordPress Code Spelunking

Earlier today, I finished up a WordPress plugin that I’ve been working on for a client.  Without divulging too many details, I’ll say that it wasn’t a huge project, nor was it overly complicated, but it was interesting. One fun thing about working on projects for clients, is that it often gives you a chance to explore areas that you might not have looked too closely at on your own.

For example, some of the areas of WordPress (and PHP in general) that I researched and learned more about were:

I used a combination of the links above, other articles I found, plus some good old exploration in the WordPress core to learn more about these topics. Exploring the core code was most helpful in learning about WP_Http, wp_upload_bits(), and some extras for wp_insert_post() that aren’t documented on the Codex page.

Digging around in these corners of the code can be much like spelunking. You’re digging down into places never seen by most people, and often finding little gems that you suspected might exist, but you weren’t sure until you actually saw them. If you haven’t poked around in the core code lately, maybe you should. I usually prefer to use command-line tools like grep and a text editor. But the online WordPress phpdoc xref is a nice way to look at the code, too.

Related posts:

  1. Bug Chasing
  2. Suggestion: WordPress Option Namespacing
  3. WordPress 2.9 Features

HP Pavilion dv7-3180us

The dv7-3180us is a mid-range entertainment laptop from HP’s Pavilion dv7 Series.  The laptop is designed with a large 17.3" widescreen HD+ LED-backlit display that utilizes Nvidia GeForce GT230M graphics for gaming or watching movies.  It is an upgrade to the dv7-3160us since it features a 1.6GHz Intel Core i7 processor with 6GB of DDR3 memory and a 640GB hard drive.  The dv7-3180us offers a full-size keyboard with a 10-key numeric keypad and has integrated speakers with a triple bass reflex subwoofer.   It also includes a Blu-Ray ROM which the dv7-3160us lacks, and has 4 USB ports, an HDMI input, and both Gigabit ethernet and Bluetooth connectivity.  The dv7-3180us comes in HP’s Imprint Espresso finish in high-gloss black and has a suggested retail price of $1450.

Features/Specs

  • 17.3" Diagonal High-Definition HP LED BrightView Widescreen Display
  • 1600 × 900 Pixel Resolution
  • 1.6GHz Intel Core i7-720QM processor with Turbo Boost Technology
  • 6GB DDR3 Memory
  • 640GB HDD, 5400 rpm
  • Integrated Nvidia GeForce GT 230M
  • Blu-Ray ROM & SuperMulti 8× DVD±R/RW with Double Layer Support
  • Full-Size Keyboard with 10-Key Numeric Keypad
  • Altec Lansing Speakers with SRS Premium Sound & Subwoofer
  • Connectivity: Gigabit Ethernet, WLAN 802.11b/g/n, Bluetooth
  • Inputs/Outputs: 4 × USB, 2 × Headphone, 1 × Microphone, 1 × HDMI, 1 × VGA, 1 × ExpressCard, 1 × eSATA + USB Combo, 1 × Firewire
  • OS: Windows 7 Home Premium
  • Weight: 7.74 lbs.
  • HP Imprint Espresso Finish in High-Gloss Black
  • 1 year limited warranty
     
 

I Miss School

Just like they say youth is wasted on the young, I think I squandered school when I was in it. The idea of having no responsibilities except general edification seems like such a luxury now. When I had it all I wanted to do was hack around on the web. Now that the vast majority of my hours are hacking around on the web, it’s a huge luxury to just sit and read for a bit.

Part of that, for me, has been learning how much I don’t know. My search for learning in the past few years is why I’ve attended so many conferences. Events are usually a terrible medium for communicating information, at least how most of them are run, and most of their value is human connections. In the past years I’ve been to a few TED-style ones that were entertaining in their fast-paced format (15-20 minutes per presentation, musical or theatrical fluff to break dense ones up) and the curiosity they sparked by nature of being short and incomplete: TEDMED and EG. The format does become tiresome and exhausting after a while though, too short, and like pizza I appreciate the talks more once they’re on TED.com. (TED has one of the best post-conference experiences, and a big inspiration for WordPress.tv. Also check out FORA.tv which also has amazing content.)

So while events are a brief hit, most of my pleasure from learning comes these days from books and highly interlinked websites. Wikipedia is the canonical example, it can be so blissful to be lost in a web of great content, like a choose-your-own-adventure of information, stumbling from link to link and always ending up someplace you didn’t expect.

I wonder if there could be some sort of metric for writing that told you the ratio of time-to-create versus time-to-consume. On Twitter it’s basically 1:1, you can craft and consume a tweet in a time measured in seconds. For this blog post, it may take me an hour to write it and 5 minutes to read (not skim) it. You can work your way all the way up through 8-10,000 word essays, and books that may take years and years (or a lifetime) to create. The higher the ratio, the more potential for learning and self-improvement. (I wonder how you would measure the Wikipedia which has taken lots of people a little time.) I could easily spend four hours a day surfing hundreds of posts in Google Reader, most of them that took a few minutes to create. It’s a sugar-rush of content that crashes after an hour or two and leaves me empty and hungry. A great novel or book feeds my soul. That’s why I love the Kindle — it has helped me read again.