I had to evolve myself back to Java, one of the first languages I learned to program in, and boy was that a fallback. Being in PHP so long, used to the OOP capabilities it brings a long, I noticed the side effects of using PHP’s flawed and missing OOP principles.

The project I was working on needed a Java program that communicated with hardware devices under Linux. My main development environment has been OSX + Netbeans for the last year so abusing Java’s statement for being OS independent, I developed the Java program for both OSX and Linux.

One of the requirements the Java program should have was a communication bridge between Java and a Flex program (designated to be the interface). So the first thing we (me and my colleague) looked at was Merapi. Though after some hours of trial and error, when things started to communicate between each other, the work required to get it communicating was just way to much for the little information being passed through Merapi (though Merapi is superb in building a sophisticated communication bridge). So we decided to switch to a more simpler approach, a socket connection.

A socket connection is just simply said “binding a port to a socket to retrieve and send streams of data from and to the designated client(s)”. So being the lazy programmers we are, and having tied deadlines, we started searching for some ready to use Server/Client codes and I started to wonder if any of them even understood the full picture of Streams, Bytes and the Charsets, which at the end is needed to convert those pretty little bytes back to a useful characters. And hopefully the character that was meant to be send from the server and received by the client.
Read the rest of this entry »

You Can Use If, Instead Of count()

Posted: June 28, 2010 in PHP5, PHP5.3

If your using arrays as a collection for a container of data, very few know that count isn’t always needed to know if an array has values or not. Here is a comparison:

$array = array();

// without count,
// shall output nothing.
if($array)
  var_dump($array);

// with count,
// shall output nothing
if(count($array) > 0)
  var_dump($array); 

Both work😉

Why use Smarty? why use Twig? why use TinyButStrong? PHP is a ‘Template Engine’?

We use it to ‘separate PHP from HTML‘, .. or was it to ‘separate business logic and presentation logic‘ .. or .. was it because ‘I’m lazy‘ .. or maybe .. just maybe .. I’m using it because ‘everyone else is using it’.

It doesn’t matter which one you use or for which reasons you use it, as long as you understand the logic of why you use a template engine.

Read the rest of this entry »

You can read all about this approach here: ObjectivePHP

When some method or function call can be specified with alternate options using a one dimensional array, for example:

function whoAmI($iam, $baboon, array('setme' => false, 'else' => true));

A lot of programmers tend to manually check the passed option keys and set it’s value, this is what
I see in a lot of tutorials on many PHP sites:

// function or method
function whoAmI($iam, $baboon, array $options = array()) {
  if(count($options) > 0) {
    $options['setme'] = empty($options['setme']) ? 'some_default' : $options['setme'];
    // rest of the key options ..
  }

  // rest of function or method code
}

Then some programmers comment and say:

The empty is not the correct function to use on set keys. If the value of the key is false, then the return value of empty will also be false, although it should be true. A better solution would be to use the array_key_exists function.

$options[‘setme’] = array_key_exists(‘setme’, $options) ? ‘some_default’ : $options[‘setme’];
// rest of the key options ..

Although the problem with empty on array key/values is correct, the solution is far from better.

Read the rest of this entry »

Update # Some minor mistakes solved, tnx a lot for the emails #

A little guide I’ve made for those who would like to develop with multiple PHP versions on a local server without restarting the Apache server every time you wish to change the PHP version. If you would like to read more about FastCGI, take a look here.

I’m still perfecting this guide bit by bit, any suggestions or comments are welcome.

I’m using Lucid 10.04 (2.6.32-21-generic). This guide would surely work on most Debian based systems. If not, for testing purposes, Sun’s Virtualbox is ideal for setting up a testing environment that equals this guide.

This guide is terminal-based. In Lucid the terminal is bound by default under the shortcut CTRL+ALT+T or located in the menu under Accessories->Terminal

Read the rest of this entry »