Basic Debugging

Posted by on in Blogs
The Debugger is the perfect tool to locate the source of any logic error in your application. You can use it to get control of the execution flow of your scripts while you watch the value of variables and expressions, allowing you to track the data until the point when something unexpected happens.

Now, lets see some code:

define('YES', 0); // What strcmp() returns for a string match.

$pet = $_GET['pet'];
$pet = strtolower($pet); // strcmp() is case-sensitive.
$isCat = strcmp($pet, 'cat'); // Save the return value of strcmp().

if($isCat = YES) { // Check if given pet is a cat.
echo "You have a cat!";
} else {
echo "Your pet is not a cat.";


This is the code we are going to test. You might have already spotted the error, which is a common PHP logic error, but do not tell anyone! ;)

This script expects the ‘pet’ variable to be set as an URL parameter, and it prints a different string depending on whether ‘pet’ is assigned the string ‘cat’ (case-insensitive) or a different string (if any).

Running Into the Error

With that script open in RadPHP, lets go to Run > Run to get the script rendered on a web browser.

Since we did not pass it ‘pet’ on the URL parameters (we will later see how to do that from RadPHP), so far the script is behaving as expected. Now, lets edit the URL parameters and give ‘pet’ the value ‘cat’ so the script returns the other message. This time we can do this from the web browser itself to save time, later we will do it from RadPHP.

The message is still the same, while the other message should have been returned instead. There is our logic error. Time to locate its source using the Debugger.

Custom URL Parameters

In order to use the Debugger on a script, you must run that script from RadPHP. The Debugger will not work if we call the script from a web browser. Since the error happens when we run the script with ‘pet’ parameter set to ‘cat’, we will need to customize the URL parameters passed to the script when running it from RadPHP.

Go to Run > Parameters… > Debugger, and set the Parameters field to ‘pet=cat’. Then, click OK.

Start the Execution Flow Control

Now it is time to start the execution flow control using the Debugger.

To start execution and pause it right on the first line, you can either go to Run > Step Over or Run > Trace Into, but we should start by examining the code which directly causes the error, which in this case is the expression on the if statement. We can take the execution flow directly to that line by clicking it (so the text cursor is placed on it) and going to Run > Run to Cursor.

Value on Hover

Now, lets have a look at the value that those two expressions (the variable $isCat and the constant YES) have at that point of execution. To do so, you can just hover them, and you will get their value on a tooltip.

They match, they are both 0, as expected. Why did it fail before and is working now? (remember you do not know what is going on). In any case, 0 is obviously equal to 0 so, for whatever reason, this time the if statement will return true.

Continue Execution

Yet I can not believe it suddently works. In fact, I need to see it by myself, see it print the right message this time.

To continue the execution flow, you can use the same commands that let you start its control in the first place. In this case, we need to go to the next step of execution, for which we can use either Run > Trace Into or Run > Step Over. The former, when calling a function or including a file, will take us to the included script or the script where the function is defined. The former will go directly to the next line of execution in the current script.

In this case both would work the same way, but lets go with Run > Step Over.

What the… I’m speechless, I never expected that to happen. You neither, did you? :)

Stop Debugging

Ok, if that if statement can not evaluate that expression right, I will get RadPHP to evaluate it.

First, lets stop debugging. Go to Run > Program Reset.

And lets start the debugger again, get it to that line of code, and check the values are still both 0. They are.

Evaluating Expressions

But this time, instead of continuing with the execution, lets make sure that expression is evaluating to true at that point of execution.

Right-click on the Code Editor, and go to Debug > Evaluate/Modify. Then enter the expression on the Expression field ($isCat = YES), and click Evaluate.


I don’t understand

Anyone can make a mistake, and using = instead of == or === is one of the most common ones with PHP. It is perfectly possible for someone to go through all the steps above, and still don’t realize what the cause of the error is. In fact, I bet there are a lot of PHP forum threads out there covering this issue.

Just remember, if you ever find youself at a dead end while programming with RadPHP, our forums are always open, and there is a lot of nice people around :)

Advanced Debugging

The techniques we used here to debug our code are perfect for short scripts, and fine on most situations you will run into. Yet, sooner or later you will need more advanced debugging features, which can save you a lot of time: breakpoints and watches.


  • Guest

    [...] Source ch_fluidH = 1; ch_nump = "4"; ch_client = "baguz"; ch_width = 550; ch_height = "auto"; ch_type = "mpu"; ch_sid = "Chitika Default"; ch_backfill = 1; ch_color_site_link = "0000cc"; ch_color_title = "0000cc"; ch_color_border = "ffffff"; ch_color_text = "000000"; ch_color_bg = "ffffff"; Jobs by Indeed Posts Related to Basic DebuggingHow to Fix WordPress Vulnerabilities from timthumb.phpDo you ever know about Timthumb.php ? Timthumb.php is a tool to re-size images and create to be thumbnail in your website, timthumb.php is used ...Security Scanner Procces in your siteThis Script for your have site or hostting reseller but u don't have ssh account for check your prossesing i your hosting service or server.. ...XML Sitemap PHP scriptRecently, while working on the site for my father in law (in Dutch), I wanted to create an XML sitemap for the many publications on his ...Programming Alternative to Captcha on an ASP Web Form Using a Hidden VariableHands up who thinks “captcha”, an image based challenge-response test to ensure a response is not generated by a computer, is a big pain in ...[How-To] Protect Your Blog From Adsense Invalid ClicksThese days hundreds of Adsense accounts are banned every day. Google takes hard action against invalid clicks. If you read TOS in adsense website you ...Advanced PHP Error Handling via htaccess  In my previous article on logging PHP errors, How to Enable PHP Error Logging via htaccess, we observed three fundamental aspects of preventing, preserving, ...Install PHP on Apache for the Debian 6.0 (squeeze)Here we are going to install Apache 2.2.19 and php 5.3.8 from source code for the Debian GNU/Linux 6.0 system (codename "squeeze"). There some requirements ...PHP IncludeWithout understanding much about the details of PHP, you can save yourself a great deal of time with the use of the PHP include command. ...Tutorial Batch Programming IWaaaah…sekarang dah bisa tenang soalx UAS udah selesai jd disempetin buat nulis artikel. Buat yang masih UAS semangat coy..kl pengen jawaban smsin-aq aja, ntar aq ...Posting Binary File Via a Proxy Using PHP CurlDon’t ask me why I had to do this, but at work today, I had the need to post files up to a remote server ... Tags: code lt, custom url, debugger, execution, expressions, logic error, match, pet cat, pet pet, Scripts, strcmp, url parameter, url parameters, variables, web browser /* */ /* */ /* */ « Adfly blocked by facebook [...]

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: