Category Archives: PHP

Handy little function

Quite often I find myself wanting to run the same script by either cli or through a browser. But I don’t want to fill my echo statements with <br /> tags if I’m on cli because that’d just look ugly, but at the same time I don’t just want to use \n when outputting in the browser because everything would be on the same line.

This handy little function helps to do simple output that will be readable in the browser as well as the command line:

$_ = function($str) {
    if (PHP_SAPI == 'cli') {
        echo $str;
    } else {
        echo nl2br(str_replace("\t", str_repeat('&nbsp;', 6), $str))."\n";
        flush();
    }
};

Then when I want to echo something I just do:

$_("This is a test\n");
$_("\tTime:" . time() . "\n\n");

Simple but handy.

Did you like this? Share it:

Sorting an array of objects by one or more object property

Quite often I find myself having an array of objects and needing to sort that array of objects by property (either one property or multiple)…

Imagine, for example, getting a large result set from your database and ordering in the query just takes too long. Or perhaps you’re getting results from a web service and that service doesn’t return the results in the order you’d like to use. Have you ever found yourself in that situation, too? On looking at the usort documentation one day I came across a comment by someone called Will Shaver that did almost what I wanted. With a little adaptation for my own use (being able to change the sort order, for example), it has become one of my favourite functions to use for sorting.

loading gist...

Now a few cools things about the function:

  1. It uses anonymous/lambda functions (or closures, whatever your prefer to call them), and that’s just plain fun
  2. You can sort on more than one property and because the sorting is recursive, it’ll sort the second property within the confines of the first, the third within the confines of the second, and so on. Think sorting in SQL
  3. You can sort in ascending or descending order for any of the properties
  4. It retains key associations so you could use this on an associative array of objects
  5. If the parameter you want to sort on is an array itself then you can use any value (by specifying it’s key) in that array as the sorting value
Did you like this? Share it:

Extend Zend_View_Stream to easily escape view variables

Zend_View_Stream is used pretty much when ever you use Zend_View, and I’ve blogged about how handy it is before.  But as it’s a class like any other, you can extend it to give added functionality.  One such use is to add automatic escaping to your view variables when you want.  So instead of doing:

<?php echo $this->escape($this->var); ?>
<?= $this->escape($this->var); ?>

You could simply do:

<?=~ $this->var; ?>

That’s a lot simpler, isn’t it?
Continue reading Extend Zend_View_Stream to easily escape view variables

Did you like this? Share it:

PHPNW

image

Usually I’m a total wallflower at conferences, gravitating to only the people I know. This time round I’m trying to change that and speak to people, ask speakers questions, and all that.

Right now, though, I’m enjoying dinner. 🙂

Did you like this? Share it:

PHPNW Conference 2010

Going to be travelling to the PHPNW Conference today (it’s tomorrow, but I don’t fancy catching the stupidly early train to get there on time), but going over the schedule is a pain… There are just too many good talks! How can I possibly go see them all?! The 11:15 time slot is easy, that’ll be Rob Allen’s talk on ZF 2 – we use it so much at work now that it’d be crazy to not find out what’s coming and how this may affect what we’re currently doing.  Same with the  Michelangelo van Dam talk on unit testing with ZF.  But the 12:15, 15:00 and 16:15 talks?  I have no idea what to choose!  Juozas Kaziukena’s Optimizing Zend Framework might be worth while, but then again, is it all about ZF1 and how much will be relevant for ZF2?  The HipHop talk by Scott McVicar would be interesting. I can’t see it being deployed at my work, should still be a good talk… I’m liking the sound of the Database version control without pain by Harrie Verveer as well!  And that still leaves me with two other time slots to decide on… Sheesh!

The agony of choice, eh? 😉

Did you like this? Share it:

QrCode view helper

You see QrCodes popping up every now and again on sites, in publications and the like. I think they can be a very handy way for people with cameras on their phones to get a url or other content on to their phone very easily. (I’m thinking more about those people without iPhones or full keyboards, of course!)

If you’ve never seen a QrCode before, it looks something like this:

QR Code image

Now how cool would it be to be able to generate that automatically for each page on your site and allow people to be able bookmark that site on their phone? Well, I think it’d be pretty cool! So I came up with a very simple ZF view helper to do it for me.

Continue reading QrCode view helper

Did you like this? Share it:

Adding new items to RSS feed – it shouldn’t be this hard!

I have just started to use the Zend_Feed related components in earnest and am really liking the Zend_Feed_Writer (new to ZF 1.10.0). So what I wanted to do was created an RSS feed file is one didn’t exist and then keep updating that file as-and-when new items came in. Seems a really easy and simple thing to do, right? That, unfortunately, has not been my experience.

I have to say that to documentation seems quite lacking on the ZF site (for all the the Feed components, really, not just the Writer). Because of that, what follows may be idiotic and there really is an easy way. If so, I hope that you will post up a comment and let me know because I’d love to learn!

On with what I did…

Continue reading Adding new items to RSS feed – it shouldn’t be this hard!

Did you like this? Share it:

PHP Team Development (book)

Book coverThe other day I had a new book sent to me called PHP Team Development, written by Samisa Abeysinghe and published by Packt Publishing. Unfortunately, it arrived at work when I was on holiday so I haven’t been able to have a look at it yet. :-/ However, I’m back today and have the book in my hands (well, not literally, of course, else typing would be much more difficult), so am looking forward to diving in to it.

Hopefully have a bit of a review posted up here some time soon!

Did you like this? Share it: