Tag archive: PHP

Yoda conditions

Because I usually work with WordPress, and because coding standards are a good way of avoiding endless personal decisions that then get in the way of collaborating, I tend to follow the WordPress coding standards. You should too.

Now, there’s something that I’ve noticed when looking at core WP code, which I’d always assumed was some idiosyncrasy that the WP had settled on arbitrarily. It’s where condition expressions are written with the value being tested first, then the variable against which it’s being tested:

if ( 'value' == $var ) { [do stuff...] }

I just thought that was weird, and in the interests of readability I’ve always been doing it the other way around.

Well it turns out there’s a very good reason for this style, and they call it Yoda conditions. The reason is something we’ve all done – typing a single ‘=’ instead of a double one (‘==’). What happens then (with $var = 'value') is that the value isĀ assigned to the variable, and the expression will return the assigned value. So the supposed ‘test’ will always return something equivalent to boolean false, or true, depending on the value (in this case, any string would mean the expression evaluated to true). This can be damnably hard to debug.

So, switching the order means that if you mistype the assignment operator, the mistake will result in an attempt to assign a variable to the value – which isn’t going to happen. So the error is thrown in the right place, and you don’t lose time searching for it.

The weirdness contains wisdom after all.

A little bizarre, it is, to read. Get used to it, you will.

(Credit to the guy from the WordCamp London after party last night! I forgot your name but the tip made it.)

WordPress plugin to help repair serialized arrays in custom fields

I just launched a project, and despite using a heavily tried-and-tested script for dealing with serialized data in migrating, we ended up with some custom field values that weren’t being output (in admin or on the front-end) because the serialized values had become corrupted. I think it must have been something strange to do with characters pasted in from Word, maybe in conjunction with the recent WP core upgrade to handling utf8mb4 data. I’m not sure.

In any case, I found a PHP script that repairs serialized data, and turned it into a plugin: Repair Meta.

Read more »

Formatting a string with optional elements

I’ve often found myself writing little bits of code to format strings with optional elements. The classic example is outputting the company someone works for, together with their position in that company—if the position is given. The format if both are given is:

[position], [company]

If the position isn’t given, you just output the company. Without, obviously, the comma. It’s a trivial thing to code, but on a current project I’ve found myself doing it in several different places. Of course, this suggests there should be a function written for it.

Ideally the function should be able to cope with any number of optional elements. With PHP’s func_get_args(), it’s easy:

function formatOptionalText( $delimiter ) {
	$elements = array_slice( func_get_args(), 1 );
	$text = "";
	$i = 0;
	foreach ( $elements as $element ) {
		if ( trim ( $element ) ) {
			if ( $i && $text ) $text .= $delimiter;
			$text .= $element;
		}
		$i++;
	}
	return $text;
}

Note that you have to pass a delimiter first, and you should include a space if necessary (e.g. ", "). After that, pass any number of strings and they’ll be output without gaps.

WordPress dashboard widget for PHP errors log

Jeff Starr recently posted on ways to monitor PHP errors. For some reason his method using WordPress’s wp-config.php file didn’t work for me, but I got the .htaccess version working OK.

I thought I’d knock out some code to display the latest errors as a widget on the WordPress dashboard.

As usual, this code is provided “as is”—use as you will, and let me know about any problems, but I can’t really offer support to people who aren’t comfortable with WordPress theme development. If anyone finds the time to wrap this up into a user-friendly plugin, do let me know (likewise if there’s already a plugin that does this better—I’ve not looked properly!).

Read more »

Complete archive

Main index