How to Create a Simple Message Debugging Script for Your Website in PHP

Hello,

Whenever I am developing, I need to know where the script fails, what is currently doing, what values variables have, etc, therefore I always use a simple messages/errors script for debugging that will throw all the messages and errors to the top of the page.

Difficulty level: Easy

1. Create/Open a PHP file

2. Declare our variable $msg as an array

$msg = array();

3. To add new messages, just add the message to the array, like this:

$msg[] = "Whichever message you want";

You can add it for example in an if():

if($var == "yes") {
 $msg[] = "Var equals yes";
  } else {
 $msg[] = "Var doesn't equal yes";
}

I also use it to display MySQL errors in query execution:

if(mysql_query($query)) {
 $msg[] = "Query successfully executed";
  } else {
 $msg[] = mysql_error(); //Add the error to as an array element
}

You now get the idea of this simple script.

4. Now to display the messages, add at the top of your page this code:

<div style="width: 80%; text-align: center; margin-left: 100px; -moz-border-radius: 5px 5px 5px 5px; background-color: rgb(131, 247, 181); border: 1px solid rgb(40, 156, 90);font-size:14px;font-family:arial;">
    <ul>
        <?php foreach($msg as $msg) { print "<li>".$msg."</li>"; }?>
    </ul>
</div>

It will loop through the array and display all elements in a list, in a good looking green box.

5. Save and Upload.

You can now test your code. Believe, this little script is something I use EVERYTIME I am developing, it’s the most useful thing for a developer. Any question, comment, or whatever, please do not hesitate to contact me. I’ll be more than glad to assist you!

Best Regards,
Richi
Owner of Juapo2Services

Looking for quality Linux Web Hosting? Try out Arvixe web hosting and you won’t be disappointed.
Tags: , , , , , , , , , , , , , , , , | Posted under Programming/Coding | RSS 2.0

Author Spotlight

Richi González

Richi González

I started programming when I was 12. I'm a Developer and have been working with the Clip-Bucket script since it came out. I became a Liaison between Arvixe and Clip-Bucket, so I am here to assist you with anything you need regarding Clip-Bucket or any particular computer-related subject you need. Best Regards, Richi

Leave a Reply

Your email address will not be published. Required fields are marked *


× 9 = 72

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>