PHP Debugging question

I am new to PHP, so this is a newbie question. Let's say I have this code:

$.ajax({
	url: "somepage.php",
        .
        .
  })

Open in new window


"somepage.php" is a page that accesses the database and returns results via an "echo" statement.

My question is, what PHP commands do PHP developers use to debug "somepage.php"? For example, let's say I just wanted to see the value in a variable, how do I output it the way you'd use console.log() in JavaScript? I can't use "echo" because PHP will treat it as output being sent to the calling page.

Thanks.
elepilAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
If you use Firefox you can install FirePhp, an addon for Firebug. Once installed, FirePHP let's you to do exactly what you want:

$firephp->log('$variable');

to print the values in the console, exactly as it be javascript.
Otherwise, if you find problem, you can serializaze your variables and echo them as result of the script and use javascript to put them in the console.
In addition, unsing the console, you can see the responses of the php scripts just echoing them in the script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
To Marco Gasi. You said:

Otherwise, if you find problem, you can serializaze your variables and echo them as result of the script and use javascript to put them in the console.

How do I serialize my variables?
Marco GasiFreelancerCommented:
You can just create an array with the variables you want to debug and then echoing it using json_encode:

$arr = array("firstname" => $firstname, "lastname" => $lastname, and so on);
echo json_encode($arr);

Open in new window


Then in your javascript:

$.ajax({
	url: "somepage.php",
        . ,
        . ,
        dataType: 'json',
        success: function(result){
             $(result).each(function(key, value){
                  console.log(key + ' = ' + value);
             }
        }
        ...
  })

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

elepilAuthor Commented:
Marco, I tried to install FirePHP. The instructions in firephp.org talk about a FirePHPCore folder that I am supposed to put in my application root. Problem is, I can't find that folder! When I installed it in my Firefox, it never asked me for any install directory, it just added FirePHP, and I can see it as an add-on. I tried doing a disk search through Windows Explorer for "FirePHPCore" and found nothing.

Where the heck is that folder??
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Are you familiar with using the browser console?    All the browsers have one.  What I do is run the page with the console open and check the network section.  

console
https://developer.chrome.com/devtools/docs/console
https://developer.mozilla.org/en-US/docs/Tools/Web_Console

Of course you can also just run your page directly with some sample post or get data too.
Marco GasiFreelancerCommented:
Hi elepil.
Go here to download FirePhp: http://www.firephp.org/HQ/Install.htm
You can follow this tutorial: I followed it me too.
elepilAuthor Commented:
Marco. I've already installed both FireBug and FirePHP. It is apparent to me from code samples that I need FirePHPCore, something I could not find nor download (because one of the links in the page you referred me to led to Page Not Found).

It's not that I don't know how to use it, because it seems simple. I think all I need to do is put FirePHPCore folder somewhere in my application root, then "include" it in any page I want to debug, then use its FD::log/info/warn/error static methods. My problem is I can't find FirePHPCore.
elepilAuthor Commented:
To Scott Fel. Thanks for responding.

The only times I've had to use Network was when I needed to look into headers information or when I needed to see the incoming data in its raw format. If I were debugging an "off-by-one" situation within a small piece of code in the page, I don't see how Network would help.
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
The network also shows the response generated. There are tabs for header,preview, response,timing.   However, I find it easier to just surf to the somepage.php and fiddler with different querystrings to view the response.
Tom BeckCommented:
I'm just following this question to learn something new so no points for this, but the link Marco provided worked for me. I was able to download necessary files for FirePHPCore (attached).
FirePHPCore.zip
Marco GasiFreelancerCommented:
That's way I posted that link: just click on Download link and you'll get all needed files.
elepilAuthor Commented:
I found a good one called ChromePhp. But FirePhp is something to keep in mind. Thanks to all who responded.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.