162

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.

5
  • Is your script running in a web browser or in a console (aka parsed using php.exe rather than apache/iis)? Commented May 21, 2011 at 3:56
  • You should look at this question, has a bit of useful logging code: stackoverflow.com/questions/5769537/…
    – Jess
    Commented May 21, 2011 at 3:57
  • what language are you comming from? Just so you php is very different then other compiled languages
    – Ibu
    Commented May 21, 2011 at 4:02
  • 2
    @lbu: indeed.. I didn't expect, printf question would get this many upvotes...
    – eugene
    Commented Oct 24, 2014 at 12:44
  • error_log($string); Commented Aug 8, 2018 at 19:44

15 Answers 15

215

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.

3
  • 1
    + oned for mentionning that, btw you can also set the value of error_log to 'php://stdout' when debugging a console app and have the error messages appear straight to console, ie: error_log("You messed up!", 3, "php://stdout"); Commented May 21, 2011 at 12:38
  • 4
    +1 It took me too long to find this answer. This is all I really was looking for. I couldn't figure out how to see what my array contained (working in Drupal).
    – SteveS
    Commented May 7, 2014 at 14:31
  • I would also suggest using var_export($foo, true) instead of print_r($foo, true) if print_rdoesn't get you the type information you need.
    – Ben
    Commented Dec 14, 2017 at 17:08
192
error_log(print_r($variable, TRUE)); 

might be useful

1
  • 3
    Unfortunately, this is the most convenient solution. Xdebug is to heavy to run it all the time. Voted for you Commented Dec 23, 2014 at 15:57
30

You can use error_log to send to your servers error log file (or an optional other file if you'd like)

0
27

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.

1
  • 1
    One may also append to a log file using file_put_contents('your_log_file', 'your_content', FILE_APPEND).
    – Curt
    Commented Jan 5, 2021 at 9:44
7

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:

  1. Output to a webpage (see dBug library for a nicer view of things).
  2. Write to a log file
  3. In session debugging with xDebug

Learn to use those instead of trying to make PHP behave like whatever other language you are used to.

6
  • 34
    As one who spends a lot of time writing console apps and various non web scripts, I will politely disagree with that. Commented May 21, 2011 at 12:33
  • I do work a lot maintaining and developping console applications and in my bad english interpretation of the question had assumed he was looking to debug a console app. Xdebug is fine and very handy though I do like debugging info right in my editor Commented May 23, 2011 at 1:14
  • 1
    @Stefgosselin: Netbeans does pretty much the same, although the best I have seen with xDebug was with Aptana.
    – Sylverdrag
    Commented May 29, 2011 at 5:23
  • 1
    I use xDebug on Netbeans and it works fine most of the time. But, there are times when you need to log stuff on a file.
    – cosan
    Commented Mar 18, 2017 at 13:23
  • try these too..you'll be able to do more!stackoverflow.com/a/44050914/2053479
    – ganesh
    Commented May 18, 2017 at 14:43
5

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 ....
      <a href="<?php $tpl->link;?>">some link to test</a>
</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!

3

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).

2

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.

2

Simply way is trigger_error:

 trigger_error("My error");

but you can't put arrays or Objects therefore use

var_dump
1
  • trigger_error is probably the best way to write to your error logs. You will thank yourself when you want to modify say the log lines with some custom data later.
    – Anshul
    Commented Aug 3, 2012 at 4:52
1

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.

1

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!

1

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)

0

I use cakephp so I use:

$this->log(YOUR_STRING_GOES_HERE, 'debug');
1
  • I understand this isn't a cake question, but oftentimes when I look for an obscure answer that is in a common area of various frameworks or languages I will stumble upon and extrapolate for my needs an answer that is given.
    – mmv_sat
    Commented Nov 21, 2020 at 22:50
-3

You can use:

<?php
echo '<script>console.log("debug log")</script>';
?>
2
  • 3
    this has much overhead. why invoke javascript for a simple error output?
    – clockw0rk
    Commented Aug 30, 2018 at 10:29
  • this seems to assume the output is going into the browser properly, which may not be the case if it's doing some sort of AJAX/REST servicing which doesn't update the document.
    – Michael
    Commented Mar 29, 2022 at 16:46
-9

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);
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.