How to dump() in PHP?

I was absent from Grav for some time and wanted to continue develoment. About two or three years ago I could simply place a dump() command inside a PHP file and saw the content in the browser. This is not the case any more. I see nothing. Any ideas?

I tried it on a hosted site where it worked in the past as well as on a fresh local installation on my laptop. It runs Arch with php7-apache, php7-fpm, php7-gd using built-in symfony.

1 Like

Thanks for providing all that environment info. :star: More posters should do this!

Pretty sure this PHP command is still supported. The only reasons I can imagine that it might not render are:

  • CSS or something else in the page layout is hiding the dump output
  • the PHP code is being skipped (for example, within an if block that is evaluated false)

Take a look at the chapter on other available debug techniques as well, sometimes there are better options than PHP dump().

Let us know how you go, please.

Thanks for your reply. I have a fresh install of grav-admin and added the events plugin. To the plugin I added the following code:

	public function onAdminSave(Event $event)
	{
		$this->grav['log']->info('Entering onAdminSave');

		$config = (array) $this->config->get('plugins.events');
		dump($config);
		$this->grav['log']->info('onAdminSave dump finished');
        ....

This is the only change I made after installation and it leads to

[2022-11-09 21:31:10] grav.INFO: Entering onAdminSave [] []
[2022-11-09 21:31:10] grav.INFO: onAdminSave dump finished [] []

in grav.log. But I see nothing from the dump.

So it looks like that code block is being run. Since this is part of Admin and that has lots of tricky JS and CSS and AJAX, I think it might be the first option I suggested.

Sometimes I have added die(); after the dump() when I suspect my dump output is being obscured.

Thank you very much for that hint. When I add die(); I can see the dump. It’s not very comfortable, but it will help.

What about logging variable contents to log file or debugbar? The documentation didn’t help me much, as the PHP examples only output some textstrings. How do I use it for more complicated PHP types?

I don’t know, but here are some suggestions:

  • remove die() and view the page source (it’s obscured in rendering but should be in there somewhere); browser ‘Inspect’ might show something semi-readable too
  • enclose your ugly dumped output before die() with <pre>...</pre>
  • use the other debugger “clockwork” - I understand that it’s much better (you’d need to use $this->grav['debugger']->addMessage($myvariable) from PHP)
  • try Xdebug if it’s available - have only just started with this and have not got everything working yet.

The reason why dump() doesn’t show during onAdminSave is because the Save action is performed by multiple task requests to the server.

When hitting Save on a page:

  • The first request: onAdminSave is being called, but there is no call to onOutputGenerated.
  • In subsequent requests, onAdminSave is not being called, and onOutputGenerated is being called in some of them.

Conclusions:

  • So I guess that any output generated using dump() during onAdminSave will never be sent to the client.
  • dump() does work in events like OnPluginInitialized, onPageInitialized, …

Just curious, why would one use dump() when one can step through code using a debugger?

	public function onAdminAfterSave(Event $event)
	{
		$this->grav['debugger']->addMessage("onAdminAfterSave");

		$icalendar = new \Events\iCalendarProcessor();
		$this->grav['debugger']->addMessage($icalendar);

doesn’t show anything in the debugbar. What am I doing wrong?

Because one didn’t know that it’s possible :see_no_evil:
Are you talking about Xdebug, too?

@myscha,

Because one didn’t know that it’s possible :see_no_evil:

Uh… wasn’t that part of course Programming 101 ? :wink:

Are you talking about Xdebug, too?

Yes. See Xdebug docs.

Here is a screenshot when using Xdebug in VScode while hovering over parameter $event after a breakpoint inside onAdminSave (line 88) has been hit.

And this is the list of variables shown in the left sidebar of VScode

I assume you use a/some VScode plugin/s, can you tell which one/s for PHP?

1 Like

I would also like to know this.

Plenty of tutorials can be found on the web…

  • What have you tried sofar? Which steps?
  • Where in the process of setting up Xdebug with VSCode did you get stuck?

In short:

  • Install Xdebug on your system and update Apaches PHP ini file.
  • Install Xdebug Helper for Chrome or Firefox.
    • Browse to the website you wish to debug, open extension and switch on “Debug”.
  • In VSCode:
    • Install VSCode extension:
      • PHP Debug by Xdebug
    • While you’re adding extensions, add as a bonus for PHP editing:
      • PHP Intelephense by Ben Mewburn
      • Twig Language 2 by mblode
    • From the left menu hit “Run and Debug” icon. Then, in the sidebar:
      • Click “Create a launch.json file”
      • Chose “PHP” from dropdown.
      • Start the debugger using the green arrow