Php где посмотреть ошибки

I can guarantee you, I am not the only person who has been driven to madness at least once in a frustrating search for a log file. It seems like it should be the easiest thing to find in the whole system.

A definitive guide on where the PHP error log is stored would be a complicated bit of work. The official PHP manual does not even try to address the whole topic, because there are dependencies on systems outside PHP, such as the operating system (Linux vs. Windows, which distribution of Linux), including settings within Windows and Linux that affect the name and location of the PHP error log.

Until someone takes the time to write a complete, cross-system guide, the best you are going to get is general directions where you can inquire. Every PHP developer has had to endure agony in this pursuit, with one exception. If you work in one place and the information is provided when you first need it, then you have the information need forever, that is, until you find yourself in a new working environment. There are such fortunate people.

If the information is not given to you on a silver platter, so to speak, you have some hunting to do. The hunt is not the longest you will face in your career, but it is not the simplest either.

As is evident from the many answers already posted, a smart place to begin is the output of phpinfo(). To view it, create a PHP file containing this:

<?php
    phpinfo();

Either browse to that file or run it from the command line. If you do both, you likely will find the error_log is in different places, depending on command line vs. web server use of PHP. That is because the PHP interpreter that runs on a web server is not the same PHP interpreter that runs from the command line, even when the command line is on the same machine as the web server. The answers already posted in here mostly are making an unstated assumption that PHP is running as part of a web server.

Sample output from phpinfo() via web server and web browser

The default for error_log is no value

Default error_log in php running as an Apache mod.

Whatever the value is, it comes from the php.ini files used to configure PHP. There can be many php.ini files. Finding your way among them is confusing at first, but you do not need to deal with this to find your PHP log.

If the output from phpinfo() shows a full path to a file, that is where the log is. You are lucky.

The trick is there usually is not a full path indicated in phpinfo(). When there is not a full path, the location depends on:

  1. Whether error_log is no value. If it is, the log file location will depend on the operating system and the mode PHP is running. If PHP is running as an Apache module, on Linux the log often is in /var/log/apache2/error.log. Another likely spot is in a logs directory in your account home directory, ~/logs/error.log.

  2. If there is a file name without a path, the location depends on whether the file name has the value syslog. If it syslog, then the PHP error log is injected into the syslog for the server, which varies by Linux distribution. A common location is /var/log/syslog, but it can be anywhere. Even the name of the syslog varies by distribution.

  3. If the name without a path is not syslog, a frequent home for the file is is the document root of the website (a.k.a., website home directory, not to be confused with the home directory for your account).

This cheat sheet has been helpful in some situations, but I regret to have to admit it is not nearly universal. You have my condolences.

Enter image description here

(PHP 4, PHP 5, PHP 7, PHP 8)

error_logSend an error message to the defined error handling routines

Description

error_log(
    string $message,
    int $message_type = 0,
    ?string $destination = null,
    ?string $additional_headers = null
): bool

Parameters

message

The error message that should be logged.

message_type

Says where the error should go. The possible message types are as
follows:

error_log() log types

0 message is sent to PHP’s system logger, using
the Operating System’s system logging mechanism or a file, depending
on what the error_log
configuration directive is set to. This is the default option.
1 message is sent by email to the address in
the destination parameter. This is the only
message type where the fourth parameter,
additional_headers is used.
2 No longer an option.
3 message is appended to the file
destination. A newline is not automatically
added to the end of the message string.
4 message is sent directly to the SAPI logging
handler.
destination

The destination. Its meaning depends on the
message_type parameter as described above.

additional_headers

The extra headers. It’s used when the message_type
parameter is set to 1.
This message type uses the same internal function as
mail() does.

Return Values

Returns true on success or false on failure.
If message_type is zero, this function always returns true,
regardless of whether the error could be logged or not.

Changelog

Version Description
8.0.0 destination and
additional_headers are now nullable.

Examples

Example #1 error_log() examples


<?php
// Send notification through the server log if we can not
// connect to the database.
if (!Ora_Logon($username, $password)) {
error_log("Oracle database not available!", 0);
}
// Notify administrator by email if we run out of FOO
if (!($foo = allocate_new_foo())) {
error_log("Big trouble, we're all out of FOOs!", 1,
"operator@example.com");
}
// another way to call error_log():
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
?>

Notes

Warning

error_log() is not binary safe. message will be truncated by null character.

Tip

message should not contain null character. Note that message may be sent to file, mail, syslog, etc. Use appropriate conversion/escape function, base64_encode(), rawurlencode() or addslashes() before calling error_log().

kevindougans at gmail dot com

13 years ago


Advice to novices: This function works great along with "tail" which is a unix command to watch a log file live. There are versions of Tail for Windows too, like Tail for Win32 or Kiwi Log Viewer.

Using both error_log() and tail to view the php_error.log you can debug code without having to worry so much about printing debug messages to the screen and who they might be seen by.

Further Note: This works even better when you have two monitors setup. One for your browser and IDE and the other for viewing the log files update live as you go.


Sion

4 years ago


DO NOT try to output TOO LARGE texts in the error_log();

if you try to output massive amounts of texts it will either cut of the text at about 8ooo characters (for reasonable massive strings, < 32 K characters) or (for insanely massive strings, about 1.6 million characters) totally crash without even throwing an error or anything (I even put it in a try/catch without getting any result from the catch).

I had this problem when I tried to debug a response from a wp_remote_get(); all of my error_log() worked as they should, except for ONE of them... (-_-)
After about a day of debugging I finally found out why & that's why I type this.

Apparently the response contained a body with over 1.6 million chars (or bytes? (whatever strlen() returns)).

If you have a string of unknown length, use this:
$start_index = 0;
$end_index = 8000;
error_log( substr( $output_text , $start_index , $end_index ) );


frank at booksku dot com

16 years ago


Beware!  If multiple scripts share the same log file, but run as different users, whichever script logs an error first owns the file, and calls to error_log() run as a different user will fail *silently*!

Nothing more frustrating than trying to figure out why all your error_log calls aren't actually writing, than to find it was due to a *silent* permission denied error!


i dot buttinoni at intandtel dot com

15 years ago


Be carefull. Unexpected PHP dies when 2GByte of file log reached (on systems having upper file size limit).
A work aorund is rotate logs :)

php at kennel17 dot NOSPAM dot co dot uk

17 years ago


It appears that the system log = stderr if you are running PHP from the command line, and that often stderr = stdout.  This means that if you are using a custom error to both display the error and log it to syslog, then a command-line user will see the same error reported twice.

Anonymous

20 years ago


when using error_log to send email, not all elements of an extra_headers string are handled the same way.  "From: " and "Reply-To: " header values will replace the default header values. "Subject: " header values won't: they are *added* to the mail header but don't replace the default, leading to mail messages with two Subject fields.

<?php

error_log

("sometext", 1, "zigzag@my.domain",
 
"Subject: FoonFrom: Rizzlas@my.domainn");?>

---------------%<-----------------------
To: zigzag@my.domain
Envelope-to: zigzag@my.domain
Date: Fri, 28 Mar 2003 13:29:02 -0500
From: Rizzlas@my.domain
Subject: PHP error_log message
Subject: Foo
Delivery-date: Fri, 28 Mar 2003 13:29:03 -0500

sometext
---------------%<---------------------

quoth the docs: "This message type uses the same internal function as mail() does." 

mail() will also fail to set a Subject field based on extra_header data - instead it takes a seperate argument to specify a "Subject: " string.

php v.4.2.3, SunOS 5.8


russ at russtanner dot com

3 years ago


You can easily filter messages sent to error_log() using "tail" and "grep" on *nix systems. This makes monitoring debug messages easy to see during development.

Be sure to "tag" your error message with a unique string so you can filter it using "grep":

In your code:

error_log("DevSys1 - FirstName: $FirstName - LastName: $Lastname");

On your command line:

tail -f /var/log/httpd/error_log | grep DevSys1

In this example, we pipe apache log output to grep (STDIN) which filters it for you only showing messages that contain "DevSys1".

The "-f" option means "follow" which streams all new log entries to your terminal or to any piped command that follows, in this case "grep".


Matthew Swift

3 years ago


Relative paths are accepted as the destination of message_type 3, but beware that the root directory is determined by the context of the call to error_log(), which can change, so that one instance of error_log () in your code can lead to the creation of multiple log files in different locations.

In a WordPress context, the root directory will be the site's root in many cases, but it will be /wp-admin/ for AJAX calls, and a plugin's directory in other cases. If you want all your output to go to one file, use an absolute path.


paul dot chubb at abs dot gov dot au

14 years ago


When logging to apache on windows, both error_log and also trigger_error result in an apache status of error on the front of the message. This is bad if all you want to do is log information. However you can simply log to stderr however you will have to do all message assembly:

LogToApache($Message) {
        $stderr = fopen('php://stderr', 'w');
        fwrite($stderr,$Message);
        fclose($stderr);
}


SJL

15 years ago


"It appears that the system log = stderr if you are running PHP from the command line"

Actually, it seems that PHP logs to stderr if it can't write to the log file. Command line PHP falls back to stderr because the log file is (usually) only writable by the webserver.


stepheneliotdewey at GmailDotCom

15 years ago


Note that since typical email is unencrypted, sending data about your errors over email using this function could be considered a security risk. How much of a risk it is depends on how much and what type of information you are sending, but the mere act of sending an email when something happens (even if it cannot be read) could itself imply to a sophisticated hacker observing your site over time that they have managed to cause an error.

Of course, security through obscurity is the weakest kind of security, as most open source supporters will agree. This is just something that you should keep in mind.

And of course, whatever you do, make sure that such emails don't contain sensitive user data.


p dot lhonorey at nospam-laposte dot net

16 years ago


Hi !

Another trick to post "HTML" mail body. Just add "Content-Type: text/html; charset=ISO-8859-1" into extra_header string. Of course you can set charset according to your country or Env or content.

EG: Error_log("<html><h2>stuff</h2></html>",1,"eat@joe.com","subject  :lunchnContent-Type: text/html; charset=ISO-8859-1");

Enjoy !


eguvenc at gmail dot com

14 years ago


<?php

//Multiline error log class

// ersin güvenç 2008 eguvenc@gmail.com

//For break use "n" instead 'n'
Class log {

 
//

 
const USER_ERROR_DIR = '/home/site/error_log/Site_User_errors.log';

  const
GENERAL_ERROR_DIR = '/home/site/error_log/Site_General_errors.log';
/*

   User Errors...

  */

   
public function user($msg,$username)

    {

   
$date = date('d.m.Y h:i:s');

   
$log = $msg."   |  Date:  ".$date."  |  User:  ".$username."n";

   
error_log($log, 3, self::USER_ERROR_DIR);

    }

   
/*

   General Errors...

  */

   
public function general($msg)

    {

   
$date = date('d.m.Y h:i:s');

   
$log = $msg."   |  Date:  ".$date."n";

   
error_log($msg."   |  Tarih:  ".$date, 3, self::GENERAL_ERROR_DIR);

    }

}

$log = new log();

$log->user($msg,$username); //use for user errors

//$log->general($msg); //use for general errors

?>

franz at fholzinger dot com

18 years ago


In the case of missing your entries in the error_log file:
When you use error_log in a script that does not produce any output, which means that you cannot see anything during the execution of the script, and when you wonder why there are no error_log entries produced in your error_log file, the reasons can be:
- you did not configure error_log output in php.ini
- the script has a syntax error and did therefore not execute

daniel dot fukuda at gmail dot com

13 years ago


If you have a problem with log file permission *silently*
it's best to leave error_log directive unset so errors will be written in your Apache log file for current VirtualHost.

Anonymous

2 years ago


Depending on the error, you may also want to add an error 500 header, and a message for the user:

$message =  'Description of the error.';
error_log($message);
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
exit($message);


Robert Chapin

4 years ago


When error_log() unexpectedly uses stdout, you should check if the php.ini value for error_log is empty in your CLI environment.  Something as simple as this might restore expected behavior:

<?php ini_set('error_log', 'error_log'); ?>


kazezb at nospam dot carleton dot edu

17 years ago


It appears that error_log() only logs the first line of multi-line log messages. To log a multi-line message, either log each line individually or write the message to another file.

Anonymous

13 years ago


After scouring the internet for getting event logging to
work in syslog on Windows 2003, I found the following
from this post and was able to successfully get Windows
Event Viewer to log PHP errors/notices:

http://forums.iis.net/p/1159662/1912015.aspx#1913338

   1. Copy the PHP 5 binaries to "C:php".
   2. Right-click My Computer and select Properties to bring
up the Computer Properties dialog. Switch to the Advanced
tab and click Environment Variables. Find the system
environment variable PATH, edit it and add ";C:php"
(without the quotes) to the end.
   3. Make sure that the configuration file "php.ini" resides
in the directory "C:php" and contains the correct path
settings.
   4. DELETE any old "php.ini" files from "C:WINDOWS"
and other directories.
   5. Open REGEDIT, navigate to the key
"HKLMSOFTWAREPHP" and DELETE the string value
"IniFilePath" from there. It is outdated and no longer
necessary!
   6. Modify NTFS security permissions of the directory
"C:php" to give Read and Execute permissions to (1) the
IIS Guest Account and (2) the group IIS_WPG.
   7. Modify NTFS security permissions of the directories
"C:phpsession" and "C:phpupload" to give additional
Modify permissions to (1) the IIS Guest Account and (2)
the group IIS_WPG.
   8. Navigate to the registry key
"HKLMSYSTEMCurrentControlSetServicesEventlog
Application" and edit the value "CustomSD" there. Find
the substring "(D;;0xf0007;;;BG)" which Denies access to
the application event log for Builtin Guest accounts (like
the IIS Web User account) and replace this substring with
"(A;;0x3;;;BG)" which allows read and write access. Please
pay attention to leave the rest of the security string intact.
Damaging this value can have dangerous effects!
   9. Create or update the registry key
"HKLMSYSTEMCurrentControlSetServicesEventlogApplication
PHP-5.2.0" (adapt the last to your version part
if necessary) with the following values:

          * "EventMessageFile" (REG_EXPAND_SZ) = "C:phpphp5ts.dll"

          * "TypesSupported" (REG_DWORD) = 7


Если у вас сайт на PHP, даже самый простой, время от времени в его скриптах могут возникать ошибки. Это может привести к различным неприятным последствиям, от некорректной работы некоторых компонентов сайта (например, формы обратной связи), до недоступности сайта целиком. Как самостоятельно распознать тип ошибки PHP и понять, что с ней делать дальше?

Этот материал поможет вам, во-первых, самостоятельно оценить ситуацию (и, возможно, даже решить ее), а во-вторых, точно ускорит диагностику и решение проблемы при обращении в службу поддержки. Самые ценные советы по устранению наиболее частых ошибок PHP, связанных с лимитами оперативной памяти, вы найдете в конце статьи.

error 500

Как обнаружить ошибку PHP на сайте

1. Встроенными средствами браузера

Итак, если на сайте вместо привычной страницы ничего не отображается (вы видите “пустую страницу”), то, вероятнее всего, в одном из скриптов возникла ошибка. В этом можно убедиться, воспользовавшись встроенными «Инструментами разработчика» вашего браузера. В каждом браузере они могут называться немного по-разному, но суть от этого не меняется.

Например, в браузере Google Chrome это вкладка Dev Tools (или «Инструменты разработчика»). В Mozilla Firefox — это расширение Firebug  (его нужно установить отдельно в меню Adds On) или же вкладка Developer.

Внутри «Инструментов разработчика» нас интересует вкладка, которая называется Network (или Net, или каким-то похожим образом).

Если на странице сайта присутствует ошибка, в этой вкладке вы увидите код ответа 500 (“Internal Server Error”).Ошибка 505 server error

2. Если вывод сообщений об ошибках в браузер отключен

Случается, что вывод сообщений об ошибках в браузер отключён. Чтобы сообщение об ошибке отображалось в браузере, достаточно добавить в файл .htaccess в корневой директории сайта следующую строку:

php_value display_errors on

Файл .htaccess вы найдете по адресу: /home/login/domains/domain.ru/public_html/, где вместо login следует подставить логин вашего аккаунта, а вместо domain.ru — домен вашего сайта.

После сохранения файла .htaccess и обновления страницы вы сможете увидеть ошибку.

Если сайтом используется, например, CMS WordPress, то отображение ошибок можно также включить, заменив в файле wp-config.php:

 define(‘WP_DEBUG’, false);

на:

define(‘WP_DEBUG’, true);

3. С помощью журнала ошибок PHP

Иногда по различным причинам отображать ошибки в браузере нежелательно. В этом случае лучше всего сохранять их в какой-нибудь файл, например errors.log — журнал ошибок PHP. Для этого достаточно в файле .htaccess добавить следующую строку:

 php_value error_log /home/login/domains/domain.ru/log/errors.log

Здесь /home/login/domains/domain.ru/log/errors.log — это полный путь до файла, в который будут записываться ошибки PHP (если файла с таким именем нет, он будет создан автоматически при появлении ошибки).
Теперь, если мы снова зайдем на сайт с ошибкой (либо обновим страницу с ошибкой), то в errors.log будут записаны сообщения об ошибках.

Журнал ошибок PHP можно просмотреть, например, с помощью файлового менеджера в Панели управления, открыв файл errors.log:

03

Также можно открыть файл с ошибками и нажать кнопку “Включить автообновление”. Таким образом, новые записи в журнале можно просматривать в реальном времени.

 Расшифровка ошибок PHP

Как правило, в сообщении об ошибке достаточно подробно указано где именно и при выполнении какой части кода она возникла. Например:

Сообщение об ошибке PHP Здесь ошибка заключается в следующем:

Fatal error: Call to undefined function weblizar_get_options() in /home/login/domains/domain.ru/public_html/wp-content/themes/enigma/header.php on line 14

“Вызов неопределенной функции weblizar_get_options() в файле используемой на сайте темы enigma”.

Вероятнее всего, был поврежден один из файлов темы, поэтому можно восстановить только директорию темы ./wp-content/themes/enigma/ , а не всего сайта.

Что делать, в зависимости от типа ошибки PHP

Условно ошибки PHP можно разбить на 4 уровня:

  1. PARSE ERROR
  2. FATAL ERROR
  3. WARNING
  4. NOTICE
Parse Error

Возникают, если уже на этапе проверки кода интерпретатором PHP найдена ошибка. Чаще всего это синтаксические ошибка (например, пропущенная точка с запятой). Скорее всего, такая ошибка возникла в результате последних внесенных на сайт изменений.

Что делать?

1. Если вы НЕ специалист в PHP, восстановите сайт из последней резервной копии на тот момент, когда сайт работал без ошибок.

2. Если вы специалист и самостоятельно вносили правки в код сайта, вы наверняка сможете отследить синтаксическую ошибку и исправить ее. Но проще все же воспользоваться пунктом 1.

Fatal Error и Warning

Возникают, если при выполнении кода какой-то его участок не может быть выполнен (например, попытка открыть несуществующий файл). Разница между 2-ым и 3-им уровнем в том, что при получении “критической ошибки” (FATAL ERROR) выполнение скрипта завершится, а при получении “предупреждения” (WARNING) — нет.

Что делать?

Восстановите сайт из последней доступной резервной копии на тот момент, когда он работал без ошибок.

Notice

К этому уровню ошибок относятся различные “замечания”, суть которых обычно отображена в тексте ошибки.

Что делать?

Если замечание самостоятельно исправить не получается, обратитесь в службу поддержки или же восстановите сайт из последней доступной резервной копии на тот момент, когда он работал без ошибок.

Частые ошибки PHP и их решение

 Fatal Error: Allowed Memory

Означает, что для выполнения какой-либо части кода PHP не хватает выделенной оперативной памяти. При этом лимит памяти ограничен какими-то директивами «изнутри» сайта (то есть где-либо в скриптах сайта, либо директивой memory_limit в файле .htaccess). Чтобы исправить это, измените данный лимит в большую сторону, например, в файле .htaccess.

Для этого найдите в .htaccess такую директиву:

php_value memory_limit 128M

Вместо 128M укажите желаемый размер ограничения. Обратите внимание, что символ «M» (латинская M) указывается слитно со значением.

Помните, что есть максимальные значения памяти, отведенной на выполнение скриптов PHP, предусмотенные вашим тарифом хостинга (например, на тарифах виртуального хостинга это 512 Мб, премиум — 1024 Мб). Уточните эти значения у вашего провайдера, если они не указаны явно.

Fatal Error: Out of memory

То же самое, что и предыдущая ошибка, с той разницей, что достигнут лимит памяти, заданный “снаружи”. Обратите внимание на параметр “Памяти на процесс, Мб, не более“ в условиях пользования нашим сервисом.

Для решения вопроса в данном случае, скорее всего, потребуется либо оптимизация скриптов, чтобы они потребляли меньше памяти, либо разбиение процессов на части. Например, объемную загрузку или выгрузку данных, если она упирается в данный лимит, имеет смысл производить частями.

Также в этом случае мы советуем попробовать отключить акселераторы PHP, если они у вас подключены.

Unable to allocate memory for pool

Сайтам на аккаунте не хватает выделенной на тарифном плане памяти для акселераторов PHP.

Для решения проблемы вы можете отключить использование акселераторов в Панели управления хостингом, в разделе «Управление персональным веб-сервером».

Также, например, можно отключить акселератор APC для определенного сайта, добавив в файл .htaccess корневой директории следующую директиву:

php_value apc.cache_by_default off

Обычно имеет смысл оставлять APC для использования на самом посещаемом из ваших сайтов — это позволит использовать именно на нем преимущества акселератора, не заполняя его память данными с других, менее посещаемых сайтов.

В случаях, когда акселератор объективно необходим для корректной и комфортной работы сайтов и его отключение нежелательно, напишите в службу поддержки.

Мы постараемся предложить возможные варианты решения.

Желаем вам приятной работы!

Я арендовал виртуальный хостинг и использую Cpanel, Apache, PHP, который работает под управлением fastcgi. Где PHP хранит журнал ошибок?

Есть ли другой способ найти журнал ошибок в среде общего хостинга вместо того, чтобы просматривать всю структуру сайта в поисках файлов error_log?

У меня есть доступ к php.ini (я использую PHP версии 5.2.16).

Ответ 1

PHP сохраняет журналы ошибок в папке /var/log/apache2, если PHP является модулем apache2. Общие хосты часто хранят файлы журналов в подпапке корневого каталога /log. Но, если у вас есть доступ к php.ini файлу, вы можете сделать это:

error_log = /var/log/php-scripts.log

Если вы используете cPanel, главный файл журнала, который вы, вероятно, ищете, хранится (по умолчанию) в:

/usr/local/apache/logs/error_log

Если ничего не помогает, вы можете проверить расположение файла журнала, используя:

<?php phpinfo(); ?>

Ответ 2

В Linux:

php —info | grep error 

Терминал выведет местоположение журнала ошибок.

В Windows:

php —info | findstr /r /c:»error_log»

Командная строка выведет местоположение журнала ошибок.

Чтобы установить местоположение журнала:

Откройте php.ini и добавьте следующую строку:

error_log = /log/myCustomLog.log

Ответ 3

В среде LAMP ошибки php по умолчанию направляются в этот файл:

/var/log/httpd/error_log

Все журналы доступа находятся здесь:

/var/log/httpd/access_log

Ответ 4

Как найти журнал ошибок PHP в Linux:

eric@dev /var $ sudo updatedb

[sudo] password for eric:

eric@dev /var $ sudo locate error_log

/var/log/httpd/error_log

Альтернативный способ:

eric@dev /home/eric $ sudo find / -name «error_log» 2>/dev/null

/var/log/httpd/error_log

Ответ 5

Необходимо использовать абсолютный путь при установке переменной error_log в вашем файле php.ini, иначе журналы ошибок будут сохраняться в соответствии с вашим относительным путем.

error_log = /var/log/php.errors

Другое решение — написать простой скрипт, который будет перечислять все файлы журналов ошибок из дерева каталогов.

Ответ 6

По умолчанию php нигде не регистрирует журнал ошибок error_log. В php.ini параметр закомментирован во всех установках, которые я видел.

Возможные решения:

  1. ищите файлы php.ini. в locate php.ini;

  2. ищите в этих файлах error_reporting значение;

  3. проверяйте error_log значение, чтобы убедиться, что оно указывает на реальное место и не закомментировано.

Ответ 7

Лучше всего заглянуть в файл httpd.conf и посмотреть, что установлено по умолчанию. Он также может быть отменен вашим конкретным виртуальным хостом. Я начинаю с просмотра /etc/httpd/conf/httpd.conf или /etc/apache2/httpd.conf и ищу error_log. Он может быть указан как /var/log/httpd/error_log или /var/log/apache2/error_log как logs/error_log.

В этом случае это будет относительный путь, который означает, что он будет указывать на /etc/httpd/logs/error_log. Если вы все еще не можете его найти, посмотрите на конечную часть файла httpd.conf и посмотрите, где включены ваши виртуальные хосты. Он может находиться в /etc/httpd/conf.d/<- как «другой» или «дополнительный». Ваш виртуальный хост может переопределить его с помощью ErrorLog «/path/to/error_log».

Вчера всё работало, а сегодня не работает / Код не работает как задумано

или

Debugging (Отладка)


В чем заключается процесс отладки? Что это такое?

Процесс отладки состоит в том, что мы останавливаем выполнения скрипта в любом месте, смотрим, что находится в переменных, в функциях, анализируем и переходим в другие места; ищем те места, где поведение отклоняется от правильного.


Важное замечание:

Есть много IDE и редакторов кода, которые позволяют производить отладку. Процесс настройки в них у всех различается. Поэтому стОит обратиться к документации по настройке отладки для непосредственно той среды разработки и той версии, в которой работаете именно ВЫ.

На текущий момент будет рассмотрен пример с PHPStorm 2017.


Подготовка

Для начала необходимо, чтобы в PHP имелась библиотека для отладки под названием xdebug. Если её еще нет, то надо установить.

ВАЖНО! Для очень новых версий PHP (например 8), требуется и новый xdebug, а он, в свою очередь, работает на порту 9003. Не пропустите указание правильного порта в IDE!! (Примерно в разделе PHP -> Debug -> Debug Port . Где точно — зависит от конкретной IDE)

Для WINDOWS:

скачать dll, например на xdebug.org.

Обычно все библиотеки лежат в папке ext внутри папки PHP. Туда и надо поместить dll.

Далее в php.ini прописываем настройки:

[Xdebug]
zend_extension="C:/server/php/ext/php_xdebug.dll" // <!-- тут свой путь до dll!!! Это для среды Windows. 
; Для Linux путь должен быть что-то типа zend_extension=/usr/lib/php/20151012/xdebug.so 
xdebug.default_enable = 1
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000
xdebug.auto_trace = 0

Перезагружаем сервер, на всякий случай.

Для UBUNTU:

  • sudo apt update ИЛИ sudo apt-get update

  • sudo apt install php-xdebug или если нужнен отладчик для конкретной версии PHP, то sudo apt install php7.0-xdebug где 7.0 указывается версия PHP

  • sudo nano /etc/php/7.0/mods-available/xdebug.ini

    вписываем строки:

     zend_extension=/usr/lib/php/20151012/xdebug.so
     xdebug.remote_autostart = 1
     xdebug.remote_enable = 1
     xdebug.remote_handler = dbgp
     xdebug.remote_host = 127.0.0.1
     xdebug.remote_log = /tmp/xdebug_remote.log
     xdebug.remote_mode = req
    

    Примечание: каталог 20151012, скорее всего, будет другим. cd в /usr/lib/php и проверьте, в каком каталоге в этом формате находится файл xdebug.so, и используйте этот путь. 7.0 — тоже отличается, в зависимости от того, какая версия у вас используется

  • Перезагружаем сервер, на всякий случай.


Теперь если в файле .php написать phpinfo(); то можно будет увидеть в самом низу такую картину:

введите сюда описание изображения

Открываем PHPStorm

  • нажимаем create project from existing files
  • выбираем Web server is installed locally, source files are located under its document root
  • выбираем папку с файлами, и нажав вверху кнопку «Project Root» помечаем папку как корень проекта
  • нажимаем «Next»
  • нажимаем Add new local server

введите сюда описание изображения

  • вводим имя сервера любое и Web Server root URL. В рассматриваемом примере это http://localhost/testy2

введите сюда описание изображения

  • нажимаем «Next» и затем «Finish»

Запуск

Для начала в левой части панели с кодом на любой строке можно кликнуть ЛКМ, тем самым поставив точку останова (breakpoint — брейкпойнт). Это то место, где отладчик автоматически остановит выполнение PHP, как только до него дойдёт. Количество breakpoint’ов не ограничено. Можно ставить везде и много.

введите сюда описание изображения

Если кликнуть ПКМ и во всплывающем меню выбрать Debug (или в верхнем меню — RunDebug), то при первом запуске PHPStorm попросит настроить интерпретатор. Т.е. надо выбрать версию PHP из папки, где он лежит, чтобы шторм знал, какую версию он будет отлаживать.

введите сюда описание изображения

Теперь можно нажать Debug!!!

В данном случае, т.к. функция вызывается сразу на той же странице, то при нажатии кнопки Debug — отладчик моментально вызовет функцию, выполнение «заморозится» на первом же брейкпойнте. В ином случае, для активации требуется исполнить действие, при котором произойдет исполнение нужного участка кода (клик на кнопку, передача POST-запроса с формы с данными и другие действия).

введите сюда описание изображения

Цифрами обозначены:

  1. Стэк вызовов, все вложенные вызовы, которые привели к текущему месту кода.
  2. Переменные. На текущий момент строки ниже номера 3 ещё не выполнились, поэтому определена лишь $data
  3. Показывает текущие значения любых переменных и выражений. В любой момент здесь можно нажать на +, вписать имя любой переменной и посмотреть её значение в реальном времени. Например: $data или $nums[0], а можно и $nums[i] и item['test']['data'][$name[5]][$info[$key[1]]] и т.д. На текущий момент строки ниже номера 3 ещё не выполнились, поэтому $sum и $output обозначены красным цветом с надписью «cannot evaluate expression».

Процесс

Для самого процесса используются элементы управления (см. изображение выше, выделено зеленым прямоугольником) и немного из дополнительно (см. изображение выше, выделено оранжевым прямоугольником).

введите сюда описание изображения

Show Execution Point (Alt+F10) — переносит в файл и текущую линию отлаживаемого скрипта. Например, если файлов много, решили посмотреть что в других вкладках, а потом забыли где у вас отладка :)

Step Over (F8) — делает один шаг, не заходя внутрь функции. Т.е. если на текущей линии есть какая-то функция, а не просто переменная со значением, то при клике данной кнопки, отладчик не будет заходить внутрь неё.

Step Into (F7) — делает шаг. Но в отличие от предыдущей, если есть вложенный вызов (например функция), то заходит внутрь неё.

Step Out (Shift+F8) — выполняет команды до завершения текущей функции. Удобно, если случайно вошли во вложенный вызов и нужно быстро из него выйти, не завершая при этом отладку.

Rerun (Ctrl+F5) — перезапускает отладку.

Resume Program(F9) — продолжает выполнение скрипта с текущего момента. Если больше нет других точек останова, то отладка заканчивается и скрипт продолжает работу. В ином случае работа прерывается на следующей точке останова.

Stop (Ctrl+F2) — завершает отладку.

View Breakpoints (Ctrl+Shift+F8) — просмотр всех установленных брейкпойнтов.

Mute Breakpoints — отключает брейкпойнты.

Итак, в текущем коде видно значение входного параметра:

  • $data = "23 24 11 18" — строка с данными через пробел
  • $nums = (4) ["23", "24", "11", "18"] — массив, который получился из входной переменной.

введите сюда описание изображения

Если нажмем F8 2 раза, то окажемся на строке 7; во вкладках Watches и Variables и в самой странице с кодом увидим, что переменная $sum была инициализирована и её значение равно 0.

Если теперь нажмем F8, то попадем внутрь цикла foreach и, нажимая теперь F8, пока не окончится цикл, можно будет наблюдать на каждой итерации, как значения $num и $sum постоянно изменяются. Тем самым мы можем проследить шаг за шагом весь процесс изменения любых переменных и значений на любом этапе, который интересует.

Дальнейшие нажатия F8 переместят линию кода на строки 11, 12 и, наконец, 15.


Дополнительно

Если нажать на View Breakpoints в левой панели, то можно не только посмотреть все брейкпойнты, но в появившемся окне можно еще более тонко настроить условие, при котором на данной отметке надо остановиться.
В функции выше, например, нужно остановиться только когда $sum превысит значение 20.

введите сюда описание изображения

Это удобно, если останов нужен только при определённом значении, а не всегда (особенно в случае с циклами).

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

debug_backtraceGenerates a backtrace

Description

debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array

Parameters

options

This parameter is a bitmask for the following options:

debug_backtrace() options

DEBUG_BACKTRACE_PROVIDE_OBJECT Whether or not to populate the «object» index.
DEBUG_BACKTRACE_IGNORE_ARGS Whether or not to omit the «args» index, and thus all the function/method arguments,
to save memory.
limit

This parameter can be used to limit the number of stack frames returned.
By default (limit=0) it returns all stack frames.

Return Values

Returns an array of associative arrays. The possible returned elements
are as follows:

Possible returned elements from debug_backtrace()

Name Type Description
function string The current function name. See also
__FUNCTION__.
line int The current line number. See also
__LINE__.
file string The current file name. See also
__FILE__.
class string The current class name. See also
__CLASS__
object object The current object.
type string The current call type. If a method call, «->» is returned. If a static
method call, «::» is returned. If a function call, nothing is returned.
args array If inside a function, this lists the functions arguments. If
inside an included file, this lists the included file name(s).

Examples

Example #1 debug_backtrace() example


<?php
// filename: /tmp/a.phpfunction a_test($str)
{
echo
"nHi: $str";
var_dump(debug_backtrace());
}
a_test('friend');
?>

<?php
// filename: /tmp/b.php
include_once '/tmp/a.php';
?>

Results similar to the following when executing
/tmp/b.php:

Hi: friend
array(2) {
[0]=>
array(4) {
    ["file"] => string(10) "/tmp/a.php"
    ["line"] => int(10)
    ["function"] => string(6) "a_test"
    ["args"]=>
    array(1) {
      [0] => &string(6) "friend"
    }
}
[1]=>
array(4) {
    ["file"] => string(10) "/tmp/b.php"
    ["line"] => int(2)
    ["args"] =>
    array(1) {
      [0] => string(10) "/tmp/a.php"
    }
    ["function"] => string(12) "include_once"
  }
}

See Also

  • trigger_error() — Generates a user-level error/warning/notice message
  • debug_print_backtrace() — Prints a backtrace

jurchiks101 at gmail dot com

9 years ago


Here's a function I just wrote for getting a nice and comprehensible call trace. It is probably more resource-intensive than some other alternatives but it is short, understandable, and gives nice output (Exception->getTraceAsString()).

<?php
function generateCallTrace()
{
   
$e = new Exception();
   
$trace = explode("n", $e->getTraceAsString());
   
// reverse array to make steps line up chronologically
   
$trace = array_reverse($trace);
   
array_shift($trace); // remove {main}
   
array_pop($trace); // remove call to this method
   
$length = count($trace);
   
$result = array();

        for (

$i = 0; $i < $length; $i++)
    {
       
$result[] = ($i + 1)  . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
   
}

        return

"t" . implode("nt", $result);
}
?>

Example output:
    1) /var/www/test/test.php(15): SomeClass->__construct()
    2) /var/www/test/SomeClass.class.php(36): SomeClass->callSomething()

Anonymous

10 years ago


Simple function to get a string in form "filename: [class->][function(): ]"

<?php
function get_caller_info() {
   
$c = '';
   
$file = '';
   
$func = '';
   
$class = '';
   
$trace = debug_backtrace();
    if (isset(
$trace[2])) {
       
$file = $trace[1]['file'];
       
$func = $trace[2]['function'];
        if ((
substr($func, 0, 7) == 'include') || (substr($func, 0, 7) == 'require')) {
           
$func = '';
        }
    } else if (isset(
$trace[1])) {
       
$file = $trace[1]['file'];
       
$func = '';
    }
    if (isset(
$trace[3]['class'])) {
       
$class = $trace[3]['class'];
       
$func = $trace[3]['function'];
       
$file = $trace[2]['file'];
    } else if (isset(
$trace[2]['class'])) {
       
$class = $trace[2]['class'];
       
$func = $trace[2]['function'];
       
$file = $trace[1]['file'];
    }
    if (
$file != '') $file = basename($file);
   
$c = $file . ": ";
   
$c .= ($class != '') ? ":" . $class . "->" : "";
   
$c .= ($func != '') ? $func . "(): " : "";
    return(
$c);
}
?>

Usage like:

<?php
function debug($str) {
    echo
get_caller_info() . $str . "<br>n";
}
?>

get_caller_info() will return info about the function /class->method that called debug().

jsnell at e-normous dot com

15 years ago


If you are using the backtrace function in an error handler, avoid using var_export() on the args, as you will cause fatal errors in some situations, preventing you from seeing your stack trace.  Some structures will cause PHP to generate the fatal error "Nesting level too deep - recursive dependency?" This is a design feature of php, not a bug (see http://bugs.php.net/bug.php?id=30471)

robert at medianis dot net

6 years ago


Just a short note on debug_backtrace options for PHP 5.3.6 or newer:

debug_backtrace() - show all options
debug_backtrace(0) - exlude ["object"]
debug_backtrace(1) - same as debug_backtrace()
debug_backtrace(2) - exlude ["object"] AND ["args"]

use this example and try calling debug_backtrace with different options

<?php
function F1()
{
    echo
"<br />";
    echo
"in F1 now";
    echo
"<pre>".print_r(debug_backtrace(2),true)."</pre>";
}

class

DebugOptionsTest
{
    function
F2()
    {
        echo
"<br />";
        echo
"in F2 now";
       
F1();
    }

}

echo

"<hr />calling F1";
F1();$c=new DebugOptionsTest();
echo
"<hr /><hr /><hr />calling F2";
$c->F2("testValue");?>

jake at qzdesign dot co dot uk

3 years ago


The `args` element contains only the arguments actually passed to the function or method.  It does not include default parameters if they were not explicitly specified.  (A least, this is the case with PHP 7.1.9.)  This is consistent with the behaviour of `func_get_args()`.

d at rren dot me

10 years ago


Howdy guys, just a note really - The ['args'] data within the resulting array is supplied by reference. I found myself editing the reference unknowingly which in turn shows its ugly head further down the line if you call multiple backtrace.

<?php

$trace

= array_reverse(debug_backtrace());
// LOOP BACKTRACE$la = 0;$lb = count($trace);

while (

$la<$lb){
// DATA FROM BACKTRACE$trace[$la]['file'];$trace[$la]['line'];$trace[$la]['args'];$trace[$la]['function'];// DATA FROM BACKTRACE

    // LOOP ARGUMENTS ARRAY

$ba = 0;$bb = count($trace[$la]['args']);

    while (

$ba<$bb){
$trace[$la]['args'][$ba] = "EDITING A REFERENCE/POINTER";
$ba++;

    }

    unset(

$bb);

    unset(

$ba);// LOOP ARGUMENTS ARRAY
$la++;

}

unset(

$lb);

unset(

$la);// LOOP BACKTRACE?>

michael dot schramm at gmail dot com

13 years ago


Be carefull if you are using objects as arguments for function calls!

<?php
error_reporting
(E_ALL);

function

myPrint($trace){
    foreach(
$trace as $i=>$call){
       
/**
         * THIS IS NEEDED! If all your objects have a __toString function it's not needed!
         *
         * Catchable fatal error: Object of class B could not be converted to string
         * Catchable fatal error: Object of class A could not be converted to string
         * Catchable fatal error: Object of class B could not be converted to string
         */
       
if (is_object($call['object'])) { $call['object'] = 'CONVERTED OBJECT OF CLASS '.get_class($call['object']); }
        if (
is_array($call['args'])) {
            foreach (
$call['args'] AS &$arg) {
                if (
is_object($arg)) { $arg = 'CONVERTED OBJECT OF CLASS '.get_class($arg); }
            }
        }
$trace_text[$i] = "#".$i." ".$call['file'].'('.$call['line'].') ';
       
$trace_text[$i].= (!empty($call['object'])?$call['object'].$call['type']:'');
       
$trace_text[$i].= $call['function'].'('.implode(', ',$call['args']).')';
    }
var_dump($trace_text);
}

class

A{
    public function
test($obj){
       
$obj->test();
    }
}

class

B{
    public function
test(){
        echo
myPrint(debug_backtrace());
    }
}
$A = new A();
$B = new B();$A->test($B);?>

Emmett Brosnan

6 years ago


Quick and dirty formatted output from debug_backtrace.

$file_paths = debug_backtrace();

  foreach($file_paths AS $file_path) {
  foreach($file_path AS $key => $var) {
    if($key == 'args') {
      foreach($var AS $key_arg => $var_arg) {
        echo $key_arg . ': ' . $var_arg . '<br>';
      }
    } else {
      echo $key . ': ' . $var . '<br>';
    } 
  }
}

root at jackyyf dot com

10 years ago


When use register_shutdown_function, and the function called when shutting down, there are no line number nor filename information about this function, only function, class(if possible), type(if possible) and args are provided.

jcmargentina at gmail dot com

3 years ago


I want to point out that debug_backtrace() in new versions of php can detect recursion // circular references .. avoiding memory consumption.

Example:

<?phpclass ParentClass {
        public function
__construct()
        {
               
$this->_child = new ChildClass($this);
               
var_dump(debug_backtrace());
        }
}

class

ChildClass {
        public function
__construct(ParentClass $p)
        {
               
$this->_parent = $p;
        }
}
$test = new ParentClass();
?>

Output:

array(1) {
  [0]=>
  array(7) {
    ["file"]=>
    string(23) "/home/jcm/testdebug.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(11) "__construct"
    ["class"]=>
    string(11) "ParentClass"
    ["object"]=>
    object(ParentClass)#1 (1) {
      ["_child"]=>
      object(ChildClass)#2 (1) {
        ["_parent"]=>
        *RECURSION*
      }
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}

Attention in the *RECURSION* hint provided

kenorb at gmail dot com

12 years ago


One line of code to print simplest and shortest human readable backtrace:)
<?php
array_walk
(debug_backtrace(),create_function('$a,$b','print "{$a['function']}()(".basename($a['file']).":{$a['line']}); ";'));
?>

jonas at faceways dot se

9 years ago


When using debug_backtrace() to check if you're being accessed from another caller, please remember to ask debug_backtrace to only go as far as needed in depth and skip taking the entire debug object as return parameter:

<?php
   
if (count(debug_backtrace(FALSE, 1)) == 0)
    {
       
// Do something
   
}
?>

anoam at yandex dot ru

8 years ago


It works a little bit different with resources in different PHP versions.

For example:
function foo($bar)
{
  return debug_backtrace();
}

$resource = fopen(__FILE__, 'r');
$backtrace = foo($resource);
echo "when resource is opened: " . gettype($backtrace[0]['args'][0]) . "n";
fclose($resource);
echo "when resource is closed: " . gettype($backtrace[0]['args'][0]) . "n";

With 5.3.10 I got:
when resource is opened: resource
when resource is closed: resource

With 5.5.9:
when resource is opened: resource
when resource is closed: unknown type

Be carefull.

Bill Getas

12 years ago


Here's my little updated contribution - it prints colorful output in the way I prefer.  Define a helper function isRootIp() that contains an array including your IP; then calls to bt() simply return, so you can sprinkle backtraces in live sites w/o anyone knowing.

<?phpfunction bt()

{

    if( !

isRootIp() )

    {

        return

false;

    }

array_walk( debug_backtrace(), create_function( '$a,$b', 'print "<br /><b>". basename( $a['file'] ). "</b> &nbsp; <font color="red">{$a['line']}</font> &nbsp; <font color="green">{$a['function']} ()</font> &nbsp; -- ". dirname( $a['file'] ). "/";' ) );

}

?>

jlammertink at gmail dot com

12 years ago


I use this simple but effective function so i can see which method in the child class called the current method (in the parent class).

<?phpfunction get_caller_method()

{

$traces = debug_backtrace();

    if (isset(

$traces[2]))

    {

        return

$traces[2]['function'];

    }

    return

null;

}

?>

kroczu AT interia DOT pl

16 years ago


<?
// useful and comfortable debug function
// it's show memory usage and time flow between calls, so we can quickly find a block of code that need optimisation...
// example result:
/*
debug example.php> initialize
debug example.php> code-lines: 39-41 time: 2.0002 mem: 19 KB
debug example.php> code-lines: 41-44 time: 0.0000 mem: 19 KB
debug example.php> code-lines: 44-51 time: 0.6343 mem: 9117 KB
debug example.php> code-lines: 51-53 time: 0.1003 mem: 9117 KB
debug example.php> code-lines: 53-55 time: 0.0595 mem: 49 KB
*/

function debug()
{
   static $start_time = NULL;
   static $start_code_line = 0;

   $call_info = array_shift( debug_backtrace() );
   $code_line = $call_info['line'];
   $file = array_pop( explode('/', $call_info['file']));

   if( $start_time === NULL )
   {
       print "debug ".$file."> initializen";
       $start_time = time() + microtime();
       $start_code_line = $code_line;
       return 0;
   }

   printf("debug %s> code-lines: %d-%d time: %.4f mem: %d KBn", $file, $start_code_line, $code_line, (time() + microtime() - $start_time), ceil( memory_get_usage()/1024));
   $start_time = time() + microtime();
   $start_code_line = $code_line;
}

////////////////////////////////////////////////
// example:

debug();
sleep(2);
debug();
// soft-code...
$a = 3 + 5;
debug();

// hard-code
for( $i=0; $i<100000; $i++)
{
    $dummy['alamakota'.$i] = 'alamakota'.$i;
}
debug();
usleep(100000);
debug();
unset($dummy);
debug();

?>

nyoung55 at that_google_mail.com

10 years ago


Here is a function to cleanly output the debug_backtrace to the error_log

<?php/*

* Send the output from a backtrace to the error_log

* @param string $message Optional message that will be sent the the error_log before the backtrace

*/

function log_trace($message = '') {$trace = debug_backtrace();

    if (

$message) {error_log($message);

    }

$caller = array_shift($trace);$function_name = $caller['function'];error_log(sprintf('%s: Called from %s:%s', $function_name, $caller['file'], $caller['line']));

    foreach (

$trace as $entry_id => $entry) {$entry['file'] = $entry['file'] ? : '-';$entry['line'] = $entry['line'] ? : '-';

        if (empty(

$entry['class'])) {error_log(sprintf('%s %3s. %s() %s:%s', $function_name, $entry_id + 1, $entry['function'], $entry['file'], $entry['line']));

        } else {

error_log(sprintf('%s %3s. %s->%s() %s:%s', $function_name, $entry_id + 1, $entry['class'], $entry['function'], $entry['file'], $entry['line']));

        }

    }

}

?>

Anonymous

9 years ago


A usual entry looks like this:
<?php
array(6) {
 
'file' =>
 
string(87) "DbSelector.php"
  'line'
=>
 
int(171)
 
'function' =>
 
string(5) "error"
  'class'
=>
 
string(42) "LoggingService"
  'type'
=>
 
string(2) "::"
  'args'
=>
  array(
1) {
    [
0] =>
   
string(27) "Connecting to DB: unittests"
 
}
}
?>

Be warned though that 'file' and 'class' do not reference the same thing!
'file' means which file calls the next step.
'class' is the next step being called.

So 'file' is the caller, 'class' is the callee.

Gemorroj

10 years ago


Another variation formatting backtrace.

Parameter $ignore to ignore the extra calls.

<?php/**

* Getting backtrace

*

* @param int $ignore ignore calls

*

* @return string

*/

protected function getBacktrace($ignore = 2)

{

$trace = '';

    foreach (

debug_backtrace() as $k => $v) {

        if (

$k < $ignore) {

            continue;

        }

array_walk($v['args'], function (&$item, $key) {$item = var_export($item, true);

        });

$trace .= '#' . ($k - $ignore) . ' ' . $v['file'] . '(' . $v['line'] . '): ' . (isset($v['class']) ? $v['class'] . '->' : '') . $v['function'] . '(' . implode(', ', $v['args']) . ')' . "n";

    }

    return

$trace;

}

?>

kexianbin at diyism dot com

10 years ago


need no Xdebug or dbg.so on server, return more detailed message:

diyism_trace.php:
<?php
define
(TRACES_MODE, 'TEXTAREA');//'TEXTAREA' or 'FIREPHP'
$GLOBALS['traces.pre']=array();
function
my_array_diff($arr1, $arr2)
         {foreach (
$arr1 as $k=>$v)
                  {if (
in_array($v, $arr2, true))
                      {unset(
$arr1[$k]);
                      }
                  }
          return
$arr1;
         }
function
my_var_export($var, $is_str=false)
         {
$rtn=preg_replace(array('/Arrays+(/', '/[(d+)] => (.*)n/', '/[([^d].*)] => (.*)n/'), array('array (', '1 => '2''."n", ''1' => '2''."n"), substr(print_r($var, true), 0, -1));
         
$rtn=strtr($rtn, array("=> 'array ('"=>'=> array ('));
         
$rtn=strtr($rtn, array(")nn"=>")n"));
         
$rtn=strtr($rtn, array("'n"=>"',n", ")n"=>"),n"));
         
$rtn=preg_replace(array('/n +/e'), array('strtr('', array('    '=>'  '))'), $rtn);
         
$rtn=strtr($rtn, array(" Object',"=>" Object'<-"));
          if (
$is_str)
             {return
$rtn;
             }
          else
              {echo
$rtn;
              }
         }
function
tick_handler()
         {
$tmp=debug_backtrace();
         
$trace=my_array_diff($tmp, $GLOBALS['traces.pre']);
         
//echo '<pre>';var_export($trace);echo '</pre>';echo '<br/>'; //for debug diyism_trace.php
         
$trace=array_values($trace);
         
$GLOBALS['traces.pre']=$tmp;
          if (
count($trace)>0 && $trace[0]['file'].'/'.@$tmp[1]['function']!==@$GLOBALS['traces'][count($GLOBALS['traces'])-1]['key']) //filter empty array and rearrange array_values(), because some lines will trigger two tick events per line, for example: 1.last line is "some code;questmark>" 2.error_reporting(...
            
{for ($i=count($trace)-1; $i>=0; --$i)
                  {
$GLOBALS['traces'][]=$tmp_fb=array_merge(array('key'=>$trace[$i]['file'].'/'.@$tmp[$i+1]['function']), $trace[$i], array('function'=>strtr($trace[$i]['function'], array('tick_handler'=>'CONTINUE')), 'in_function'=>@$tmp[$i+1]['function']));
                  
TRACES_MODE==='FIREPHP'?fb(trace_output($tmp_fb), 'diyism_trace:'.++$GLOBALS['diyism_trace_no']):'';
                  }
             }
         }
function
trace_output($trace)
         {
$trace['in_function']=strtr(@$trace['in_function'], array('require'=>'', 'require_once'=>'', 'include'=>'', 'include_once'=>''));
         
$trace['args']=$trace['args']?strtr(preg_replace(array('/n +/'), array(''), preg_replace(array('/n  d+ => /'), array(''), substr(my_var_export($trace['args'], true), 7, -3))), array("r"=>'r', "n"=>'n')):'';
          return
$trace['file'].($trace['in_function']?'/'.$trace['in_function'].'()':'').'/'.$trace['line'].': '.$trace['function'].'('.$trace['args'].')';
         }
function
traces_output()
         {echo
'<textarea style="width:100%;height:300px;">';
         
$GLOBALS['traces']=array_slice($GLOBALS['traces'], 2);//remove registering tick line and requiring 'diyism_trace.php' line
         
foreach ($GLOBALS['traces'] as $k=>$trace)
                  {echo
htmlentities($k.':'.trace_output($trace)."n");
                  }
          echo
'</textarea>';
         }
register_tick_function('tick_handler');
TRACES_MODE==='TEXTAREA'?register_shutdown_function('traces_output'):'';
?>

test.php:
<?php
declare(ticks=1);
require
'diyism_trace.php';a('a', array('hello'));
1+2;
b();
function
a()
         {
$d=1;
         
b();
         
$d=2;
         }
function
b()
         {
1+1;
         }
?>

php noob

12 years ago


Surprisingly, no one has described one of the best uses of this: dumping a variable and showing the location. When debugging, especially a big and unfamiliar system, it's a pain remembering where I added those var dumps. Also, this way there is a separator between multiple dump calls.

<?phpfunction dump( $var ) {
   
$result = var_export( $var, true );
   
$loc = whereCalled();
    return
"n<pre>Dump: $locn$result</pre>";
}

function

whereCalled( $level = 1 ) {
   
$trace = debug_backtrace();
   
$file   = $trace[$level]['file'];
   
$line   = $trace[$level]['line'];
   
$object = $trace[$level]['object'];
    if (
is_object($object)) { $object = get_class($object); }

    return

"Where called: line $line of $object n(in $file)";
}
?>

In addition, calling 'whereCalled()' from any function will quickly identify locations that are doing something unexpected (e.g., updating a property at the wrong time). I'm new to PHP, but have used the equivalent in Perl for years.

php at kennel17 dot co dot uk

15 years ago


Further to my previous note, the 'object' element of the array can be used to get the parent object.  So changing the get_class_static() function to the following will make the code behave as expected:

<?php
   
function get_class_static() {
       
$bt = debug_backtrace();

            if (isset(

$bt[1]['object']))
            return
get_class($bt[1]['object']);
        else
            return
$bt[1]['class'];
    }
?>

HOWEVER, it still fails when being called statically.  Changing the last two lines of my previous example to

<?php
  foo
::printClassName();
 
bar::printClassName();
?>

...still gives the same problematic result in PHP5, but in this case the 'object' property is not set, so that technique is unavailable.

henzeberkheij at gmail dot com

12 years ago


I find it useful to know if a function is being called. in Java for instance you usually print a line with the functionname and arguments in the beginning of the function. I wanted to achieve the same thing in php thus i wrote the following class:

<?phpclass Debug{

    private static

$calls;

    public static function

log($message = null)

    {

        if(!

is_array(self::$calls))self::$calls = array();
$call = debug_backtrace(false);$call = (isset($call[1]))?$call[1]:$call[0];
$call['message'] = $message;array_push(self::$calls, $call);

    }

}

?>

include this class before anything else

usage: Debug::log($message); at the beginning of your function.

write yourself a nice printout of the data;

john dot risken at gmail dot com

12 years ago


Everybody seems to have their favorite use.  I substitute this function for die().  It gives a message

to the user and emails me a PrettyPrint of what went wrong.  $info is set by me,

and it does a special check in the database object.

<?php// var_format
function var_format($v) // pretty-print var_export{

    return (

str_replace(array("n"," ","array"),

array(

"<br>","&nbsp;","&nbsp;<i>array</i>"),var_export($v,true))."<br>");

}

function

myDie($info)

{

$mysqlerr=strpos($info,"ERROR=You have an error in your SQL syntax");

    if(

$mysqlerr>0)$info=substr($info,0,$mysqlerr)." mySql format error";$out="<br>MSG='$info'<br>".var_format($_REQUEST)."<br>";$bt=debug_backtrace();$sp=0;$trace="";

    foreach(

$bt as $k=>$v)

    {

extract($v);$file=substr($file,1+strrpos($file,"/"));

        if(

$file=="db.php")continue; // the db object$trace.=str_repeat("&nbsp;",++$sp); //spaces(++$sp);$trace.="file=$file, line=$line, function=$function<br>";       

    }

$out.="<br>".backTrace();

    if(

substr($info,0,4)=="XXX ") // special errrors when db is inaccessible{$out=str_replace("<br>","n",$out);$out=str_replace("&nbsp;"," ",$out);mail("me@example.com","Database Execution Error for user ".$REMOTE_ADDR,"$out");

        exit(

"Database Access Error. Please try again later.");

    }

mail("me@example.com",'Error Monitor','Execution Error',$out);

    exit(

"DANG! An execution error in the program has been sent to the webmaster.

If you don't get an email from him soon, please call him."

);

}

?>

This produces an output like this

file=badmode.php, line=5, function=backTrace

  file=login.php, line=209, function=require

   file=midScreen.php, line=264, function=require

    file=masterindex.php, line=161, function=require

     file=production2.php, line=121, function=require

      file=index.php, line=16, function=require

http://synergy8.com

17 years ago


It should be noted that if an internal php function such as call_user_func in the backtrace, the 'file' and 'line' entries will not be set.

Most debug tracers will use these entries.  You should place a check to see if the key exists in the array before using this function.  Otherwise notices will be generated.

<?php

$arrTrace

= debug_backtrace();

foreach (

$arrTrace as $arr)
{
    if (!isset (
$arr['file']))
    {
       
$arr['file'] = '[PHP Kernel]';
    }

    if (!isset (

$arr['line']))
    {
       
$arr['line'] = '';
    }
// Do something
}?>

frank at frank dot com

14 years ago


Here is my simple example:
Code printing variable of class which instatiates the printing class.

Well, I am sure you understand when looking at the code:
Print result is: jippii

<?php
class A {

        function

something() {
               
$s = debug_backtrace();$callingObject = $s[1]['object'];
               
$test = $callingObject->jip;
                print
$test;
        }

}

class

B {
      var
$jip;

              function

execute() {
               
$a = new A();
               
$this->jip = "jippii"
               
$a->something();
        }

}

$control = new B();
$control->execute();
?>

samthor

15 years ago


Here's a way to get the arguments for an upstream function in your stack (works with class methods, static methods and non-class methods):
<?php
/**
* getArgs - find arguments of upstream method
* can be called with, e.g. "funcname", "class::staticmethod", "class->instancemethod".
*/
function getArgs( $target, $subclass_ok = true ) {

    if(

strpos( $target, "::" ) ) {
        list(
$class, $target ) = explode( "::", $target, 2 );
       
$type = "::";
    }
    else if(
strpos( $target, "->" ) ) {
        list(
$class, $target ) = explode( "->", $target, 2 );
       
$type = "->";
    }
    else {
       
$type = NULL;
       
$class = NULL;
    }
   
$class and $class = new ReflectionClass( $class );

    foreach(

debug_backtrace() as $obj ) {

        if(

$obj['function'] == $target ) {
            if(
$type and $obj['type'] == $type ) {
               
$_cl = new ReflectionClass( $obj['class'] );
                if(
$_cl->getName() == $class->getName() or ( $subclass_ok and $_cl->isSubclassOf( $class ) ) ) {
                    return
$obj['args'];
                }
                unset(
$_cl );
            }
            else if( !
$type ) {
                return
$obj['args'];
            }
        }

    }

    return

NULL;

}

?>

Some example usage:
<?php
class Foo {
    function
test() {
       
$args = getArgs( "Foo->base" );
        print(
"the parameter 'v' to my call of base was: {$args[0]}n" );
    }
    function
base( $v ) {
       
$this->test();
    }
}
$f = new Foo();
$f->base( 713 ); // will print.. ".. my call of base was: 713"?>

Trust me, there are some reasons for why you might want to do this :)

bernyregeling AT hotmail DOT com

19 years ago


I wrote this function, in addition to jlim, for a nice NO-HTML output.

Thee result has similarities to a Java-error. Hope you like it.

(BTW, this function exits the script too, if debug_backtrace is displayed)

------------------------------

    function debug_bt()

    {

        if(!function_exists('debug_backtrace'))

        {

            echo 'function debug_backtrace does not exists'."rn";

            return;

        }

        //echo '<pre>';

        echo "rn".'----------------'."rn";

        echo 'Debug backtrace:'."rn";

        echo '----------------'."rn";

        foreach(debug_backtrace() as $t)

        {

            echo "t" . '@ ';

            if(isset($t['file'])) echo basename($t['file']) . ':' . $t['line'];

            else

            {

                // if file was not set, I assumed the functioncall

                // was from PHP compiled source (ie XML-callbacks).

                echo '<PHP inner-code>';

            }

            echo ' -- ';

            if(isset($t['class'])) echo $t['class'] . $t['type'];

            echo $t['function'];

            if(isset($t['args']) && sizeof($t['args']) > 0) echo '(...)';

            else echo '()';

            echo "rn";

        }

        //echo '</pre>';

        exit;

         }

seaside dot ki at mac dot com

16 years ago


I've started creating an external debug server for PHP. A PHP app require_once's a TADebugger(), which communicates with the debug sever. Find the OS X universal binary here [PHP source sample included]:

   http://www.turingart.com/downloads/phpDebugger.zip

Currently, TADebugger allows to post these properties back to the debug server:

- Call backtraces
- String messages
- Source files, which were referenced by a backtrace call

Note, that the binary is a early version.

icefragment at gmail dot com

16 years ago


A simple python-like backtrace. Note that I don't recurse into arrays if they are passed as arguments to functions.

function backtrace()
{
    $bt = debug_backtrace();

        echo("<br /><br />Backtrace (most recent call last):<br /><br />n");   
    for($i = 0; $i <= count($bt) - 1; $i++)
    {
        if(!isset($bt[$i]["file"]))
            echo("[PHP core called function]<br />");
        else
            echo("File: ".$bt[$i]["file"]."<br />");

                if(isset($bt[$i]["line"]))
            echo("&nbsp;&nbsp;&nbsp;&nbsp;line ".$bt[$i]["line"]."<br />");
        echo("&nbsp;&nbsp;&nbsp;&nbsp;function called: ".$bt[$i]["function"]);

                if($bt[$i]["args"])
        {
            echo("<br />&nbsp;&nbsp;&nbsp;&nbsp;args: ");
            for($j = 0; $j <= count($bt[$i]["args"]) - 1; $j++)
            {
                if(is_array($bt[$i]["args"][$j]))
                {
                    print_r($bt[$i]["args"][$j]);
                }
                else
                    echo($bt[$i]["args"][$j]);   

                                            if($j != count($bt[$i]["args"]) - 1)
                    echo(", ");
            }
        }
        echo("<br /><br />");
    }
}

zmorris at mac dot com

16 years ago


Hi, I got tired of using a trace( $message, __FILE__, __LINE__ ) function I made.  It forced me to include the file and line params (since php doesn't have macros) so I decided to make an alternative.

Simply call this new version using trace( 'my message' ); and it prints out a stack trace in a clearer way than the one stored in the debug_backtrace() array.  It handles traces from outside of functions, traces in nested functions, and traces in included files, and also displays the function in a way that can be pasted right back into your php code for faster testing!

NOTE - be sure to save your files with the correct line endings for the line numbers to work correctly, which for Mac OS X is unix.  You can get to this option in the popup menu in the toolbar at the top of each window in BBEdit.

<?phpfunction    print_var( $var )
{
   if(
is_string( $var ) )
       return(
'"'.str_replace( array("x00", "x0a", "x0d", "x1a", "x09"), array('', 'n', 'r', 'Z', 't'), $var ).'"' );
   else if(
is_bool( $var ) )
   {
       if(
$var )
           return(
'true' );
       else
           return(
'false' );
   }
   else if(
is_array( $var ) )
   {
      
$result = 'array( ';
      
$comma = '';
       foreach(
$var as $key => $val )
       {
          
$result .= $comma.print_var( $key ).' => '.print_var( $val );
          
$comma = ', ';
       }
      
$result .= ' )';
       return(
$result );
   }

      return(

var_export( $var, true ) );    // anything else, just let php try to print it
}

function   

trace( $msg )
{
   echo
"<pre>n";//var_export( debug_backtrace() ); echo "</pre>n"; return;    // this line shows what is going on underneath$trace = array_reverse( debug_backtrace() );
  
$indent = '';
  
$func = '';

      echo

$msg."n";

      foreach(

$trace as $val)
   {
       echo
$indent.$val['file'].' on line '.$val['line'];

              if(

$func ) echo ' in function '.$func;

              if(

$val['function'] == 'include' ||
          
$val['function'] == 'require' ||
          
$val['function'] == 'include_once' ||
          
$val['function'] == 'require_once' )
          
$func = '';
       else
       {
          
$func = $val['function'].'(';

                      if( isset(

$val['args'][0] ) )
           {
              
$func .= ' ';
              
$comma = '';
               foreach(
$val['args'] as $val )
               {
                  
$func .= $comma.print_var( $val );
                  
$comma = ', ';
               }
              
$func .= ' ';
           }
$func .= ')';
       }

              echo

"n";$indent .= "t";
   }

      echo

"</pre>n";
}
trace( 'error outside function' );

function   

test( $param1, $param2, $param3, $param4 )
{
  
trace( 'error in test()' );
}
test( 1.1, "param2n", array( 1 => "an", "bn" => 2 ), false );?>

admin at sgssweb dot com

16 years ago


Surprizingly, debug_backtrace() cannot aquire arguments from the function that is used as the second or later argument of a function.

<?phpfunction a($p) {
   
$backtrace = debug_backtrace();

        if (isset(

$backtrace[0]['args']))
       
var_export($backtrace[0]['args']);
    else
        echo
"Cannot aquire arguments";
    echo
"<br />";

        return

$p;
}

function

b($p1, $p2, $p3) {
    echo
"$p1, $p2, $p3";
}
// This outputs:
//    array ( 0 => 'First a', )
//    Cannot aquire arguments
//    Cannot aquire arguments
//    First a, Second a, Third a
b(a("First a"), a("Second a"), a("Third a"));?>

jlim#natsoft.com.my

19 years ago


Pretty print the backtrace(). Functions are indented based on call value, and file is linked using file:// for convenience.

Enjoy, John Lim

    function adodb_backtrace($print=true)
    {
        $s = '';
        if (PHPVERSION() >= 4.3) {

                    $MAXSTRLEN = 64;

                    $s = '<pre align=left>';
            $traceArr = debug_backtrace();
            array_shift($traceArr);
            $tabs = sizeof($traceArr)-1;
            foreach ($traceArr as $arr) {
                for ($i=0; $i < $tabs; $i++) $s .= ' &nbsp; ';
                $tabs -= 1;
                $s .= '<font face="Courier New,Courier">';
                if (isset($arr['class'])) $s .= $arr['class'].'.';
                foreach($arr['args'] as $v) {
                    if (is_null($v)) $args[] = 'null';
                    else if (is_array($v)) $args[] = 'Array['.sizeof($v).']';
                    else if (is_object($v)) $args[] = 'Object:'.get_class($v);
                    else if (is_bool($v)) $args[] = $v ? 'true' : 'false';
                    else {
                        $v = (string) @$v;
                        $str = htmlspecialchars(substr($v,0,$MAXSTRLEN));
                        if (strlen($v) > $MAXSTRLEN) $str .= '...';
                        $args[] = $str;
                    }
                }

                                $s .= $arr['function'].'('.implode(', ',$args).')';
                $s .= sprintf("</font><font color=#808080 size=-1> # line %4d,".
  " file: <a href="file:/%s">%s</a></font>",
  $arr['line'],$arr['file'],$arr['file']);
                $s .= "n";
            }   
            $s .= '</pre>';
            if ($print) print $s;
        }
        return $s;
    }

spagmoid at yahoo dot NOSPAMcom

19 years ago


ATTN: jlim#natsoft.com.my

Great function, but you have a few bugs.

At the line:
foreach($arr['args'] as $v)

Change it to:
$args = array();
if(!empty($arr['args'])) foreach($arr['args'] as $v)

And since line & file are not present in the array if calling from the error handler,

$Line = (isset($arr['line'])? $arr['line'] : "unknown");
$File = (isset($arr['file'])? $arr['file'] : "unknown");

and substitute accordingly.

Here's my version of it, alas with different formatting:
----------------------------------------

function DBG_GetBacktrace()
{
    $s = '';
    $MAXSTRLEN = 64;

        $s = '<pre align=left>';
    $traceArr = debug_backtrace();
    array_shift($traceArr);
    $tabs = sizeof($traceArr)-1;
    foreach($traceArr as $arr)
    {
        for ($i=0; $i < $tabs; $i++) $s .= ' &nbsp; ';
        $tabs -= 1;
        $s .= '<font face="Courier New,Courier">';
        if (isset($arr['class'])) $s .= $arr['class'].'.';
        $args = array();
        if(!empty($arr['args'])) foreach($arr['args'] as $v)
        {
            if (is_null($v)) $args[] = 'null';
            else if (is_array($v)) $args[] = 'Array['.sizeof($v).']';
            else if (is_object($v)) $args[] = 'Object:'.get_class($v);
            else if (is_bool($v)) $args[] = $v ? 'true' : 'false';
            else
            {
                $v = (string) @$v;
                $str = htmlspecialchars(substr($v,0,$MAXSTRLEN));
                if (strlen($v) > $MAXSTRLEN) $str .= '...';
                $args[] = """.$str.""";
            }
        }
        $s .= $arr['function'].'('.implode(', ',$args).')</font>';
        $Line = (isset($arr['line'])? $arr['line'] : "unknown");
        $File = (isset($arr['file'])? $arr['file'] : "unknown");
        $s .= sprintf("<font color=#808080 size=-1> # line %4d, file: <a href="file:/%s">%s</a></font>",
            $Line, $File, $File);
        $s .= "n";
    }   
    $s .= '</pre>';
    return $s;
}

ciprian dot stingu at gmail dot com

11 years ago


A function that i use for debug
I shortened variables name and i eliminated the spaces from second function in order fit in post :(

<?php
define
("LFP", './lt.log');
function
LogTrace($Argument, $lfn = LFP, $itw = '  ')
{
   
error_log("=====r", 3, $lfn);
   
error_log("[BEGIN BACKTRACE]r", 3, $lfn);
   
$it = '';
   
$Ts = array_reverse(debug_backtrace());
    foreach(
$Ts as $T)
       { 
        if(
$T['function'] != 'include' && $T['function'] != 'require' && $T['function'] != 'include_once' && $T['function'] != 'require_once')
        {
           
$ft = $it . '<'. basename($T['file']) . '> on line ' . $T['line']; 
            if(
$T['function'] != 'LogTrace')
            {
                if(isset(
$T['class']))
                   
$ft .= ' in method ' . $T['class'] . $T['type'];
                else
                   
$ft .= ' in function ';
               
$ft .= $Trace['function'] . '(';
            }
            else
               
$ft .= '(';
            if(isset(
$T['args'][0]))
            {
                if(
$T['function'] != 'LogTrace')
                {
                   
$ct = '';
                    foreach(
$T['args'] as $A)
                    {
                       
$ft .= $ct . LogVar($A, '', $it, $itw, 0);
                       
$ct = $it . $itw . ',';
                    }
                }
                else
                   
$ft .= LogVar($T['args'][0], '', $it, $itw, 0);
            }
           
$ft .= $it . ")r";
           
error_log($ft, 3, $lfn);
           
$it .= $itw;
        }           
    }
   
error_log("[END BACKTRACE]r", 3, $lfn);
}

function

LogVar(&$Var, $vn, $pit, $itw, $nlvl, $m = '')
{
    if(
$nlvl>=16) return;
    if(
$nlvl==0){$tv=serialize($Var);$tv=unserialize($tv);}
    else
$tv=&$Var;
   
$it=$pit.$itw;
    for(
$i=0; $i<$nlvl;$i++) $it.='.'.$itw;
   
$o='';$nl="n";
    if(
is_array($tv))
    {
        if(
strlen($vn)>0) $o.=$it.$m.'<array> $'.$vn.' = (';
        else
$o.="r".$it.$m.'<array> = (';
       
$o.= $nl;$AK=array_keys($tv);
        foreach(
$AK as $AN) {$AV=&$tv[$AN];$o.=LogVar($AV,$AN,$pit,$itw,$nlvl+1);}
       
$o.=$it.')'.$nl;
    }
    else if(
is_string($tv))
    {
        if(
strlen($vn)>0)$o.=$it.$m.'<string> $'.$vn.' = ';
        else
$o.=' '.$m.'<string> = ';
        if(
$tv===null) $o.='NULL';
        else
$o.='"'.$tv.'"';
       
$o.=$nl;
    }
    else if(
is_bool($tv))
    {
        if(
strlen($vn) > 0) $o.=$it.$m.'<boolean> $'.$vn.' = ';
        else
$o.=' '.$m.'<boolean> = ';
        if(
$tv===true) $o.='TRUE';
        else
$o.='FALSE';
       
$o.=$nl;
    }
    else if(
is_object($tv))
    {
        if(
strlen($vn)>0)
        {
           
$o.=$pit.$itw;
            for(
$i=0;$i<$nlvl;$i++) $o.='.'.$itw;
           
$o.=$m.'<'.get_class($tv).'::$'.$vn.'> = {'.$nl;
        }
        else
$o.=' '.$m.'<'.get_class($tv).'::> = {'.$nl;
       
$R=new ReflectionClass($tv);
       
$o.=$it.'.'.$itw.'Class methods {'.$nl;
       
$CM=$R->getMethods();
        foreach(
$CM as $MN => $MV)
        {
           
$o.=$it.'.'.$itw.'.'.$itw.implode(' ',Reflection::getModifierNames($MV->getModifiers())).' '.$MV->getName().'(';
           
$MP=$MV->getParameters(); $ct='';
            foreach(
$MP as $MPN => $MPV)
            {
               
$o.=$ct; $o.=$MPV->isOptional()?'[':'';
                if(
$MPV->isArray()) $o.='<array> ';
                else if(
$MPV->getClass()!==null) $o.='<'.$MPV->getClass()->getName().'::> ';
               
$o.=$MPV->isPassedByReference()?'&':''; $o.='$'.$MPV->getName();
                if(
$MPV->isDefaultValueAvailable())
                 {
                    if(
$MPV->getDefaultValue()===null) $o.=' = NULL';
                    else if(
$MPV->getDefaultValue()===true) $o.=' = TRUE';
                    else if(
$MPV->getDefaultValue()===false) $o.=' = FALSE';   
                    else
$o.=' = '.$MPV->getDefaultValue();   
                }
               
$o.=$MPV->isOptional()?']':''; $ct=', ';
            }
           
$o.=')'.$nl;
        }
       
$o.=$it.'.'.$itw.'}'.$nl; $o.=$it.'.'.$itw.'Class properties {'.$nl;
       
$CV=$R->getProperties();
        foreach(
$CV as $CN => $CV)
        {
           
$M=implode(' ',Reflection::getModifierNames($CV->getModifiers())).' ';
           
$CV->setAccessible(true);
           
$o.=LogVar($CV->getValue($tv),$CV->getName(),$pit,$itw,$nlvl+2,$M);
        }
       
$o.=$it.'.'.$itw.'}'.$nl; $o.=$it.'.'.$itw.'Object variables {'.$nl;
        
$OVs=get_object_vars($tv);   
        foreach(
$OVs as $ON => $OV) $o.=LogVar($OV,$ON,$pit,$itw,$nlvl+2);
       
$o.=$it.'.'.$itw.'}'.$nl; $o.=$pit.$itw;
        for(
$i=0;$i<$nlvl;$i++)    $o.='.'.$itw;
       
$o.='}'.$nl;
    }
    else
    {
        if(
strlen($vn)>0) $o.=$it.$m.'<'.gettype($tv).'> $'.$vn.' = '.$tv;
        else
$o.=' '.$m.'<'.gettype($tv).'> = '.$tv;
       
$o.=$nl;
    }         
    return
$o;   
}
//test
date_default_timezone_set('Europe/Bucharest');
$date = new DateTime('2010-01-28');
LogTrace($date);
?>

aryel at iku dot com dot br

15 years ago


An easy function to pull all details of the debug backtrace:

<?php
function getDebugBacktrace($NL = "<BR>") {
   
$dbgTrace = debug_backtrace();
   
$dbgMsg .= $NL."Debug backtrace begin:$NL";
    foreach(
$dbgTrace as $dbgIndex => $dbgInfo) {
       
$dbgMsg .= "t at $dbgIndex  ".$dbgInfo['file']." (line {$dbgInfo['line']}) -> {$dbgInfo['function']}(".join(",",$dbgInfo['args'])")$NL";
    }
   
$dbgMsg .= "Debug backtrace end".$NL;
    return
$dbgMsg;
}
?>

Then you can call it anywhere you want to get a string with the debug backtrace in readable format (i.e. your error handling function)

<?php
$backtrace
= getDebugBacktrace();
echo
"Fatal error! Cannot connect to database!";
echo
$backtrace;
?>

If you're running on command line, you might want to replace the line split. You can do that thru the function argument:

<?php
$backtrace
= getDebugBacktrace("n");
echo
"Error! Server is running out of foos! Dumping error backtrace";
echo
$backtrace;
?>

Hope that helps,
Aryel

В этом руководстве мы расскажем о различных способах того, как в PHP включить вывод ошибок. Мы также обсудим, как записывать ошибки в журнал (лог).

Самый быстрый способ отобразить все ошибки и предупреждения php — добавить эти строки в файл PHP:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Что именно делают эти строки?

Функция ini_set попытается переопределить конфигурацию, найденную в вашем ini-файле PHP.

Display_errors и display_startup_errors — это только две из доступных директив. Директива display_errors определяет, будут ли ошибки отображаться для пользователя. Обычно директива dispay_errors не должна использоваться для “боевого” режима работы сайта, а должна использоваться только для разработки.

display_startup_errors — это отдельная директива, потому что display_errors не обрабатывает ошибки, которые будут встречаться во время запуска PHP. Список директив, которые могут быть переопределены функцией ini_set, находится в официальной документации .

К сожалению, эти две директивы не смогут отображать синтаксические ошибки, такие как пропущенные точки с запятой или отсутствующие фигурные скобки.

Отображение ошибок PHP через настройки в php.ini

Если ошибки в браузере по-прежнему не отображаются, то добавьте директиву:

display_errors = on

Директиву display_errors следует добавить в ini-файл PHP. Она отобразит все ошибки, включая синтаксические ошибки, которые невозможно отобразить, просто вызвав функцию ini_set в коде PHP.

Актуальный INI-файл можно найти в выводе функции phpinfo (). Он помечен как “загруженный файл конфигурации” (“loaded configuration file”).

Отображать ошибки PHP через настройки в .htaccess

Включить или выключить отображение ошибок можно и с помощью файла .htaccess, расположенного в каталоге сайта.

php_flag display_startup_errors on
php_flag display_errors on

.htaccess также имеет директивы для display_startup_errors и display_errors.

Вы можете настроить display_errors в .htaccess или в вашем файле PHP.ini. Однако многие хостинг-провайдеры не разрешают вам изменять ваш файл PHP.ini для включения display_errors.

В файле .htaccess также можно включить настраиваемый журнал ошибок, если папка журнала или файл журнала доступны для записи. Файл журнала может быть относительным путем к месту расположения .htaccess или абсолютным путем, например /var/www/html/website/public/logs.

php_value error_log logs/all_errors.log

Включить подробные предупреждения и уведомления

Иногда предупреждения приводят к некоторым фатальным ошибкам в определенных условиях. Скрыть ошибки, но отображать только предупреждающие (warning) сообщения можно вот так:

error_reporting(E_WARNING);

Для отображения предупреждений и уведомлений укажите «E_WARNING | E_NOTICE».

Также можно указать E_ERROR, E_WARNING, E_PARSE и E_NOTICE в качестве аргументов. Чтобы сообщить обо всех ошибках, кроме уведомлений, укажите «E_ALL & ~ E_NOTICE», где E_ALL обозначает все возможные параметры функции error_reporting.

Более подробно о функции error_reporting ()

Функция сообщения об ошибках — это встроенная функция PHP, которая позволяет разработчикам контролировать, какие ошибки будут отображаться. Помните, что в PHP ini есть директива error_reporting, которая будет задана ​​этой функцией во время выполнения.

error_reporting(0);

Для удаления всех ошибок, предупреждений, сообщений и уведомлений передайте в функцию error_reporting ноль. Можно сразу отключить сообщения отчетов в ini-файле PHP или в .htaccess:

error_reporting(E_NOTICE);

PHP позволяет использовать переменные, даже если они не объявлены. Это не стандартная практика, поскольку необъявленные переменные будут вызывать проблемы для приложения, если они используются в циклах и условиях.

Иногда это также происходит потому, что объявленная переменная имеет другое написание, чем переменная, используемая для условий или циклов. Когда E_NOTICE передается в функцию error_reporting, эти необъявленные переменные будут отображаться.

error_reporting(E_ALL & ~E_NOTICE);

Функция сообщения об ошибках позволяет вам фильтровать, какие ошибки могут отображаться. Символ «~» означает «нет», поэтому параметр ~ E_NOTICE означает не показывать уведомления. Обратите внимание на символы «&» и «|» между возможными параметрами. Символ «&» означает «верно для всех», в то время как символ «|» представляет любой из них, если он истинен. Эти два символа имеют одинаковое значение в условиях PHP OR и AND.

error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);

Эти три строки кода делают одно и то же, они будут отображать все ошибки PHP. Error_reporting(E_ALL) наиболее широко используется разработчиками для отображения ошибок, потому что он более читабелен и понятен.

Включить ошибки php в файл с помощью функции error_log ()

У сайта на хостинге сообщения об ошибках не должны показываться конечным пользователям, но эта информация все равно должна быть записана в журнал (лог).

Простой способ использовать файлы журналов — использовать функцию error_log, которая принимает четыре параметра. Единственный обязательный параметр — это первый параметр, который содержит подробную информацию об ошибке или о том, что нужно регистрировать. Тип, назначение и заголовок являются необязательными параметрами.

error_log("There is something wrong!", 0);

Параметр type, если он не определен, будет по умолчанию равен 0, что означает, что эта информация журнала будет добавлена ​​к любому файлу журнала, определенному на веб-сервере.

error_log("Email this error to someone!", 1, "someone@mydomain.com");

Параметр 1 отправит журнал ошибок на почтовый ящик, указанный в третьем параметре. Чтобы эта функция работала, PHP ini должен иметь правильную конфигурацию SMTP, чтобы иметь возможность отправлять электронные письма. Эти SMTP-директивы ini включают хост, тип шифрования, имя пользователя, пароль и порт. Этот вид отчетов рекомендуется использовать для самых критичных ошибок.

error_log("Write this error down to a file!", 3, "logs/my-errors.log");

Для записи сообщений в отдельный файл необходимо использовать тип 3. Третий параметр будет служить местоположением файла журнала и должен быть доступен для записи веб-сервером. Расположение файла журнала может быть относительным путем к тому, где этот код вызывается, или абсолютным путем.

Журнал ошибок PHP через конфигурацию веб-сервера

Лучший способ регистрировать ошибки — это определить их в файле конфигурации веб-сервера.

Однако в этом случае вам нужно попросить администратора сервера добавить следующие строки в конфигурацию.

Пример для Apache:

ErrorLog "/var/log/apache2/my-website-error.log"

В nginx директива называется error_log.

error_log /var/log/nginx/my-website-error.log;

Теперь вы знаете, как в PHP включить отображение ошибок. Надеемся, что эта информация была вам полезна.

PHP is a great language to start with when you are learning how to code. It has a simple syntax, it’s easy to learn and you can make dynamic websites with it. But even though it’s easy to write PHP code, it’s not always easy to debug. There are a lot of tools out there that can help you, but since PHP is an interpreted language, you can also use a couple of debugging techniques to help you find bugs in your code.

In this blog post I’ll cover the the following sections.

  1. What is Debugging?
  2. Debugging techniques in PHP
  3. Debugging tools in PHP

#1 What is Debugging?

The term bug was first used by Thomas Edison. Debugging is nothing but diagnosing the errors in the programs and determining how to correct them. The most common type of mistakes are:

  • Programming without thinking
  • Writing code in an unstructured manner

The debugging process involves multiple steps like

  • Identifying the bug
  • Identify the location of the bug
  • Analysis of the bug
  • Fix the bug
  • Apply the fix and test
Why do we need Debugging?
  • Reports an error immediately so that we can detect the errors in it’s earlier stage.
  • It assists the developer in reducing useless and distracting information.

#2 Debugging Techniques In PHP

If the application has some fatal or syntax error we can find it easily since the terminal or console will itself show where the error has arisen. When it comes to logical error or show erroneous data one has to debug the code. The simple way to debug code is by echoing the output values.

Here are a few functions which you can use for debugging, will help you in printing the output values.

  • Method 1: var_dump()
  • Method 2: print_r()
  • Method 3: get_defined_vars()
  • Method 4: debug_zval_dump()
  • Method 5: debug_print_backtrace()
  • Method 6: debug_backtrace()

Method 1: var_dump($var)

  • Used to dump information about a variable.
  • Displays structured information such as the type and value of the given variable.

Syntax: var_dump(variable1, variable2, ….variable n)

Sample Code:

<?php
$var_name3="678";
$var_name4="sara";

echo var_dump($var_name1)."<br>"; 
echo var_dump($var_name2)."<br>";
?>

Output:

string(3) "678"
string(4) "Sara"

Method 2: print_r($var)

  • Prints the information stored in a variable.
  • Prints the value stored in the variable in a human-readable format.

Syntax: print_r(var_name, return_output)

Code:

<?php
$var1="sara";
$var2=123456;

print_r($var1);
echo "<br>";

print_r($var2);
echo "<br>";

$arr = array("Name"=>"Sara" ,"Age"=>20);
print_r($arr);
?>

Output:

sara
123456
Array ( [Name] => Sara [Age] => 20 )

Method 3: get_defined_vars()

  • Gets all the defined variables including built-ins and custom variables.
  • It returns an array of all the defined variables.

Syntax: get_defined_vars()

Sample Code:

<?php
$array1 = array("Nathan", "Brown");
$arr = get_defined_vars();

print_r($arr["array1"]);
?>

Output:

Array ( [0] => Nathan [1] => Brown )

Method 4: debug_zval_dump()

  • Dumps the variable with its reference counts. This is useful when there are multiple paths to update a single reference.

Syntax: debug_zval_dump (var_name)

Sample Code:

<?php
$var1 = "Hello World";
$var2 = "";
$var2 = $var1;

debug_zval_dump($var1);
?>

Output:

string(11) "Hello World" refcount(4)

Method 5: debug_print_backtrace()

  • Prints a backtrace that shows the current function call-chain.

Syntax: debug_print_backtrace() (var_name)

Sample Code:

<?php
$var1 = "Hello World";
$var2 = "";$var2 = $var1;

debug_print_backtrace($var1);
?>

Output:

string(11) "Hello World" refcount(4)

Method 6: debug_backtrace()

  • Generates a back-trace.
  • It will print out the chain of code that leads up to the point where the back-trace function is called.

Sample Code:

<?php   
function sample($str) {
    echo "Hi: $str";      
    var_dump(debug_backtrace());   
    }

sample('Debugging');
?>

Output:

Hi: Debuggingarray(1) {
[0]=> array(4) { 
["file"]=> string(61) "/home/vaishnavi/testphp/index.php" 
["line"]=> int(6) 
["function"]=> string(6) "sample" 
["args"]=> array(1) { 
[0]=> string(9) "Debugging"    
		}   
  	} 
}

#3 Debugging Tools In PHP

While there are a number of debugging tools PHP developers can use, the best way to debug PHP code is to use the tools that PHP makes available. Here I have listed few most used tools, you can find the best one out of it.

  • Xdebug
  • ZendDebugger
  • phptrace
  • phpdbg
  • ltrace
  • Inotify

Debugging Xdebug in VS Code

It was first released in May 2002 by Derick Rathans. Xdebug is a PHP extension which provides debugging capabilities. It uses the DBGp debugging protocol.Xdebug is a PHP extension which provides debugging and profiling capabilities. It uses the DBGp debugging protocol. At this end this tool is responsible to enable the debugging on PHP, and Xcode takes advantage of it.

Basic Features in Xdebug

  • We can set and remove the break points.
  • Allows remote debugging.
  • Enable call logging functions.
  • Sets a manual variable watch.
  • Enable profiling.

Installation with VS Code

Visual Studio Code is a free source-code editor which is light-weight yet more powerful made by Microsoft for Windows, Linux and macOS. It supports features like syntax highlighting, snippets, embedded git, debugging and so on.

  • Install the Xdebug extension plugin.

  • Click on the debug tab on the left panel and click on the create a launch.json file, select Add configuration.

  • A launch.json file will get opened and click on the Add configuration button at the right corner of the page.

  • Click on the PHP Listen for Xdebug option and click on add configuration button again to add the PHP Launch currently open script option.

  • Click on the PHP option from the drop down menu. A launch.json file will get opened, specify the runtimeExecutable which is the path of your PHP.exe interpreter and save the changes in your launch.json file.

How to debug the code using Xdebug?

After the setting up in VS code you can add the PHP file to start the debugging process. Here I have explained in detail about how to run debug using both Launch currently open script and Listen for xdebug methods.

a.) Using Launch Currently Open Script method:

1. Open a .php file, add breakpoints to your code.

Click on the plus icon and add breakpoints. Breakpoints stop the code execution at every breakpoint so that you can see variables output in only one iteration of code.

You can also add breakpoints in a single click. Click on the line of the code in VS code where you want to add breakpoint.

2. Set the debugger to «Launch Currently Open Script». Click on the green play button to implement the «Launch currently open script» feature which helps you in debugging the single script without using a browser.

3. To start the debug process, press F5 or click on the green arrow in the top right corner.

4. You will be able to see the debugger break at your breakpoint.

5. Upon clicking on the green arrow button, it starts a listener that will be triggered once you run the PHPUnit test.

b.) Using Listen for xDebug method:

In this method you will need the browser to debug.

  1. Open a .php file and add some breakpoints.
  2. Select the debug option to ‘Listen for xDebug’.
  3. To start the debug process, press F5.
  4. Open Chrome and navigate to your index.php file.
  5. Click the new XDebug Helper extension and click the Debug option.
  6. The helper icon would have turned green.
  7. Start the debugging process by refreshing the page.
  8. Go back into VS Code and you will see that it has stopped at the breakpoint.

Advantages Of Debugging:

  • Allows the detection of errors in the initial stage and makes the software development process easier using the error reports.
  • By finding errors in software, it allows developers to fix the errors before releasing them and provides bug-free software to the customers.

Here are a few steps that can help a programmer to repair errors easily:

  1. Make sure you have the right information about the error.
  2. Always refer to the user’s guide for more information about the error.
  3. Make sure that your program is working properly.
  4. Use the debugger to fix the error.
  5. Use the resources available on the internet to find

Are You All Set To Debug Your PHP Code?

Every organization wants to be at the top of the market and this is possible only if our software is bug-free. Using debugging we can resolve bugs and produce a bug-free software to our customer. Identifying errors in the early stage while debugging saves time. One can find bugs with their previous experience, learning the product carefully, using print statements and so on.

To boost your revenue keep debugging your code to find all the possible errors before your customer experiences it. In this blog post I have given the brief explanation on Xdebug tool about how to set up it in VS code and how to run. I will cover the other tools in the next blog.

Hope this article helps in debugging your application!!

Понравилась статья? Поделить с друзьями:

Не пропустите эти материалы по теме:

  • Яндекс еда ошибка привязки карты
  • Php soap ошибка
  • Php выкинуть ошибку
  • Php require once ошибка
  • Php вызов ошибки

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии