Explosive PHP Application

Published: February 10th, 2009 by:

Sometimes a complex PHP-built website can become tedious when dealing with minor changes to the template and HTML markup.  Changing, for instance, a set of links (navigation) or other integral parts of a website, can become quite the lengthy process.  In this article, I'll share a technique that I sometimes use to make something as simple as a website navigation a little more automated using an explosive function.


So maybe you’ve caught on to the references to explosions and are thinking that something’s about to get dangerous, and really, this application is quite safe and easy to use.  The function itself is properly named “explode”:


<?php

...

$arr = explode(delimiter, string [,limit]);

...

?>

Now you may be thinking: that’s all well in good but what in the world is a delimiter?  I thought the same way before I truly understood the function.  The delimiter is just the common separator between each element.  For instance, if we wanted to explode a sentence into an array with each element as a separate word from the string, the delimiter would be an empty space (” “).  The optional “limit” is a limit of the number of elements to allow to be created.  If it is left empty or set to “-1,” no limit will be set.  Otherwise, once the limit is reached, the last element will be the rest of the unexploded string.

Now this is all good and well unless you have an awesome application of the function, and believe me, there are many, but my favorite is the dynamic navigation idea.  I always get sick of having to add in all the extra template elements when altering a website’s navigation, and with this simple idea that can cease.

Here’s a basic run-down of what will happen:  first, we’ll define a string holding all the link names and source locations.  Then, we’ll explode that string to an array of all link name-source pairs which we will in turn explode individually to produce a great link.  Here’s an example:


<?php

...

$links = "Home:index.php,About Us:about.php,Contact Us:contact.php,Portfolio:portfolio.php";

$links = explode(",",$links);

echo "<ul>";

foreach ($links as $link) {

$link = explode(":",$link);

echo "<li><a href='".$link[1]."'>".$link[0]."</a></li>";

}

echo "</ul>";

?>

Be aware of the order in which we are exploding the elements.  If you explode incorrectly, you could end up with inaccurate pairs.  With this application, to update the navigation, instead of sifting through what could become complex HTML, you simply need to edit the “links” variable and let the PHP take care of business for you.  Nothing complex, but certainly useful.


3 Responses to “Explosive PHP Application”

  • Matt (aka teammatt3)

    “With this application, to update the navigation, instead of sifting through what could become complex HTML, you simply need to edit the “links” variable”

    It appears to me that sifting through that cryptic variable would always be harder than editing HTML (even if the HTML was complex). It is also difficult to debug if you make a mistake in the syntax.

    Aren’t there better ways to solve this problem?

    Include a page that has the basic HTML of the links wrapped in list item tags. (You could make it a function that prints HTML if you don’t want to access the file system).

    Why not use a regular array instead of a cryptic string?

    array(“Home” => “index.php”,
    “About Us” => “aboutus.php”);

     

  • Kurtis

    Sure, there are certainly alternatives, but in my opinion, none is more “cryptic” than the other. Your array solution and my variable solution are on the same level with what I was trying to achieve: making an easy-to-update solution. With my method, simply add a “,Text:href” pair to the end of the variable. With yours, simply add a “,’Text’ => ‘href’” pair to the end of the array. The simplicity is maintained across the board.

     

  • Andrew

    I think they both have their pluses and minuses. If I used the “explosive method”, I would probably allow line breaks after the comma. This could be achieved by putting array_map(‘trim’, $links); after $links = explode(“,”,$links); to take out any breaks from the elements, then you can do something like:

    $links = "Home:index.php,About Us:about.php,
    			Contact Us:contact.php,Portfolio:portfolio.php";
    

    It would certainly make it more readable if there were a couple dozen links.

     

Leave a Reply





Wordpress doesn't like it when you post PHP code. Go save your code at pastebin, and post the link here.

About the Author

Kurtis has been working with PHP for nearly four years, and he has moderate experience with MySQL as well as other programming languages, like Java and C++.