Advanced Techniques


Now I know that PHP isn’t very “cool” these days; it’s probably the most misunderstood web language because all the script kiddies use it to include files on their webpages and put up forms that get hacked hours later. But I think PHP deserves a whole lot more credit than it gets; it’s fast, it integrates very nicely with Apache, it got all the right features in version 5, and even if you don’t like using it, there’s always a chance you might have to write some anyway. So, maybe these tips can help you out:

  • Use heredoc syntax for strings: If there’s one thing I got tired of fast, it was building long strings like this:
    $s = "This is something ";
    $s .= "that will make ";
    $s .= "a long string, with ";
    $s .= "a variable: $x.";

    If I am going to make a string with more than one line, and even sometimes when I’m not, then I use heredoc. It’s much, much cleaner than worrying about quotation marks, because it isn’t delimited with them. Here’s the same string as above, in one clean heredoc block:

    $s = <<<HEREDOC
      This is something 
      that will make 
      a long string, with 
      a variable: {$x}.
    HEREDOC;

    And, that delimiter can be anything; EOF, MYSTRING, ELEPHANT, etc. The only caveat is that the ending delimiter has to be all the way at the left; you can’t put any spaces or tabs before it. This is a slight nuisance if you are serious about maintaining indentation, but it’s a small compromise for having such a clean way of making strings.

  • Contain your variables in strings with curly braces. This goes along with the heredoc syntax above, but you can use it in double-quote delimited strings too. It looks like so:
    $x = "Something with {$y['key']} and {$z}.";

    It allows you to include array variables in strings and it makes the variables stand out better in most any text editor. Update 11/12: I should mention, for anyone who doesn’t know, that using curly braces in your strings is not recommended when you are working with user input. See this bug. Either validate the user input to make sure that there are no curly braces before plugging it in, or don’t use curly braces with user input at all.

  • Build arrays. Always. You probably won’t make just one of something. Sooner or later, you’ll realize you need two, and then you’ll have to go back and turn that variable into an array so you can store multiple things. I walked into this mistake too many times… I would be processing a form, and have a variable called $error to store a possible error, and then I realize 10 minutes later that there might be more than one error, and I need to make an array of errors, and I have to go back and rewrite things to use an array instead. Nowadays I don’t get into that mess anymore; I just start with arrays and save myself the trouble.
  • Use associative arrays. (These are called hashes too.) They make things easier to keep track of. This goes along with the previous tip, though if you are just making a linear list of things, there’s no need. Where associative arrays come in handy is when you need to remember the keys you use, because you can refer to the key with a variable. Say I have an array that I use to retrieve the address for a page, I can do the following (this is a horrible example though):
    $pages = array(
     'index' => 'startPage.php',
     'contact' => 'sendForm.php'
    );
     
    // could do some function here
    $thePage = 'contact';
     
    $theAddress = $pages[$thePage];
     
    // here I'll get sendForm.php

    I think that’s very convenient.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: