Looking Inside Code

When starting out on your first bit of coding, quite often things don't work as you expect. In order to 'see' what is happening as you are building your mod, there are php and html code that will allow you to view the values in variables at any stage of your code, within your browser.

The following are invaluable troubleshooting tools and this exercise will demonstrate their use during code construction. They can equally be used to troubleshoot code supplied from elsewhere that isn't operating as expected.


All that follows should be done in a test environment only!

To begin with, place the following in index.php, just above the <?php center() ?> function call:

<?php exercise(); ?>

and then at the bottom of your snews.php:

function exercise(){
}

Everything we do from now on, will be between the { braces } and tested in your web browser. So, open a browser window to your test site.

We will be using the infoline function, tags(), to get a variable to work with.
The first entry after the { is:

$a = tags('infoline');

If you refresh your browser, there is no change. You can't see what $a is holding, if anything at all, which means... is there anything in it?

echo

Let's echo it to screen. Next line:

/*screen*/ echo $a;

...and the result in our browser, is:

,readmore,comments,date,edit,

However, there is more in $a, but it is html which the browser is using it as part of the page.

str_replace

We can, at this point, break the html, using str_replace to replace the opening < of the html tags, without affecting the content of $a. We then alter our second line to like this.

/*screen*/ echo str_replace('<','&lt;',$a);

The new code says: replace all instances of < with the alternate code &lt; (which the browser treats as a character to display) that is found in $a. Now the browser shows us:

<p class="date">,readmore,comments,date,edit,</p>

And that is the full contents of $a, which you can verify, by looking in your snews.php function tags().

The function so far

So far our function looks something like this.

function exercise(){
$a = tags('infoline');
/*screen*/ echo str_replace('<','&lt;',$a); }

The next line will take $a and create an array, $b, with the content.

$b = explode(',',$a);

If you now echo that with the next line,

/*screen*/ echo $b;

you get in your browser:

Array

... which is not helpful at all, and certainly doesn't tell us if the values that are supposed to be in there, are actually there. Remove that echo line, and we'll replace it with something else.

print_r

Place this as your next line instead:

/*screen viewer*/ print_r($b);

This produces a browser output like:

Array ( [0] => 

[1] => readmore [2] => comments [3] => date [4] => edit [5] =>

Why the blanks and new line? That's the html in $a affecting display again. We know the html is there, and if we want to show it in the array, we will have to alter $a temporarily. Change the str_replace line to:

/*screen-altered $a*/ echo $a = str_replace('<','&lt;',$a).'<br>';

to give us this in the browser:

Array ( [0] => <p class="date"> [1] => readmore [2] => comments 
[3] => date [4] => edit [5] => </p>

Which shows our html clearly. When you start to manipulate the values in the array and develop final output using it, you will need to remove this altered line else you will get odd results.
Now, the above result is ok, but it is not real clear what is there, and with more complex arrays, it will be very confusing. If you look at the source of your browser page, you will see that it is layed out differently than you actually see in the browser. We will replicate this right now.

<pre></pre>

What we are going to do now, is to surround the print_r line with <pre> and </pre> tags. So, if you make the print_r line like this:

/*screen*/ echo '<pre>';print_r($b);echo '</pre>';

and then refresh your browser, you will see this:

Array
(
    [0] => <p class="date">
    [1] => readmore
    [2] => comments
    [3] => date
    [4] => edit
    [5] => </p>

)

... and this is much easier to read!

Recap

Final function

function exercise(){
$a = tags('infoline');
/*screen-altered $a*/ echo $a = str_replace('<','&lt;',$a).'<br>';
$b = explode(',',$a);
/*screenr*/ echo '<pre>';print_r($b);echo '</pre>';
}

Well, that is the end of this little exercise, but if you want some code to practice on, try it out on our Mod Example Image Rotator.

With these php and html tags, you will be able to work out your own process of mod development and debugging. Viewing the contents of variables as you progress, you can verify that each procedure in your code is operating as expected. If it isn't, then you know at what point something has gone wrong, and have a starting point for investigation.
When you have finished checking your code, you then go through and remove any redundant 'viewing' code, which is easily found because of the comment labels that you inserted.
You did put them in didn't you?

Don't forget to remove this function from snews.php, and the line in index.php.

23.02.2007. 04:04

GET STARTED

sNews requires PHP, MySQL and mod rewrite. If your server meets those requirements, get started and learn how to install sNews on your website in 3 easy steps.

LEARN

Browse through our help center and learn about template tags and how to simply create your own theme. Dig into sNews and customize it to behave the way you want.

EXTEND

sNews can be a simple blog tool and a full blown CMS. Customize it with addons, mods or play with different themes.

DISCUSS

Talk about sNews on our Forum and share your expirience.