Archive | Development

PHP is_numeric () fails WordPress version string check

This is, perhaps, obvious to most PHP developers. But it came somewhat as a surprise to me.

Using is_numeric () for validating a WordPress version string, such as ‘4.7’, does not seem to work very well when WordPress introduces minor releases such as ‘4.7.1’.

Since I cannot be bothered to figure out why it behaves in this (erratic, IMHO) way, I have since replaced the call to is_numeric () with a small function using a simple regular expression (regexp):

    function wpVersionStringCheck ($vs)                                                                                                 
        return (preg_match ('/^(\d+\.)+\d+$/', $vs));                                                                                   

I’m sure there is a hole in there somewhere, but on the following strings at least, it gives me the desired result:

1.0 is valid
1.0. is invalid
1.0.1 is valid
1.banana.0 is invalid


Making SimpleXML truly simple using JSON in PHP

So using SimpleXML in PHP is possibly one of the worst hells of typecasting and data extraction procedures one can encounter, or close to it 🙂 But more the point, you don’t need to walk down that road alone, enter JSON.

In the simplest of ways, this will typically get you something useful:

    $pvar = json_decode ($xml);


(Where $xml is a SimpleXML-type object) This returns a “slightly easier to manage” PHP array.

If you want an associative array, do this:

    $pvar = json_decode ($xml, true);


To get it back to JSON, you simply use:

    $json = json_encode ($pvar).


You may need to handle XML data, but you don’t necessarily need to work with it in XML form inside your application. The above examples may not work fully for you if you have to deal with “foreign” files or clean the input data prior to conversion, but you get the idea.

This is, obviously, obvious to many. It’s a lifesaver for others 🙂



Hur svÄrt kan det vara att testa nÄgot innan man slÀpper det?

Dag 1:

[Jag] Hej, det hÀr ni slÀppte i fredags fungerar inte. Jag fÄr inget svar frÄn servern nÀr jag testar.

[MS] Är du sĂ€ker pĂ„ att det inte fungerar?

[Jag] Ja, jag Àr helt sÀker. Jag har testat 10 ggr nu och det blir samma resultat varje gÄng.

[MS] Vi har inte Àndrat nÄgot annat Àn att lÀgga till fixen. Vi fÄr helt enkelt börja felsöka.

Dag 2:

[Jag] Hej, hur gÄr det med felsökningen, kommer ni nÄgon vart?

[MS] Nej, det ser bra ut pĂ„ vĂ„r sida. Är du helt sĂ€ker pĂ„ att du gör rĂ€tt? Vi har inte Ă€ndrat nĂ„got hĂ€r.

[Jag] Du sade det. Nej, det ser inte bra ut pÄ er sida. Jag kommer inte ens fram till er sida.

[MS] Vi fortsÀtter att felsöka dÄ.

Dag 3:

[MS] Vi har hittat problemet. Den hÀr delen av servern som tar emot anrop frÄn er applikation fungerade visst inte sÄ bra. Vi har startat om den och nu fungerar det bÀttre tror jag.

[Jag] Åh vad bra. DĂ„ testar jag vidare.

Dag 4:

[Jag] Det fungerar inte sÄ bra, er server verkar inte gilla vÄrat sÀtt att göra det pÄ.

[MS] Nej, ni gör fel. Ni skall göra sÄ hÀr.

[Jag] Men vi gör exakt precis sÄ.

[MS] Jag kan inte se att ni gör sÄ som ni skall. I vÄra loggar hittar vi bara andra metoder.

[Jag] Men vi gör precis exakt sÄ som ni har sagt Ät oss att göra. Har ni testat det sjÀlva?

[MS] Nu testade jag det. Det fungerar inte. Jag fÄr Äterkomma nÀr det hÀr fungerar.


Timkostnad: En bit över 1000kr/tim ex moms.

Antal dagar bortslösade: 4.

Tror du de tÀnker bjuda pÄ nedlagd tid eftersom det var hos dem felet lÄg? Skulle inte tro det.

[MS] Ă€r ett “IT-företag” i Stockholm som utvecklat ett “standardsystem” som Ă€r allt annat Ă€n standard. Man skrĂ€ddarsyr varje sĂ„ld kundlösning och tar betalt tills kunden gĂ„r sönder för allt man gör. Man tar inget ansvar för fel som uppstĂ„r vare sig det rör sig om uppgraderingar av egen kod eller saker man levererat. Allt som behöver korrigeras/Ă€ndras/fixas debiteras kund.



Locating older version of MySQL 4, MySQL 5, source code, binaries, rpm, etc.

Quite some time ago, we needed to move a customer’s MySQL 4 server from one location to another. In the process, we figured we’d update the server to use some moderately modern version like MySQL 5.0 at least. Also, if we were to have any chance of virtualizing and upgrading the actual server environment to something more modern like Ubuntu 10.04.LTS or 12.04.LTS, or Debian 6.0, we’d have to re-compile the sources regardless. Not taking other incompatibilities into account, that line of thinking ran into Chuck Norris because the Windows DLLs supplied with the application using the database were not compatible with anything but MySQL 4.

The particular version of MySQL 4 running on the customer’s server was self-compiled (by us), so I figured I’d at least locate the “most recent” version of MySQL 4. To my surprise, this turned out to be harder than I could possibly imagine. In a world where “nobody” forgets anything, I could not find a single trace of a source distribution for MySQL 4. Google, Facebook, Microsoft, and Apple probably know the size of shoes I wear, but they don’t know where MySQL 4 sources are located. This struck me as very odd as MySQL 4 was a) very popular, b) open source, and c) should at least reside on half a dozen servers on the Internet, or so I thought.

Like a core dump out of the blue skies, someone Skyped me a link today. The person had ran into a mirror archive and remembered that I was looking for this “eons ago”. I have now mirrored most of that archive into/onto my own cloud store. I’ll go through that in a few days and remove the things I don’t need, but this may very well turn out to he a lifesaver.

I wonder if Sun and/or Oracle decided that keeping old MySQL versions around was a bad idea …

If you, like me, need to find some odd version of MySQL, for whatever reason, here are two links that may be of good use to you:


jQueryMobile or “Mobile site” selection sets

Using jQueryMobile for a fairly lightweight “mobile site”, I wonder what all you experts say about selection sets. A (long) list of countries for example. To prevent excessive delays, I now split country selection in two screens. The first one shows A-Ö (A-Z for you English-speaking people), if I tap on B, the next screen shows a list of countries starting with B. Hitting the back button brings me back to A-Ö (A-Z), selecting a country takes me to.

It feels right, and it doesn’t transfer massive amounts of data that a) isn’t used half of the time, and b) doesn’t take time to render on slower mobile devices.

I’m not too concerned with database performance on the server for this particular selection set since the SQL query is static and resides in the cache 99.5% of the time.


Bye bye NetBeans, still fails on remote projects, cannot detect source file changes

So NetBeans 7.x is out since a few months. With many new functions, new and updated plugins, a lot of bugfixes, etc. Fantastic! Go NetBeans!

And just because I’m a sucker for punishment, I open up a new PHP project, select a WordPress site where I do some plugin sandbox development, enter all the parameters NetBeans wants, and click “OK”.

And … NetBeans starts doing its normal routine of scanning the remote (via SSH) directories, asks me to confirm that I want to download some 1714 files to my local computer, and when I do (why wouldn’t I, right?), it begins its glorious work.

In the meantime, I set-up a new project using ActiveState’s Komodo 6.13 IDE, I point it to the same server and directory and click “OK”. Three seconds later, I have my project.

NetBeans you ask? Oh, it’s still downloading. My Internet connection is too slow (it’ll max out around 20 mbit/s) of course .. I should have at least 100 mbit/s, or possibly GB/s. Because if you want to do NetBeans development on remote servers and do it the “proper NetBeans way”, you STILL have to download your entire project locally.

I could live with that rather awkward way of doing things if NetBeans could only detect changes to the file(s) on the remote. As it is now, and as it has been for many years with NetBeans, it’ll simply consider itself to be the master, and overwrite whatever is on the server.

Of course I’m doing it all wrong. I should use version control, I should not expect myself to be able to use my workstation as a terminal to the server. I mean, those days are gone, right? We ARE in the MODEM ERA of Internet, where you REALLY wanted to do EVERYTHING LOCALLY due to bandwidth limitations, etc.

Oh no, wait .. this is 2011 .. everyone but your pet is connected 24/7/365.

So, I think now that NetBeans have shown (again), that they have no intention of EVER fixing this problem, I’ll just do rm -r -f and be happy. And what’s more, so will the rest of the NetBeans community, because they won’t need to listen to me whining about it 🙂

Bye Bye NetBeans.

#NetBeans #IDE #PHP #fail #SSH #remote #development


Increasing the phpMyAdmin session timeout

When phpMyAdmin is installed, the default session timeout value is too low for many users, making your phpMyAdmin session expire too soon. One could argue that a low session timeout value is a good idea from a security perspective. If you do not think this is an issue, here are a few simple steps that’ll let you change how long phpMyAdmin will keep your session(s) alive.

Open in the phpMyAdmin “root” directory. Look for a line that contains this: $cfg[‘LoginCookieValidity’]. Set the value to the desired number of seconds you want the session to stay alive (3600 = one hour, which is reasonable for most users). If you do not have that line in your file, add it like this:

$cfg[‘LoginCookieValidity’] = 3600;

Don’t forget to save the file, and then login again to phpMyAdmin. You may need to close the browser and re-open your phpMyAdmin URL.

This also assumes that the PHP session garbage collection is set-up accordingly. This can be done in a number of ways:

  • php.ini; add a line (or change an existing) that contains session.gc_maxlifetime = <seconds>
  • Apache configuration; add a line to the appropriate <Directory> block that says “php_admin_value session.gc_maxlifetime <seconds>”
  • (phpMyAdmin); after the previously edited line, add a line with “ini_set(‘session.gc_maxlifetime’, <seconds>);”

The <seconds> above is the same value that you set your variable to in at the beginning of this post, “3600” (sans quotes) in my case. (Some of these methods may or may not work on the server you’re using.)

This isn’t the only way to circumvent phpMyAdmin sessions expiring on you in the middle of that important work; you can, of course, configure phpMyAdmin to have appropriate access directly, thus allowing you to access your MySQL database(s) without entering a username and a password. You’ll find more information about this on the phpMyAdmin website.

(phpMyAdmin is, IMHO, one of the best tools ever written in its class. My only gripe is that I’d really like to have a much less bloated visual display theme as the default, but you can accomplish this yourself quite easily.)


When version control systems fail miserably

When I began using version control system software, the world was mostly stuck with RCS, if you wanted to use free software that is. Next came CVS, PRCS, SVN (Subversion), Bazaar, Git, Mercurial, and many others. There were, of course, also many commercial version control systems available such as ClearCase and Perforce. So now we have several excellent pieces of software when it comes to version control.

In most cases, users of these version control systems seem to fall into two categories. Those that prefer to work locally on their own machine or in their own environment, and those that work in a shared environment. Regardless of which category you fall into, the theory behind many modern version control systems is that you never work against the immediate source, but instead use some sort of local version, be it “working copy” or “cloned repository”.

With the advent of integrated development environments such as Eclipse and Netbeans (and many others), support for version control systems is often built-in or provided via some plugin. You get immediate notifications when your source files have been modified (by yourself), when a conflict occurs, you can check files in and out of the repository, etc.

I — and others — seem to have run into a problem with the way things are being done, and I cannot seem to find a solution to this problem, other than resorting to the command-line interface of the various tools (svn, hg, git, etc).

Here’s my beef:

I keep a stable, production level, copy of a project at /home/prod/stable. It is reachable from the web (because that’s where I use the application) as It is also under version control at /home/repo/prod[..]

Now, I also keep my development version on that same server, also reachable from the web (because I don’t think duplicating my runtime environment on every single computer I may work on the project from is a grand idea). So for this, I have /home/prod/dev and; I can choose to have a complete “duplicate” repository at say /home/repo/branches/dev, or just a working copy at /home/prod/dev. The crucial issue here is that I keep my live development files on the same server as the repo, but I code using NetBeans or Eclipse (or anything based on Eclipse) from my laptop or one of my workstations.

This combination seems impossible to get working if I want to use the built-in version control system support in either of these two IDE:s. I’ve gone through Subversive, Subclipse, SVNkit, Git, and Mercurial — and none of them, in combination with NetBeans or Eclipse, can handle this.

So I’m asking myself (and you) .. what’s f-ing problem?!? Why can’t NetBeans and Eclipse just treat the remote project as any other project? Why can’t I simply be allowed to create a remote project, and then specify the appropriate parameters for version control and be allowed to utilize the power of version control from within the IDE?

Using something like SSHfs to “trick” NetBeans and Eclipse into thinking it’s a local project won’t work either due to the really poor performance of SSHfs (Ubuntu 10.10). Obviously, the solution needs to handle not just me working on the same project from many locations, it also has to support other developers working on the same project, from many locations. All Eclipse-based editors (EclipsePDT, Zend Studio, Aptana, etc) suffer from the same problem since they use the same plugins.

Searching the web for this issue, I’ve come across many requests for this, but not a single solution; which, given the nature of Internet, is a pretty rare thing. I could never in my wildest imagination (and it’s pretty far out) had thought that I’d be having a beef with something as common as a remote server or remote file system in the year 2010 🙁


Why Netbeans 6.9x+ is an awesome PHP development environment

I’ve been doing some form of coding since around the advent of the IBM PC and VIC-20 and have obviously come across many editors and IDE:s for a number of platforms. Things have come a long way since Edlin. I cannot recall every single editor and IDE I’ve been toying with, and I’m not sure it’s relevant to this post, but some of them include A/Edit, Emacs, TSE/Qedit, Brief, SlickEdit, vi, and many derivatives of these. Having been a DOS programmer for quite some time, I’ve also been working in the various Borland IDE:s.

In the past 10 years I’ve mostly been using Emacs, and every now and then I’ve been sneaking a peek at various other Linux based editors. For the past several years I’ve been giving both Eclipse and NetBeans a go. There are a number of IDE:s out there that are based on Eclipse (Zend/ZDE, Aptana Studio, Eclipse PDT to name a few), so in many cases they share their core behavior, look ‘n’ feel, pros and cons.

One thing I’ve learned, the hard way, is that there are just about as many “My editor is better than your editor”-battles being fought as there are “My language is better than your language”-battles; programmers and developers truly seem to enjoy trying to outdo each other when it comes to proving their arsenal of tools to be superior to that of others. I’ve never understood this part. If you can solve the given task, with all its parameters, with whatever tools you prefer, go and do it. If someone else can manage the same thing with some other tool, good for them. Listening to these arguments is like listening to two five-year-olds battling it out trying to convince the other that a given color is prettier than another. Of course, one could just turn a deaf ear and not listen 🙂

Now let’s talk a little bit about Eclipse and NetBeans.
Continue Reading →


Free CakePHP E-Book – Super Awesome Advanced CakePHP Tips

There are a few books out for CakePHP development. The main problem with them is that they’re fairly old. There’s also a book on the way (2010) that has not yet been released. And then there’s Matt Curry’s completely free CakePHP book for those that have managed to grasp the basics of CakePHP and want a guide to somewhat more advanced topics.

One can only congratulate Matt on a great idea and implementation; and what’s not to like about a free book about CakePHP development?

An introduction to the book here:
Matt Curry’s original blog post about it here:
The book’s direct download link is here:

More about CakePHP can of course be found on

Matt has also released a number of CakePHP plugins and code snippets here

Thanks Matt!


Powered by WordPress. Designed by WooThemes