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