UTF-8 encoded data in PHP and MySQL

unicode

Since every developer has probably struggled with data encoding once in their career, a short summary “how to get your data in and out of a MySQL database with PHP”:

  • When you create your MySQL database, set the collation to one of the UTF-8 options (e.g.: utf8_general_ci). If you have no rights to create or change your database, do this on the table level.
    Note: if you use phpMyAdmin, it’s good to set your “session” encoding to UTF-8 before you sign in (although I believe this is now the default). Otherwise perfectly good data could look weird.
  • Secondly, when you create a database connection in PHP, be sure to set the character set of your connection to UTF-8 by executing

    PHP
    1
    <span class="nodeLabelBox repTarget"><span class="nodeAttr editGroup"><span class="nodeValue editable">SET NAMES 'utf8'</span></span></span>

    (notice the missing dash). Some extensions/adapters may have a method to do this (e.g. PDO) or allow you to pass it as an option.
  • Third, save your PHP script/file as UTF-8 no-BOM (no byte-order mark since UTF-8 does not have byte order issues).
  • Last, but most important & applicable to many more projects: set the charset of your page through the HTTP header. e.g.:

    PHP
    1
    <span class="nodeLabelBox repTarget"><span class="nodeAttr editGroup"><span class="nodeValue editable">header('Content-Type: text/html;charset=utf-8');</span></span></span>

    Note: do not use the <meta> tag (solely) for this.  Only using the meta tag causes most browsers to start re-parsing your content once they notice it’s UTF-8 instead of their default assumption. This only slows down page loading.

This post is certainly not meant to be a Unicode manual but it can be a quick reference when you every wonder why you see those strange characters in your database.

Migrate from a text editor to an Eclipse PHP IDE

eclipse

More than a year ago I got convinced by a colleague to leave my favorite text editor behind and make the switch to an IDE (Integrated Development Environment) for PHP project development.

In my opinion there is only one IDE for PHP: Eclipse. PDT, PHPEclipse, Aptana Studio or Zend Studio are all more or less Eclipse plugins that convert the Eclipse IDE in a PHP development environment. Once you get used to it, your projects will benefit one way or another.

In reality, I still use NotePad++ a lot. It may be best to still link PHP files on your OS to an editor because IDE’s are not made for quick edits.

But, in case your in an upgrade process or just want to improve your set-up, read this article:

Getting Started with Eclipse PHP Development Tools (PDT)

It could have definitely saved me some time as I took exactly the same steps and use the same plugins. Besides the Zend Debugger that is, as I haven’t found a use for it yet.

I have only one wish: a simplification of the whole Eclipse & plugins project structure. I still don’t understand why I can’t easily upgrade between versions and why so many upgrade options are available. For me, the upgrade process has only proven to be useful for bug fixes and plugin upgrades.

Related post:
How to configure PDT

PHP versions on shared hosting in Belgium

Since the PHP build version became so important with Zend Framework, I made a list of some (on the Belgian market) prominent hosting companies and the PHP version they offer on their shared hosting.

It does not pretend to be a complete list, but rather an alphabetical list of hosting providers I bump or bumped in to once in a while. The PHP version listed is mentioned on their site or, if not on their site, retrieved through their support.

PHP version Mentioned on site
www.alfahosting.be 5.2.8 (*) No
www.combell.be 5.1.2 Yes
www.eops.be 5.2.6 Yes
www.hostbasket.be 5.2.3 No
www.level27.be 5.2.4 (*)
No
www.nucleus.be 5.2.4 No
www.one.com 5.2.8 No
www.openminds.be 5.2.8 (*) No
www.priorweb.be 5.2.8 No
www.uniweb.be 5.2.4 No
www.weble.be 5.2.6 (*) Yes

(*) they offered to install any version required.

As you can see, Combell is worst for PHP developers. There currently is also no way to get any higher version with them. It is a sad thing.

Besides them, only Weble mentioned (luckily a proper) full version number on their site. Although customers that tend to find the complete PHP version number important are probably a (growing) minority, it can make a sale.

Also, I’m surprised by the cheapest one, One. It’s the only “big” one that offers a recent version.

Please, don’t hesitate to add any others to the list.

Zend Framework PHP requirements

I try to enforce Zend Framework for every new project started. If not in the requirements section of my quote, it is not that hard to convince a client of its advantages.

Tell a client how Zend Framework

  • improves stability,
  • lowers development time

and in particular

  • makes them less dependent on you as other ZF developers can pick up projects you started a lot easier.

Now, please don’t start to think about job security. Just don’t.

But, Zend Framework has a fairly high PHP version requirement. Since version 1.7 Zend has quietly changed the requirements to PHP 5.2.4. In respect of a client’s (shared) hosting, this can be the most difficult part.
This was probably a logical thing to do since the requirements mentioned PHP 5.1.4 until ZF 1.6.2 and pre-5.2.4 versions miss some great features and contain nasty bugs (in reality some ZF components didn’t even run on PHP 5.1.4).

In Belgium however, it is difficult to find a hosting company that supports PHP 5.2.4.

New PHP IDE: Aptana Studio with PHP support

After PHPEclipse and PDT, a new PHP IDE based on Eclipse is available: Aptana Studio.

Aptana Studio

Read more about it in Federico Cargnelutti’s post on PHP::Impact ( [str Blog] ).

I’ve had a look at the screencast available on the Aptana site and in my opinion the only neat difference to PDT is the “Generate Getters and Setters…” option. As you could suspect, it generates get and set methods based on a selection of existing class properties.

Apart from that, the majority of features shown also exist in PDT.

But it is only version 1.0 so who knows what future versions may bring.

PHP IDE: PDT

I have become a fan of PDT, one of the 2 PHP Eclipse IDE’s (the other is PHPEclipse).

I believe it is supported by Zend and they have derived Zend Studio for Eclipse from it (thus it contains many of the features). Maybe the only feature I’m missing is the code formatter for PHP found in Zend Studio for Eclipse (which formats your code to match, for example, the Zend Framework coding standard).

Anyway, I’ve added these addons which make it even more useful:

  • JSEclipse: JavaScript syntax highlighting and code completion.
    Add as remote site: http://download.macromedia.com/pub/labs/jseclipse/autoinstall/site.xml
  • Subclipse: Subversion (SVN) client.
    Add as remote site: http://subclipse.tigris.org/update_1.4.x
  • HTML Tidy: well known (x)HTML cleanup.
    Download and add as a local site: http://eclipsetidy.sourceforge.net/

Preferences (Window – Preferences) I have adjusted:

  • Search for encoding and set everything to UTF-8.
  • Search for delimiter and set everything to Unix.
  • My favorite: go to PHP – Editor – Save Actions and select Remove trailing whitespace (it does not search for this).

The only downside: Eclipse is written in Java.

PayPal sandbox problems

Implementing the PayPal “Website Payments Standard” (advanced “Buy Now button”) has proven to be an extremely painful process for me. In my opinion, they have a terribly bad system when compared to competitors and current web standards.

I’ve been implementing the Instant Payment Notification (IPN) which basically sends feedback about a “Buy Now button” transaction.

Most importantly: it has had numerous bugs and downtime the last year or so (as you can see in their sandbox status blog). Some desperate people even mentioned PayPal intentionally wanted you to test on their live system performing real payments.

Secondly, here is a list of problems they should solve or things they should improve:

  • First of all, when accessing their cart, a pre-2000 JavaScript script performs a window resize. Why?
  • In sandbox mode, an invalid credit card number is prefilled: you cannot use it, why prefill?
  • The JavaScript page reload that is triggered after country change is a big no-no.
  • You have to sign in to the developer central in a different tab or window in order to test: why?
  • The whole sandbox is painfully slow (at least when accessing from Belgium).

Does this again prove that big companies suffer from their size when building products? I cannot imagine this would happen over such a long period of time in a small business.

Edit: it just happened again. You’re testing the IPN response and all of a sudden the exact same test order you made just does not work anymore. Without changing a single thing.

Zend Framework benchmark

A bit late, but Paul M. Jones did a nice benchmark on PHP frameworks in September.

While Paul seems to have put quite some time in it, some people will probably argue about the results (like with all benchmarks). Anyway, I’m of course mostly interested in the PHP – Zend Framework comparison which shows that his Zend Framework script runs > 90%  slower than a plain PHP script. In return you get of course RAD: faster results and tighter development budgets. In my, again, humble opinion it’s more than worth it!

They have lost speed between 1.0 and 1.6 but of course so many features were added. Hopefully it can be improved a bit in future versions.

Until now I have not worked on a high traffic project in ZF so I hope my opinion won’t change then. I did hear the guys working on tv.be had some issues once traffic started coming in…

It’s a shame I missed Shahar Evron’s presentation about Scaling PHP Applications with Zend Platform on this subject. Hopefully he’ll give this talk in Europe some day.

New FPDF version

I guess the author probably didn’t predict that FPDF, the free PDF generation library for PHP, would be used by a huge amount of projects 7 years after it’s inception.

Although it’s recommended to use something like UFPDF or Zend_Pdf with UTF-8 support, it’s notable how many websites still use FPDF. The package, until recently, hasn’t been updated in the last 4 years.

Last month though, Olivier Plathey suddenly released a new version (1.6) with some important bugfixes and features. Time to upgrade!

If this is a start of more bugfixes/new releases, a subversion repository would be nice.

PHP at FrOSCon

Although I’ve only been at FrOSCon one day I was happy to attended some interesting talks. Especially the ones by Sebastian Bergmann about PECL, PHP 5 and future PHP versions.

The most important new feature for the next PHP version will probably be Traits: a possible solution for multiple inheritance (although some may argue that they provide the same functionality & problems). I do wonder if people will actually ever use it as much as they talk about it…

I now also know what Aspect-oriented programming is.

Thanks guys for the great conference!