Home arrow PHP arrow Page 6 - JV’s Power Tips for PHP (2)
PHP

JV’s Power Tips for PHP (2)


In this article Justin explores day to day programming techniques that can help to make a PHP developers life just that little bit easier.

Author Info:
By: Justin Vincent
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
February 02, 2003
TABLE OF CONTENTS:
  1. · JV’s Power Tips for PHP (2)
  2. · The Problem
  3. · Method 1
  4. · Method 2
  5. · Method 3
  6. · Method 4
  7. · Conclusion

print this article
SEARCH DEVARTICLES

JV’s Power Tips for PHP (2) - Method 4
(Page 6 of 7 )

Firstly.

Lets dim the lights.

(turns on a suspenseful drum roll)

The ringmaster enters.

The crowd gradually quietens.

Laadeeesssss aaaannnddddd geeennnntttlllmmeeeennnn ([echo] men men men).

All thheeee wayyyy frommm jjjaaaaaiiii veeeeessssss braaaiiinnnnnn (ainn ainn ainn)

Preeessseeennttttiiinnggggggg foorrr yourrr inttteerrlleectuall ennntertainnnment

Theeeee onneeeee, theee onnnlyyyyyy, thheee veerrryyy besssst…….

<?php

  $p = &$_POST;

  $fmt = array (  
      "bold" => "b",
      "italic" => "i",
      "underline" => "u",
      "strike" => "s"
     );

  foreach ( $fmt as $key => $val )
  {
    if ( $p[$key] )
    {
      $p[‘chat’] = "<$val>{$p[‘chat’]}</$val>";
    }    
  }

  echo $p[‘chat’];

?>


Lets break this function down part by part and see why it is a better solution for the task at hand (bearing in mind that for other tasks the above concepts might actually be better solutions).

Starting with this:

  $fmt = array (  
      "bold" => "b",
      "italic" => "i",
      "underline" => "u",
      "strike" => "s"
     );


As I try to write and explain this I am finding it quite hard, which is strange because it is just an associative array, quite a simple construct. What I am finding hard to put into words is all the reasons why I chose this construct and constructed it in this way. There are actually quite a few reasons, most of them being subtle. Ones that come from lots of practice working with PHP and HTML.

Here are my reasons as I type them:

  1. On the form there are four incoming variables. I want an easy way to store those variable names in my PHP script so I can easily access them.
  2. Each of the incoming variables can evaluate to true or false. If they evaluate to true then I want to echo a value. I need a way to tie the incoming value to the echoed value.
  3. I don’t know how many types of false/true format options I might need in the future. Therefore ideally I want to define a construct that is very easy to add new formatting options to (without altering any other code). E.g. To add a quote function all I have to do is add “quote” => “blockquote”
  4. I know that the function foreach is one of the most useful things about PHP. I want to make use of it in this case.
  5. During my iteration of each array element I want to be able to easily access the array key/value pairs.
Now on to the foreach loop:

  foreach ( $fmt as $key => $val )
  {
    if ( $p[$key] )
    {
      $p[‘chat’] = "<$val>{$p[‘chat’]}</$val>";
    }    
  }


Or more specifically:

foreach ( $fmt as $key => $val )

This is an easy way to loop through each element of an array and then perform any number of actions within the following brackets. If we were to write this with sensible variable names it might make more sense:

foreach ( $element_of_this_array as $elements_key => $elements_value )
{
  echo “$elements_key = $elements_value“;
}


In our case if we were to do an echo like so:

  foreach ( $fmt as $key => $val )
  {
    echo “$key => $val<br>”;
  }


The output would be:

bold = b
italic = i
underline = u
strike = s


So that makes the next part easy to understand:

if ( $p[$key] )

Because what we are really doing is saying this:

if ( $p[‘bold’] )   <- first iteration
if ( $p[‘italic’] )  <- second iteration
if ( $p[‘underline’] )  <- third iteration
if ( $p[‘strike’] )  <- fourth iteration


Then if the if statement were to evaluate to true all four times what we would really be saying is:

$p[‘chat’] = "<b>{$p[‘chat’]}</b>";
$p[‘chat’] = "<i>{$p[‘chat’]}</i>";
$p[‘chat’] = "<u>{$p[‘chat’]}</u>";
$p[‘chat’] = "<s>{$p[‘chat’]}</s>";


Or looking at one iteration as a whole:

if ( $p[‘bold’] )
{
    $p[‘chat’] = "<b>{$p[‘chat’]}</b>";
  }


Note that within the defining associative array we didn’t need to put the full HTML <b> and </b>. That is because we know that all HTML format commands have <> and </> and there is no point using redundant code.

Now that we know all of the above lets have a look at the final solution once again. It should make perfect sense!

<?php

  $p = &$_POST;

  $fmt = array (  
      "bold" => "b",
      "italic" => "i",
      "underline" => "u",
      "strike" => "s"
     );

  foreach ( $fmt as $key => $val )
  {
    if ( $p[$key] )
    {
      $p[‘chat’] = "<$val>{$p[‘chat’]}</$val>";
    }    
  }

  echo $p[‘chat’];

?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials