John Smith
John Smith

Reputation: 8791

How can I find out the value of a variable while writing PHP code (for debugging)?

I'm writing in symfony and I'm in one of the actions.class.php files.

Normally I would just echo the variable's value but because this is an actions class module, I can't do that because I can't output to the page.

I was thinking of using FirePHP but it involves installing it server-side and I'd like to be aware of an easier or built-in solution.

Upvotes: 2

Views: 99

Answers (3)

Michal Trojanowski
Michal Trojanowski

Reputation: 12322

Emailing logs works for sure but there are some better, easier and faster ways to do that ;)

In symfony you have access to the Logger which will allow you to log anything. In prod environment by default it is disabled (of course you can switch it on) but in dev it's not only available in the log files but also prints in the Sf Web debug bar.

Inside an action use:

$this->getLogger()->debug('My $variable value is now: '.$variable);

You can also use other levels of logging (->warning(''), ->err(''), ->crit(''), etc.). Of course you need to change the variable to string if it is an array or an object.

In fact you can echo and var_dump things while in the action. They will show in your page (though mess badly with the layout as you're displaying stuff before the actual template).

Remeber that echo works only with strings and numbers. If you try to echo a boolean or array you will see nothing. If you try echo an object it's __toString() method will be used or an error raised.

Upvotes: 2

SchautDollar
SchautDollar

Reputation: 348

Email yourself throughout the code. You could either send one at the end of the script or throughout. There are benefits to both. Emailing throughout will easily let you know where something went wrong if you don't get an email and emailing at the end save overhead(doesn't seem like it would be a problem here though).

Upvotes: 1

Alex
Alex

Reputation: 1103

If you have access to the server's log files, you might want to look into PHP's error_log function. It will print a string into PHP's error log and then you can check the log file.

-edit-: error_log also allows you to email yourself the message. But that's silly.

Upvotes: 1

Related Questions