Home arrow PHP arrow Page 4 - JVís Power Tips for PHP (2)

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
  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

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

Here is another way to do the same thing without if statements:


  $p = &$_POST;

$B[][1] = "<b>";  $B[][1] = "</b>";
$I[][1] = "<i>";  $I[][1] = "lt;/i>";
$U[][1] = "<u>";  $U[][1] = "</u>";
$S[][1] = "<s>";  $S[][1] = "</s>";

echo    $B[0][$p['bold']]
  . $I[0][$p['italic']]
. $U[0][$p['underline'í]]
. $S[0][$p['strike']]
. $p['chat']
. $S[1][$p['strike']]
. $U[1][$p['underline']]
. $I[1][$p[Ďitalicí]]
. $B[1][$p['bold']];


I wonder how many people get what's going on here at first glance! In short, this code is using arrays instead of if statements to determine exactly what to print.

For the moment we will ignore the top line:

$p = &$_POST;

..and skip to this line (and explain it):

$B[][1] = "<b>"; $B[][1] = "</b>";

First thing to note is that this is really two lines of code. PHP doesn't care about what your code looks like. The only thing that is important to PHP is that you have a ; after a valid bit of code. So lets split this once again to this:

$B[][1] = "<b>";

What is happening is that a new multi-dimensional array is being created (on the fly) with the value of <b> in the index of [0][1];

The operator [] (after a variable name) does the following:

  1. If no array exists of that name then it creates a new one with index element of [0]
  2. If an array exists of that name then it adds a new element to the end of the array with index of [last index+1]
It is also interesting (and very useful to know) that you can create completely arbitrary arrays on the fly, be they multi-dimensional or not. For example:

$new_array['human']['num_legs'] = 2;
$new_array['human']['num_arms'] = 2;

N.B. Sometimes it is convenient to use numbers so that you donít have to type the 's. (If no one else is reading your code that is) Like so:

$new_array[0][0] = 2;
$new_array[0][1] = 2;

Anyway back to the point at hand. Let's re-visit that line:

$B[][1] = "<b>"; $B[][1] = "</b>";

The first half of the line creates the array of:

$B[0] array
[1] = "<b>";

Öand the second half of the line creates the array of:

$B[1] array
[1] = "</b>";

Which is the same as saying:

$B[0][1] = "<b>";
$B[1][1] = "</b>";

Now letís go back to our form. Where it says:

<input type=checkbox name=bold value=1>

When the form is submitted the value $_POST['bold'] will either be 1 or it will be non existent. So letís look at how we can use that to decide what to display (in its most simple form):


$B[0][1] = "<b>";
  echo $B[0][$_POST['bold']];


If $_POST['bold'] was equal to 1 then what the echo statement is really saying is this:

  echo $B[0][1];

Since we have defined the array $B[0][1] to equal <b> the output will be, surprisingly enough <b>;

If $_POST['bold'] did not exist, then what the echo statement is really saying is this:

  echo $B[0][];

Since $B[0][] has not been defined and does not exist, nothing will be output. So now let's look at the big chunk of code again. This time when you read through it, it should make perfect sense...


  $p = &$_POST;

$B[][1] = "<b>";  $B[][1] = "</b>";
$I[][1] = "<i>";  $I[][1] = "</i>";
$U[][1] = "<u>";  $U[][1] = "</u>";
$S[][1] = "<s>";  $S[][1] = "</s>";

echo    $B[0][$p['bold']]
  . $I[0][$p['italic']]
. $U[0][$p['underline']]
. $S[0][$p['strike']]
. $p['chat']
. $S[1][$p['strike']]
. $U[1][$p[''underline']]
. $I[1][$p['italic']]
. $B[1][$p['bold']];


One final note about this method: I did this $p = &$_POST; so that I could access the post variables with the name $p. It makes the code much shorter (and less effort) to type.

So the big question. Is this array way better than the if statement way? Personally I donít think it is in this instance. Never the less, I do find myself using this technique from time to time. It is most valuable when there are many options that need to be chopped and changed. A more useful example would be if you had complex chat formatting needs like so:


// On the form emotion is a drop down with values 1 - 3
$emotion = array

// On the form colour is a drop down with values 1 - 3
$color = (

echo   "<font color={$color[$_POST['color']]}>Ē
. "Justin "
. {$emotion[$_POST['emotion']]}
. "</font> ";


If you did the same thing with if statements there would be a lot more code.

You may ask "Why don't you just put the emotions and colours in the HTML drop downs? Then you wouldn't need the arrays in the first place!"

The reason is because if I was inserting this chat into a database (as you do with chat engines) then people would be able to put mySQL into the chat form and potentially perform SQL injection attacks. This way I know that I am expecting the values 1, 2 or 3 and there is no chance of an injection because the logic is totally abstracted from the database insert.

Right. Onward to the next solution for the same problem (i.e. evaluating bold, italic etc.).
blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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