Category Archives: PHP

Crushing Code and Shaving Chars

Who has heard of Code Golf? I hadn’t up until about 3 years ago when I first stumbled across some on Stack Overflow. I faintly recall the horror as I gazed upon some of the most cryptic code ever – somewhat akin to the feeling I had the first time I was shown a regular expression.

According to Wikipedia, “Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible code that implements a certain algorithm.”

Now I would never claim to be able to hold my own against some of the veteran golfers, however I do find myself growing more and more interested in how I can shave off a few bytes here and there. Such was the case today when I set out to answer a question on Stack Overflow.

The OP was asking how he might take a single-dimension array of words, sort them alphabetically, and then print them out. Each new group of words, categorized by their first letter, should be preceded by that letter. So you would first see “A”, followed by all “a” words, then “B”, followed by all “b” words, and so on. I’ve personally wanted to do this in the past myself.

After a few minutes I had posted the following:

$firstChar = ''; 
sort( $aTest );

foreach ( $aTest as $word ) {
  $firstChar = substr( $word, 0, 1 );
  if ( $firstChar != $currChar ) {
    $currChar = $firstChar;
    echo $currChar;
  }
  echo $word; 
}

This is ugly, but fairly straight-forward. You cycle through the array, printing out the first letter of the current word whenever it differs from the last iteration. Simple enough. But then one of the members pointed out via comment that instead of writing

substr( $word, 0, 1 );

I could just access the first letter as though the word itself were an array:

sort( $aTest );

foreach ( $aTest as $word ) {
  if ( $word[0] != $currChar ) {
    $currChar = $word[0];
    echo $currChar;
  }
  echo $word; 
}

Now it had been a while since I did that. So I was pleased to be reminded of this. Then that little spark of brevity begin to grow, and I wanted to see where else I could shave off some chars. My eyes were then drawn to the assignment and echo portion:

$currChar = $word[0];
echo $currChar;

That would be shortened so that the assignment and echo take place all at once:

sort( $aTest );

foreach ( $aTest as $word ) {
  if ( $word[0] != $currChar ) {
    echo ( $currChar = $word[0] );
  }
  echo $word; 
}

So now it’s down to a single if statement, and a trailing echo. I wondered if I could just load this up into a ternary, so I set my hand to do that:

sort( $aTest );

foreach ( $aTest as $word ) {
  echo ( $word[0] != $currChar ) 
    ? ( $currChar = $word[0] ) . $word 
    : $word; 
}

At this point, I was getting a little OCD about my second and third expression. They seemed unbalanced. The second one was setting the new current category letter, as well as echoing out the word. The third one was just echoing out the word. This is when my mind returned to something Brendan Eich called an “abusage” – using && in somewhat of an if-statement fashion.

$cond && ($foo = 'bar');

The second part is only evaluated when the first part is determined to be true. If the first part is false, the operation short-circuits and the second part is never touched. I realized I could use this to test the equality of the current category letter and the first char of the current word. If they were not equal, I could use the right hand side of the operator to record the new letter:

sort( $aTest );

foreach ( $aTest as $word ) {
  echo ( $word[0] != $currChar ) && ( $currChar = $word[0] ) 
    ? $currChar . $word 
    : $word; 
}

Awesome. Looking very slim. Now that I have only one statement within my foreach loop, I can strip away the curly braces. I can also reduce even more space by moving away from long variables names. Instead of “word”, I can simply use “w”. Instead of “currChar”, I can use “l” (for “letter”). I can also collapse my ternary operator onto one line:

sort( $aTest );

foreach ( $aTest as $w ) echo ( $w[0] != $l ) && ( $l = $w[0] ) ? $l . $w : $w ;

Nearly there. Last we can strip out all of the white space possible. Unfortunately with the syntax of the foreach, we cannot remove the spaces around the keyword “as” in the condition.

sort($aTest);foreach($aTest as $w)echo($w[0]!=$l)&&($l=$w[0])?$l.$w:$w;

In the end we’ve reduced about 195 characters and 11 lines down to 1 line and roughly 70 characters! Granted, that’s nothing compared to what others have acheived, however it’s awesome to see this solution iteratively collapse before my very eyes.

With some clever substring alternatives, ternary operators, and hacking the way logical operators work, we’re able to reduce a dandy solution into a horrific and cryptic trace of insanity. I don’t suggest you actually produce code like this when you ship (outside of the obvious minification), as it’s far better to write readable code than it is to write short code.

In the end, I was able to switch away from the original array declaration and use the deprecated split function to save even more bytes:

$a=split(",","apple,pear,banana,kiwi,pineapple,strawberry");
sort($a);foreach($a as $w)echo($w[0]!=$l)&&($l=$w[0])?$l.$w:$w;

So how about you? Do you engage in this type of behavior? Have any tricks of the trade to share? Place them in the comments below! Or perhaps you can shorten my code even further – I’d love to see what you come up with!

n Parts for Sum in PHP

I was bouncing around on Stack Overflow, answering questions here, voting on questions and answers there, when I came across a question on how to construct an array of arbitrary size, whose members add up to an explicit sum. This sounded fun, especially since math has always been an area I’d like to see some personal growth in.

So I set out to answer the question, jotting down some pseudo-code in notepad, and working through some functional portions on writecodeonline.com/php and coderpad.org. After about 10 minutes or so I felt as though I had a good solution for the OP (original poster), so I clicked back over to my Stack Overflow tab. To my terror, the question had been closed as a duplicate.

Now what? Did I just waste all that time and brain-power constructing a solution that I cannot even share? Meh, screw it, I’ll share it on my blog!

My first step was to build up an array of values that were each a fraction of the sum, rounded down to keep away from decimals. This would, of course, leave me with a slightly larger number in the end if the sum isn’t equally divisible by the expected number of parts. That’s okay, I’ll just push that last number onto the end of the array.

$sum = 200; 
$parts = 10; 
$valus = array();

for ( $i = 0; $i < $parts; $i++ ) {
  $num = $i < ( $parts - 1 )
    ? floor( $sum / $parts )
    : $sum - array_sum( $valus );
  array_push( $valus, $num );
}

Awesome. This results in an array of values that are almost entirely the same number, with the exception of the last index, which will be slightly larger when the sum doesn’t divide equally.

Next I need to randomize the values. I figured I would cycle back through and have some of the values share the wealth, so to speak. Each number would have a random portion of itself donated to a random sibling. At the very least, any particular number could be reduced to 1, but no lower (more on that in a bit).

foreach ( $valus as $key => &$value ) {
  if ( $value == 1 ) continue;
  $rVal = rand( 1, $value - 1 );
  $rKey = rand( 1, $parts );
  while ( $rKey == ( $key + 1 ) )
    $rKey = rand( 1, $parts );
  $value -= $rVal;
  $valus[--$rKey] += $rVal;
}

That’s it! (KEEP READING! One important change yet to be made) Tell it how much, and how many, and it builds an array of randomized values for you. One run of the code against the above settings resulted in the following output:

Array
(
  [0] => 26
  [1] => 59
  [2] => 4
  [3] => 61
  [4] => 35
  [5] => 1
  [6] => 2
  [7] => 7
  [8] => 1
  [9] => 4
)

One interesting thing I noticed after toying with the input was that if your sum is less than your number of parts, the code will at times provide a negative number in order to provide adjusted values for each key. For example:

$sum = 2;
$parts = 5;

Produced the following results after one run:

Array
(
  [0] => -1
  [1] => 0
  [2] => -1
  [3] => 3
  [4] => 1
)

Count it up, -1 + 0 + -1 + 3 + 1 indeed equals 2. Of course this wasn’t at all what I wanted. But it did cause me to consider what would happen if somebody wanted more parts than their provided sum. This lead me to return to my share the wealth portion and rewrite the first condition:

if ( $value <= 1 ) continue; // change == to <=

Now, whenever our current value is less than or equal to 1, we skip it. It is possible for the value to be zero already, because we floor the results of the division in earlier code. If the sum is less than our parts, that result will be a fraction, which we promptly round down to 0. This will now prevent the code from further reducing the value from 0 into negative numbers. In cases where our sum is much larger than our parts, we don’t want to borrow so much from one key that we leave another with 0. This is why I skip if the value is already 1 as well.

So asking again for the following:

$sum = 2;
$parts = 5;

Will output the following array (or one similar to it):

Array
(
  [0] => 0
  [1] => 0
  [2] => 1
  [3] => 0
  [4] => 1
)

prependChild on DOMElement in PHP, Sort of.

This evening I wanted to add a new LI onto a WordPress navigation programmatically. After considering a few different options, I decided I wanted to use PHP’s DOMDocument, and use this as an opportunity to more familiarize myself with its ins and outs.

Looking into DOMElement, I was pleased to see there was an appendChild() method, but a little down to see there there was no prependChild(). After searching online, I saw a few people creating custom classes to make their way around this absense, however I found a method that I prefer a little more.

Stumbling upon insertBefore(), I realized all of my problems had been solved…kinda. insertBefore() was leaving me just a bit confused; it sounds as though you call it from the element you wish to move around, but you don’t. In fact, you don’t even call it on the element you’re trying to snuggle up against. Instead, you call it on the parent of the two, referencing both elements as the arguments.

add_action( 'wp_nav_menu', 'custom_nav_menu' );
function custom_nav_menu( $menu ) {
  // Load it
  $html = new DOMDocument();
  $html->loadHTML( $menu );
  // Get it
  $list = $html->getElementsByTagName( 'ul' )->item( 0 );
  $first = $list->getElementsByTagName( 'li' )->item( 0 );
  $new_item = $html->createElement( 'li', 'Hello World' );
  // Place it
  $list->insertBefore( $new_item, $first );
  // Show it
  return $html->saveXML( $list->parentNode );
}

This gave the desired results, without requiring any new classes or other more complicated work-arounds. To my knowledge I don’t see any downfalls of it either, at least not at this time.

Some of you may be scratching your head at the last line, where I’m calling saveXML() rather than saveHTML(). As for 5.3.6 you can pass in an optional DOMNode to specify which portion of the DOMDocument you’d like to render. I happen to be using 5.3.5, so this option is not available to me and all I got to see was a nasty error message. Fortunately saveXML() worked as a nice substitute with little-to-no side-effects.

code-igniter-logo-black

Todays WTF Moment with CodeIgniter, MySQL and PHP

As routine as an oddly-twitching finger sabotaging any attempt to use your mouse, strange things  may take place with your environment, tools, or code. Today I was working on setting up a very small project with CodeIgniter when I came across the task of establishing database credentials so I could auto-load my resources and have a fresh connection waiting on me hand and foot.

Opening up application/config/database.php I went ahead and placed my hostname, username, password, and database name. Almost done, right? Next I went to application/config/autoload.php and updated the $autoload[‘libraries’] array to include “database”. Done, right?

To my surprise I had a whole storm of agony just waiting on me to try and pull some results from my model. When I did just that…

This took me back just a moment – I had put in the right data, right? I’m working on “localhost”, and my db user is “root” with a blank password. How exactly do you screw that up? After double-checking, my credentials showed to be valid. So I went to make sure I was auto-loading the database properly; perhaps I said “datbase” instead of “database”? Who knows, it’s happened before. Nope, I had this value correct as well.

It’s around this moment Google must necessarily be opened and error messages must necessarily be pasted into the search bar. Anytime I have problems like these, which turn me to Google, I’m reminded at just how unreliable forums are. I inevitably ended up looking at about 12 different forums, where people can post whatever the flip they think might be the problem and you have very little reason to doubt them in your ignorance.

“Did you provide your port number?” “Did you update your hosts file to associate ‘localhost’ with 127.0.0.1?” “Did you max out your connections?” All of these where the types of questions being asked, and yet none of them did me any good. My host file is fine – I run plenty of websites. I’m not maxing out my connections either – I’m the only sap accessing the laptop! It wasn’t until I came across a PHP bug filed in 2008 regarding PHP 5.3’s inability to use MySQL functions when using “localhost”.

Now, this bug submission was eventually marked as “bogus,” but it wasn’t entirely useless because it got me to explore another route in my troubleshooting. Out of curiosity I looked down to my instance of WAMPServer to see what version of PHP I was running; 5.3.1.

Out of curiosity, I switched to 5.3.5. My server went offline, restarted, and was back up and running in just a matter of seconds. Time to try my CodeIgniter app again…Bingo!

The connection issues were resolved by switching from 5.3.1 to 5.3.5. Just for some environmental facts and info, I’m running WAMPServer 2.0, Apache 2.2.11, MySQL 5.1.33 and PHP 5.3.1 (Well, 5.3.5 now). The CodeIgniter version is 2.0.2.

At this point, I still have no idea what causes this problem but I am sure glad it’s done with so I can get back to work. If you’re having a similar problem, on a similar setup, perhaps this will help you as well.

If anybody knows what actually causes this problem, I’d love to know in the comments below.

ternary

A Shorter Ternary Operator in PHP 5.3

The ternary operator is simply awesome. It allows for some really beautiful if-else style variable assignments:

$can_drink = ( $age <= 21 ) 
  ? true 
  : false ;

This operator let’s you evaluate a condition, and return one of two results based on that condition. In the example above we’re testing the legal drinking age. If the age is greater than or equal to 21, the value of $can_drink will become true (the first value after the question mark). Otherwise, the value of $can_drink will become false (the value after the colon) (this particular example doesn’t require a ternary operator as the condition evaluated will itself result in either true or false). You can test multiple conditions within the main ternary condition, but the rules still stay the same – if the main condition evaluates to true, the first result is returned, otherwise, the second result will be returned.

The operator is nice, don’t get me wrong, but it’s not always fun to play with. Suppose you’re searching for a value within an array and you would like to return it if found:

$job_title = array_search( "Jonathan Sampson", $emp )
  ? array_search( "Jonathan Sampson", $emp )
  : "n/a" ;

You’ll note that my white-spacing is a little different, but this won’t effect the results of the operator. What I’m doing here is checking to see if “Jonathan Sampson” is a value within the employees array. If “Jonathan Sampson” is in the array, I’d like to return the key for that entry, so I replicate the condition (which returns the key) in the first slot after the question mark so it will be returned if the condition is true. Otherwise, if “Jonathan Sampson” isn’t found in the array (and the condition returns false) I return the string “n/a”.

Either the $job_title variable will have a value like “Software Developer” (given the key for “Jonathan Sampson” is “Software Developer”) or it will be “n/a”. The problem here is that we’re performing the array search twice, which is really ugly. There’s another way we could do this:

$job_key = array_search( "Jonathan Sampson", $emp );
$job_title = $job_key ? $job_key : "n/a" ;

Now we’re only performing the array search once. The array_search function returns either the first key found, or FALSE. As such, $job_title will contain either the key, or the string. But this is still a bit ugly. We’ve got two lines, and repeated calls to the $job_key array.

In PHP5.3 we’re given the option of a much shorter ternary, eliminating the middle column completely. This is helpful when using functions like array_search() which themselves return the result we actually want. They serve both as the condition (since they will return FALSE when the value isn’t found) and the result (since they return the key if the value is found). Now we can do something like this:

$job_title = array_search( "Jonathan Sampson", $emp )  ?: "n/a" ;

Now, if array_search() finds the key, it’s automatically returned into the $job_title variable, else, “n/a” will be returned. Elegant.