debug variable value cakephp - php

How can I print out some debug output, like the contents of an array variable? I'm using cakephp and eclipse and can't seem to be able to do this. I am not talking about logging errors, just print some variable value. It might be obvious and really easy to do, but I can't find a way to do it.
Thank you

There are no dumb questions for someone learning. So here are your options :
Anywhere in your code, place the following statement debug($var);.
This works in Controllers/Views and Models as well.
Alternative: use CakeLog::write(LOG_DEBUG, "..."); to log debug
values
To be complete, one should install the very helpful DebugKit plugin. Get it from here

First check in your core.php file inside config folder ..
debug mode is 2 or not..
Configure::write('debug', 2);
and you can print data , array ,
like this:-
debug($data);
pr($data);
print_r($data);
we have debuging kit also for cakephp, By this you can see your request, session, $this->data values,, url,

All you have to do is to use cakephp debug function like
debug($arrayname);
Before that you have to set debug mode to 2 at core.php under app/config folder
Here is more detail about the debug

Related

How to print to server console, not to browser or browser console [duplicate]

I'd like to debug some PHP code, but I guess printing a log to screen or file is fine for me.
How should I print a log in PHP code?
The usual print/printf seems to go to HTML output not the console.
I have Apache server executing the PHP code.
A lesser known trick is that mod_php maps stderr to the Apache log. And, there is a stream for that, so file_put_contents('php://stderr', print_r($foo, TRUE)) will nicely dump the value of $foo into the Apache error log.
error_log(print_r($variable, TRUE));
might be useful
You can use error_log to send to your servers error log file (or an optional other file if you'd like)
If you are on Linux:
file_put_contents('your_log_file', 'your_content');
or
error_log ('your_content', 3, 'your_log_file');
and then in console
tail -f your_log_file
This will show continuously the last line put in the file.
You need to change your frame of mind. You are writing PHP, not whatever else it is that you are used to write. Debugging in PHP is not done in a console environment.
In PHP, you have 3 categories of debugging solutions:
Output to a webpage (see dBug library for a nicer view of things).
Write to a log file
In session debugging with xDebug
Learn to use those instead of trying to make PHP behave like whatever other language you are used to.
Are you debugging on console? There are various options for debugging PHP.
The most common function used for quick & dirty debugging is var_dump.
That being said and out of the way, although var_dump is awesome and a lot of people do everything with just that, there are other tools and techniques that can spice it up a bit.
Things to help out if debugging in a webpage, wrap <pre> </pre> tags around your dump statement to give you proper formatting on arrays and objects.
Ie:
<div> some html code ....
some link to test
</div>
dump $tpl like this:
<pre><?php var_dump($tpl); ?></pre>
And, last but not least make sure if debugging your error handling is set to display errors. Adding this at the top of your script may be needed if you cannot access server configuration to do so.
error_reporting(E_ALL);
ini_set('display_errors', '1');
Good luck!
You can also write to a file like this:
$logFilePath = '../logs/debug.text';
ob_start();
// if you want to concatenate:
if (file_exists($logFilePath)) {
include($logFilePath);
}
// for timestamp
$currentTime = date(DATE_RSS);
// echo log statement(s) here
echo "\n\n$currentTime - [log statement here]";
$logFile = fopen($logFilePath, 'w');
fwrite($logFile, ob_get_contents());
fclose($logFile);
ob_end_flush();
Make sure the proper permissions are set so php can access and write to the file (775).
If you don't want to integrate a framework like Zend, then you can use the trigger_error method to log to the php error log.
Simply way is trigger_error:
trigger_error("My error");
but you can't put arrays or Objects therefore use
var_dump
You can use the php curl module to make calls to http://liveoutput.com/. This works great in an secure, corporate environment where certain restrictions in the php.ini exists that restrict usage of file_put_contents.
This a great tool for debugging & logging php: PHp Debugger & Logger
It works right out of the box with just 3 lines of code.
It can send messages to the js console for ajax debugging and can replace the error handler.
It also dumps information about variables like var_dump() and print_r(), but in a more readable format.
Very nice tool!
I have used many of these, but since I usually need to debug when developing, and since I develop on localhost, I have followed the advice of others and now write to the browser's JavaScript debug console (see http://www.codeforest.net/debugging-php-in-browsers-javascript-console).
That means that I can look at the web page which my PHP is generating in my browser & press F12 to quickly show/hide any debug trace.
Since I am constantly looking at the developer tools for debugger, CSS layout, etc, it makes sense to look at my PHP loggon there.
If anyone does decide to us that code, I made one minor change. After
function debug($name, $var = null, $type = LOG) {
I added
$name = 'PHP: ' . $name;
This is because my server side PHP generates HTML conatining JavaScript & I find it useful to distinguish between output from PHP & JS.
(Note: I am currently updating this to allow me to switch on & off different output types: from PHP, from JS, and database access)
I use cakephp so I use:
$this->log(YOUR_STRING_GOES_HERE, 'debug');
You can use:
<?php
echo '<script>console.log("debug log")</script>';
?>
You can use
<?php
{
AddLog("anypage.php","reason",ERR_ERROR);
}
?>
or if you want to print that statement in an log you can use
AddLog("anypage.php","string: ".$string,ERR_DEBUG_LOW);

How to use var_dump in joomla

My question might be little silly but please bear with me. I suppose var_dump should work anywhere in the code which calls its service but unfortunately i can't return anything if i use it in a controller, or model. ya it does work in the view/layout page.
I tried testing the following simple thing in one of my controller function and it returns nothing;
$foo = "bar";
var_dump($foo);
Please enlighten me!
I don't know Joomla, but in an MVC framework, the view expects data to come from the controller in a particular format, perhaps JSON or XML. When you call var_dump(), it will most likely mess up the syntax of this, so the application won't work. When you're debugging with this tool, you'll want to make use of the browser's console (Developer Tools or Firebug) to view what was returned. Go into the Network tab, select the URL of the controller, and then view the response data. There you'll see the output of var_dump().
For show a variable and pause the executing you have 3 options.
echo $variable;
print_r($variable);
var_dump($variable);
and you need to write die() after them to stop code and show your $variable.

How to write debug messages to Silverstripe log file

I've got a Silverstripe 3.1 site in development and want to write messages to the default log file - silverstripe.log
This is what we use to output variables or messages to the screen:
Debug::show($variable);
Debug::message("Debug message goes here");
What is the simplest way to output these to the silverstripe.log file? I've been looking through the documentation and can't find the correct approach: http://doc.silverstripe.com/framework/en/topics/debugging
You could do the following:
in mysite/_config.php
SS_Log::add_writer(new SS_LogFileWriter('silverstripe.log'), SS_Log::WARN, '>');
in your code:
SS_Log::log("Dammit, an issue with variable ".$var, SS_Log::WARN);
More at http://doc.silverstripe.com/framework/en/topics/error-handling
Also reading the code in framework/dev/Log.php will give you more insight on how priorities work.
PS: ensure to define 'silverstripe.log' in a folder writable by the apache user
The simplest solution is actually pretty close to your original attempt.
Debug::log("output");
This will output to a debug.log file in the site root.

CodeIgniter Pretty Debugging Output

Basically what I want to do is be able to set a switch somewhere: $debugging_mode = 0 or 1 that will allow me to attach a nicely formatted output to the bottom of my views.
I have written a library with a lot going on and I have some output like this:
echo var_dump(array_before_operation);
// do some operations
echo var_dump(array_after_opertion);
// do some more stuff
echo "We have exactly 3 matches!";
My question is what is the cleanest way to get pretty debugging output? ( I am aware of CI's profiler, but I still need something extra )
Where is a good place to set a global debugging mode? I know zend framework has a development or production setting in the .htaccess file, but I don't see that in CI
Should I use PHP's native output buffer to collect my debugging stuff? then how could I append that to the view?
Thanks for your ideas ladies and gents!
global debugging mode: use config class http://codeigniter.com/user_guide/libraries/config.html
cleanest way to get pretty debugging output: text file

NetBeans-Xdebug works, but won't expose some PHP variables

UPDATE -- working on getting WAMP with phpDeveloper/Xdebug going. I still want NetBeans -- I just want to compare, see if I get some insights.
I am using NetBeans 6.9 with LAMP and Xdebug to work on PHP code. The Variables display works well, but lately it works less well. For example below, $authorized should be visible in the variables pane below the code and should expose its value. But it doesn't show, nor its value, and mousing over the code doesn't help. (The $this object is showing and it does go on and on, but $authorized isn't in there, and it wouldn't make sense if it were.)
This behavior is consistent. Maybe it's a function of the complexity of the code? Or rampant object usage? it seems to have started when I took up CodeIgniter.
Of course the variables are hidden when I need them most ... or so it seems to the poor human. What am I missing?
NetBeans debugger http://themanthursday.com/wiki/Debugger_Display.png
There's a better example below. When I'm stepping through this code, Variables displays only Superglobals and $this, just as in the picture. I can't see any values, even mere strings.
(Nagging thought: I bet the $CI SuperObject has something to do with all this ...)
class Product_documents {
function getProductImage_all($id)
//Return an array of all documents for this product
{
$imgPath = $this->_getProductImage_folder($id);
$arrayPossibleFilenames = $this->_getProductImage_possible_files($id);
foreach ($arrayPossibleFilenames as $imgFile) {
$imgPathFull = $imgPath.$imgFile;
$file_exists = get_file_info($imgPathFull);
if ($file_exists)
{
$arrayFilesPresent[] = $imgPathFull;
}
}
return $arrayFilesPresent;
}
}
Right click on the variable pane. Select "Filters". You will find the secret.
Came across this site that has a very nice link to an Xdebug page that walks one through the process of upgrading Xdebug by compiling a 'more latest' version:
http://icephoenix.us/php/xdebug-doesnt-show-local-variables-in-komodo-netbeans-or-eclipse-pdt/
Variables inside by objects/classes are showing up again! Yeah!
No watches, no 'this may make Xdebug freak out' messages - just good ol' variables that now fully expose the failure of my solution... (haha).
David
I've seen stuff like this before in Netbeans. I expect it's just a bug involving Netbean's interaction with XDebug. One possible workaround that I've seen before is adding a "Watch" for the variable that you can't see. For your example, you could go to the "Watches" tab and type in $authorized. It should show up once it has been set.
I think it comes down to the singleton pattern that is implemented in CodeIgniter as "Super Object". I never have restarted this project to test Kamal's idea. Shortly after he posted, I concluded the singleton was the reason (I did not try to guess whether Kamal has the solution or not). Thus my response to this post.
(2015) In php.ini under [xdebug], set xdebug.show_local_vars=1 if you want all the local variables in debug mode.
Try initializing $authorized to bool false.
I've seen Netbeans not show me variables initialized with a return value from a function without a doctype, but it's hit or miss enough to not be make a pattern out of.

Categories