Debugging in PHP. Now what? Explaining var_export, a useful and powerful ally

Hello.
I'm Bandai, the Wild Team member in charge of the development team.

Debugging is an inevitable part of development. In this article, I would like to explain var_export, a powerful ally when debugging

What do you do when debugging?

I think the most basic of the basics is var_dump. If you don't know this, you've probably never written PHP.
However, var_dump cannot output to the log (that is, by itself. It's not impossible, but it's not very convenient when it comes to debugging purposes).
I think this is the biggest problem with var_dump.

So, is it print_r?
Echoing pre tags before and after print_r is so common that it's practically a joke. I do it sometimes (do I ever do it?).

Serialize? Do you put it into a database? If it's based on PHP, that's fine... but of course not. Debugging with serialization seems like a thorny path, but it's something that people do sometimes

If you want to display it directly, var_dump is fine, but if you want to write it down in a log, I recommend var_export.
Perhaps it's not worth yelling about now.

What is var_export?

The var_export function, as its name suggests, is used to export variables.
As proof of this, the information it outputs is exactly like a PHP variable declaration.

<?php $hoge = array( 'a', 'b', 'c', 'd', ); var_export($hoge);

The output will be similar to the following:

array ( 0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', )

By the way, the format of var_dump and print_r is as follows

// var_dump array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" } // print_r Array ( [0] => a [1] => b [2] => c [3] => d )

By the way, the sample source of
PHP: var_export - Manual shows how to use eval to redefine var_exported data as a variable, but the purpose is unclear, but that's basically what it does.

 

The true power of var_export

var_export is a powerful tool that is fully utilized in API development, which is our specialty.
In API development, received requests are often processed and output in JSON format, but when using var_dump, JSON data with the var_dumped information is returned as a response, which will definitely result in an error when parsing the JSON on the client side.

In such cases, you will likely want to tail the log file to check its operation, but as mentioned above, writing var_dump information to the log file requires a bit of skill.
With var_export, you can simply add true to the second argument to store the output in a variable, making it very easy to output to a log file.

With fuelphp, you can output to a log file as follows:

$hoge = array( 'a', 'b', 'c', 'd', ); Log::debug(var_export($hoge, true));

This is something that var_dump with variable arguments cannot do

var_dump is useful because it also outputs the number of data items in the array

All I can say is get used to
it

If you want to consult with a development professional

At Beyond, we combine our extensive track record, technology, and know-how in system development with OSS technology and cloud technologies such as AWS to provide contracted development of web systems with reliable quality and excellent cost performance

We also handle server-side/back-end development and proprietary API collaboration development, making full use of our technology and know-how in building and operating web system/application infrastructure for large-scale, high-load games, applications, and digital content

If you have any problems with your development project, please visit the following website

● Web system development
● Server-side development (API/DB)

If you found this article useful, please click [Like]!
0
Loading...
0 votes, average: 0.00 / 10
3,287
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also grateful to be able to do a variety of other work, including marketing.
My portrait rights within Beyond are CC0.