CakeMail Logo

CakeMail Blog

Email marketing for small businesses

PHP tidbits you should know about

Posted on January 19, 2011
Avatar

Written by

Categories

Email Marketing
  • http://davejansen.com/ Dave

    Thanks for your article! There’s some interesting tidbits in here indeed. A quick question; In your example of using preg_match on a string containing a date. Would, in your example, a simple explode() or perhaps even a few substr()s if you want to set up a named array like you did be faster, or slower than a preg_match? I know the difference will probably be negligible, especially so in smaller projects, but I’m talking about the big picture. What’s your thoughts on this?

  • cjoudrey

    @Dave glad you liked it! :)

    About the example, that is a good point. I actually couldn’t think of a good example so I figured I’d take something simple to explain it. Explode will likely be faster, but the time difference is negligible.

    That said, the real advantage I see by using named subpatterns over explode is having $data['year'] instead of $date[0] in your code. If one day you change the regex for any reason, the year might not be [0] anymore, but it will always be ['year']. :)

    Hope this answers your question!

  • http://shawnbiddle.com Shawn

    Well written article but I always _hate_ these because I always see comments like “WOW, this is amazing, I’ve never seen this before” and I facepalm at what horrible kludgery they’ve been using in their code up until this point. It’s not like they’re undocumented features, they’re quite well documented in fact. I don’t accept the “you have to learn somewhere” excuse. You have a place to learn, it’s called the documentation, read it, learn it, love/hate it but not knowing something because you haven’t looked on your own an excuse.
    /rant

  • cjoudrey

    @Shawn Everyone has their own way of learning. Some study the documentation of a language to know everything about it, while others learn by example. Don’t get me wrong, I’m not saying learning by example is a good way, but some people do learn like that. Either way, we’ve all been a beginner at one point and when you are beginner, finding out something new is exciting and people are allowed to express their excitement.

  • http://nerderati.com Joël Perras

    @Dave: You’d pretty much never, ever want to do substr() or explode()’s on strings to extract out date information – the example that Christian gave is obviously a contrived one (as all of the examples in this article are), to illustrate a point. There are much better facilities for that in PHP, whose datetime handling abilities are actually quite good.

    Some resources:

    http://php.net/manual/en/function.strtotime.php
    http://www.php.net/manual/en/class.datetime.php

  • http://www.rooftopsolutions.nl/ Eveert

    I agree with joel, use the DateTime.

    As for the ‘Closure’ typehint, don’t use it. The PHP manual states that it’s an implementation detail, and it *might* change in the future. It shouldn’t be relied upon.

  • http://websitecenter.ca/ Montreal Web Design

    $foo = function ($p) {} is an anonymous function
    and
    $foo = function ($p) use ($bar) is a closure.

  • cjoudrey

    @MontrealWebDesign Thanks for pointing that out. What I meant to say was that both are instances of the Closure class. I’ll reword that.

  • http://websitecenter.ca/ Montreal Web Design

    n.p btw thanks for information about Closure type hint, I did not know about that.

  • http://davejansen.com/ Dave

    @Joël Perras Thanks for the reply, and I agree; as for dates there are much better ways. I, like Christian, merely took date as an example. I know most often speed differences can be ignored, but I’ve most always followed the ‘if you can reasonably avoid using preg_*, do so’ rule of thumb. I know in some cases there’s no going around it, but in the case like the example shows, where you know exactly what appears where –ie. processing a tab-(or other character) delimited document–, using substr() or similar would in fact be faster.

    @cjoudrey You’re absolutely right, being able to use named variables (or whatchamacallit) over meaningless (and easy to change over time) numbers is far better. In my example, I meant you could achieve the same thing with substr():


    $matches['year'] = substr($date, 0, 4);
    //etc.

    or explode(), possibly combined with a list() if you want to:


    list($year, $month, $day) = explode('-', $date);

    Note that I’m, again, only using date as an example here, using strtotime() and the like most-often make far more sense when you’re working with dates/time.

  • http://simas.posterous.com Simas Toleikis

    I don’t think it’s a good idea to advertise __autoload function like this. It is pretty much a deprecated feature right now and that example should be replaced with spl_autoload* functions.

    Also, you may want to subclass ArrayObject instead of ArrayAccess interface when you need your object to act as an array. That would give you array access, sort, foreach, iterator, count support with zero code.

    Other than that, that’s a good write-up :)

  • http://cach.me/blog guyht

    The regex trick is very neat, makes your code much more readable.

    The ArrayAccess class is also really interesting, quite a powerful feature really.