Tag Archives: PHP

Transparently compressing JS and CSS files with PHP

When I develop a web site, I usually configure it to compress the content before sending it to the end user. Downloading resources such as HTML, CSS and JS files is then smaller and thus faster.

It is possible to configure this in the .htaccess file (in case the server runs Apache) as well as it is possible to program it in PHP.

Unfortunately, it looks like my web hosting company doesn’t allow the former, so I had to take the PHP way for compressing resources in geo:truc.

The PHP code for doing this is rather simple:

This works fine: write a PHP script, put the code above in it, and it’s done! It works with HTML files, and it’s possible to use it as well with JS ans CSS files, although they need to start with this snippet and have a .php extension.

But how to do when using different JavaScript libraries that are now commonly used? A new .php file for every new version of jQuery? It sounds fastidious. And how to make it transparent, i.e. not passing a URL like http://www.example.com/js/compress.php?file=jQuery.js ?

Continue reading

Cuicui update

Cuicui has been updated!

  • Bug fixes!
  • Less code.
  • Support for nanoformat (L: …) with link to map service.

So now this small piece of PHP is tagged version 0.2.

Cuicui is a small PHP library with only one function for parsing feeds from microblogging services in order to integrate them in a web site. You can choose the number of items to display, having a breakdown by date, allowing replies, and linking to tags and other users microblogs.

See it in action at http://korbinus.geotruc.net

PHP: isset () vs strlen ()

How to test the length of a string in PHP ?  Well, PHP has a function called strlen () for this purpose.
So obviously, a simple if (strlen ($str) > 10) { ... } should do the trick, isn’t it?

Actually, there’s another way to do that: considering that a string is an array of characters, it’s possible to test if a character is set at the length we want to check with isset (). So the line of code above should be replace by if (isset ($str [10]) { ... }.

Why should we use isset () instead of strlen ()?

Well, it’s way faster: running the test above 100 000 times for each function gives me:
strlen () : 0.034812927246094 seconds
isset () : 0.0079290866851807 seconds

PHP: isset()

isset() is a function testing if a variable $var is set or not.

boolean isset (mixed $var[, mixed $var[,...]])

It returns TRUE is the variable is set, and FALSE if it isn’t.

Using isset() for testing a variable saves some times compared to try to use an nonexistent one. It also helps to avoid error messages from PHP:

/* The line below generates the warning message:
* Notice: Undefined variable: var in /path/to/script.php on line 1
*/
$toto = $var;

/* Of course it's possible to silence with @ the warnings from PHP but it's costly */
$toto = @$var;

/* Do this instead: */
if (isset ($var)) {
$toto = $var;
}

As seen in the function definition above, isset can take multiple arguments. But it is faster to have an if statement with one isset() call per variable rather than one isset() call with several variables:

if (isset($a) && isset($b)) { } /* Fast */

if (isset($a, $b)) { } /* Slow */

A small string benchmark in PHP

I’m currently working on the new portal for GBIF-Sweden, and I am trying to optimize the queries but also the PHP code.

Since (X)HTML is a lot of text, you need to format and concatenate a lot of text. So, what’s the fastest way to do the job?

There is a lot of ways to build a string in PHP, for instance embedding a variable into a double-quoted string, explicitly using a concatenate operator, of using sprintf () which let you build a string from a formated template. There is more, but from my current needs I tested only four ways to build a string.

And here is the result:

result: for 10000000 iterations
test A (concatenation $hello.” world!”): 2.7659468650818
test B (sprintf (“%s world!”, $hello)): 8.829686164856
test C (“$hello world!”): 3.7119560241699
test D (concatenation $hello.’ world!’): 2.7726769447327

So, from my test, using an explicit concatenation is the fastest way to build a string. In this exemple, concatenation with a double-quoted string is faster than a concatenation with a single-quoted one. In fact most of the time, concatenation with a single-quoted string was a little bit faster.

I’m running PHP 5 on Fedora 8, but this result may be different with a different configuration.