Monthly Archives: February 2011

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

PHP PROXY?

A PHP proxy is a single script or group of scripts that takes information from a web page or interface and transfers it to a web server for some type of processing. PHP proxies can be simple or complex, taking input from a single form or linking together multiple interfaces and computer terminals into a coherent whole. Unlike the client-side JavaScript language, PHP scripts and proxies are all run on a web server, which gives them the advantage of not needing to rely on software a user has installed.

Scripts comprising a PHP proxy can be written in two ways. First, they can be written as a sequence of commands, to be executed one after the other. Second, they can be structured like classes in an object-oriented programming language. Either type of PHP script can be included inside other PHP scripts to minimize repeated code throughout the proxy.


What is the GD Library?

The GD library is used for dynamic image creation. From PHP we use with the GD library to create GIF, PNG or JPG images instantly from our code. This allows us to do things such as create charts on the fly, created an an anti-robot security image, create thumbnail images, or even build images from other images.

If you are unsure if you have GD library, you can run phpinfo() to check that GD Support is enabled.


GD Library

The GD Library is used to create and manipulate graphics via PHP. You can create an image from scratch, create thumbnails for your photos, add text to photos, resize images, recolor images, and more


Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!