issetor shorthand function in PHP

This is one of those nifty little functions that you never seem to remember off the top of your head. Basically it is shorthand for when you want to print a variable if it is set or exists. Instead of doing a bunch of is/or checks on your variable, try this instead:

function issetor(&$var, $default = false) {
    return isset($var) ? $var : $default;
}

Usage:

echo issetor($variable);

Or in a HTML-template:

<span class="name"><?=issetor($name)?></span>

Since the function takes two arguments, the variable itself and a default value, you could change the default behaviour if you want.

(Kudos to NikiC at Stackoverflow.)

New project – Geografispel.se

geografispel_dump
So I had to do something for the kids. Especially the super-gifted ones who likes geography. The result is a work-in-progress game made with HTML/CSS/JS with a backend API made with SLIM framework. The idea is to create a place where you can practice on the worlds’ capitals and flags. So head on over to Geografispel.se and see what you remember from elementary school!

Dynamic map markers using only CSS

I had a problem recently with a site that needed a dynamic way to present map markers both on a google map and in a list view. Before I used static numbered PNGs but this was not a sustainable option since the number of markers used could exceed hundreds. And I wasn’t that particularly interested in creating 250 different PNGs in multiple color… I came up with a CSS only solution that implemented the DATA URI-scheme together with CSS. Continue reading

New project: Phonebooth API

Yesterday my little side project Phonebooth.se went live. It’s a small but hopefully helpful REST API for developers or projects that need to validate registered users’ phone numbers. It supports both mobile and landline numbers and returns a JSON result. The service currently only supports swedish numbers but more is likely to be added going forward.

The idea is that as soon as a user fills in their number, an asynchronous request could be posted to phonebooth to check whether the number is valid or not. But feel free to use it however you please. It’s free.

WordPress and jQuery Masonry setup with floating images

The Masonry jQuery plugin is an easy way to stack divs in a nice grid without the usual problem with CSS floats – e.g divs with different heights will not look good beside each other.

In a recent project a client wanted WordPress thumbnails stacked in this way. I first added some code to functions.php in order to set widths and heights to the selected thumbnails. Continue reading

Ultimate .htaccess rewrite tutorial with 301 redirects

So, over the last couple of weeks I have moved several sites to new locations and publishing platforms which demands some redirects unless you wanna be a SEO killer. The examples below are mostly URLs with query strings which I either want to hide or make prettier. The fourth and fifth examples are quite useful when you want to create human readable URLs for APIs or web services.

Updated 22 November 2011.

Continue reading

Shell script: create virtual hosts with PHP/fastCGI (mod_fcgi)

UPDATES:

Version 1.1 – June 20 2011

  • Added  “FcgidMaxRequestLen 1000000000” to virtual host settings in order to get the WordPress Flash based image uploader to work. The default value in fcgid caused it to throw a “HTTP Error” when crunching the images.
  • Commented out the Apache directive “ServerAlias” which in previous version defaulted to *.example.com. That of course might cause problems when setting up both root domain and sub domains on the same server. Feel free to uncomment or add custom aliases.
  • Moved the ports.conf tip from script echo to comments at the end.

Continue reading

Shell script – install LAMP with dependencies for PHP/mod_fcgi

This script is intended for fresh Linux installs to provide support for the LAMP stack running with mod_fcgid instead of the default mod_php Apache module.

UPDATES:

Version 1.1 – August 18 2011

  • Added apt-get update to refresh package list
  • Added MySQL server
  • Added php5-gd library (to comply with Drupal 7 requirements)
  • Added PDO MySQL driver (to comply with Drupal 7 requirements)
  • Moved specific php.ini extensions and fixes to separate custom.ini file
  • Added restart of proftpd Continue reading

Custom WordPress Varnish VCL v1

Over the last couple of days I’ve been struggling with getting my dev WordPress environment set up with Varnish (2.1.3) Out of the box with minimal config it provides great speed and hitrate but zero functionality together with the cookie-laden WordPress. So far I have tested a bunch of VCL:s found online but here’s my take (based on practically every nice example found out there). Continue reading

Webmin – Hetzner VPS Debian 6 LAMP + fastcgi + php5 + apache

This tutorial applies to servers preconfigured with LAMP (Debian 6, Apache 2, Mysql 5, PHP 5.2). If the LAMP stack is not installed, it’s pretty straightforward to install the respective components via apt-get install. Since the image I’m using has Webmin preinstalled I use it to quickly get started. Continue reading