Tag Archives: php

Valid Syntax errors: Part 1

It took me about an hour to spot a strange behaviour in my PHP skript:

Have a look at this code:

$foo = array ( 'a', 'b', 'c' );
foreach ( $foo as $bar );
    echo $bar . "\n";
// Expected Output:
// a
// b
// c
// Actual Output: 
// c

What happened?

Would I have used phpcs it would have been obvious. I used a semicolon instead of a bracket after the foreach statement.

So instead of calling the echo command with each iteration the foreach silently iterated through $foo and assigned $bar on each iteration. That left $bar with the last value of the array that gave the strange output.

I can not actually remember whether there had been closing brackets but I did not get an Error regarding that.

phpcs would have detected the missing brackets — if that would be observed by the used sniff.

More on phpcs later.

unserialize values

Last week I had the problem, that – due to a wrong database-entry – a serialized value got scrambled. And due to that (and some missconfiguration of the server) the application broke. Instead of a search result the user simply got an error message about not being able to unserialize the value due to an offset-error.

So how could I avoid the error (which actually is a notice) being thrown and besides that get to know whether unserializing worked or not?

The hack I finaly came up with is as follows:

  • suppress the notice in any way
  • check whether the returned value is a boolean false and the given string is ‘b:0;’ (which represents a boolean false in a serialized string).
  • If so, the unserialize() failed, so lets throw an InvalidArgumentException

And here it is:

public function my_unserialize ( $value )
{
    $unserialized = @unserialize ( $value );
    if ( false === $unserialized && 'b:0;' !== $value ) {
        $le = error_get_last ();
        throw new InvalidArgumentException ( $le['message'] );
    }
    return $unserialized;
}

Quality-Assurance with PHP – Unit-Testing

Unit-Testing

There has been much said about the advantages or disadvantages of unit-testing. So I will not recap on that.
But where to go after you have choosen to use unit-testing?

The following short tutorial will give you an ovberview of how to install the necessary software, how to come to unit-tests from your requirements and how to write your tests and your code simultaneously.
Continue reading Quality-Assurance with PHP – Unit-Testing

fileinfo for PHP on Leopard

Currently I had to use the fileinfo-Package on my development-system which is a MacBook Pro running MacOS 10.5 with Marc Lianages PHP-Package.

But sadly that does not contain the fileinfo-extension.

No Problem I thought: Installing a PECL-Extension isn’t hard work.

At least if it works….

Of course I ran into the problem that I couldn’t compile the fileinfo extension due to the following error-message

configure: error: Please reinstall the libmagic distribution
ERROR: `/private/tmp/pear/download/Fileinfo-1.0.4/configure' failed

I can’t be the only one that ran into the problem, I thought. And so it was.

While searching the internet, I generally found two ways to solve the problem.

  1. Use PHP 5.3, that already contains the package
  2. Install a complete second OperatingSystem beneath a perfectly working MacOS. Here for instance I found a way to get the installation working by installing the complete fink-package.

Using PHP 5.3 was no option for me, as we are developing for PHP5.2 and installing a second Operating System seemed somewhat oversized for the simple task of installing a small PHP-extension.

Back to the roots

So what was the original problem again? A missing installation of the libmagic-library. Event though the info-binary is installed on the MacOS I’m running, a shared libmagic-Library is missing.

So what the hell, why not compile one?

Continue reading fileinfo for PHP on Leopard