Tag Archives: Tutorial

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

Getting current datetime in ISO format in MySQL

Here is the MySQL function to use to get the current datetime as ISO 8601 format as asked in DarwinCore:

DATE_FORMAT(NOW(),'%Y-%m-%dT%TZ')

DATE_FORMAT() is obviously a function for formatting date and time, and we are using here %Y for four-digit year, %m for two-digits month, %d for two-digits day and %T for 24-hour time (hh:mm:ss). NOW() gives the current datetime.

And then you get a nice figure like this one:

2009-06-10T07:43:12Z

Note that the same result can be achieved with:
CONCAT(CURDATE(),'T',CURTIME(),'Z')

Where CONCAT() is used for concatenating strings, and CURDATE() and CURTIME() give respectively the current date and the current time.

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 */

Embedding a GBIF distribution map in a web page

Last june GBIF released its new portal. Together with it came a few web services. One of them is the possibility to embed a distribution map in a web page.

Let say you have a special interest in one particular species. It can be the one you’re doing your research on, it can be the one you will teach to your schoolkids, it can simply be that you like this particular species.

Then why not making a web page for presenting this and sharing your interest with others? And why not displaying a map for showing where this animal happens to be?

Fortunately, there is a big network of biodiversity databases out there: GBIF. This network makes more than 146 millions of records available to anyone. The data is about collected specimens or observations. GBIF builds maps from the records provided with coordinates, and it provides a simple way to embed them into a webpage.
Continue reading

An exemple of use of the web service

One simple exemple with two uses of geo:truc as a web service.

First we create a very simple PHP function calling the web service, with its first parameter the location we want to get the coordinates, and the second the format in which we want them; and then we invoke the function first for geotagging our web page with META tags in the header section, then we call it again for georeferencing the place we want to write about, and why not, having it as a microformat.

<?php
function getGeoTag ($location, $format) {
$url = “http://www.geotruc.net/gt.php”;
$parameters = “?q=”.urlencode($location).”&format=”.$format;

return file_get_contents($url.$parameters);
}
?><html>
<head>
<!– First call of getGeoTag //–>

<?php echo getGeoTag (“New York, USA”, “html”); ?>
<title>Test geotag</title>
</head>
<body>
<!– Second call of getGeoTag //–>
Gergovie (<?php echo getGeoTag (“Gergovie, France”, “geo”);?>) is a place for winners!
</body>
</html>

As shown with this simple example, building applications using geo:truc as a web service isn’t difficult.