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

table of contents
Hello.
I'm Bandai, the Wild team member in charge of development.
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 var_dump is the absolute basics. Anyone who doesn't know about it probably hasn't written any PHP code before.
However, var_dump cannot output to a log (that is, not on its own. It's not impossible, but it's not convenient at all when considering debugging purposes).
I think this is the biggest problem with var_dump.
So, is it print_r then?
Echoing the pre tag before and after print_r is so common it's almost a joke. I do it sometimes (do I?), though.
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 everything directly, var_dump is fine, but if you're using it for purposes like writing to a log, I'd recommend var_export.
Maybe it's not something I need to be emphasizing at this point.
What is var_export?
The `var_export` function, as its name suggests, is used to export variables.
As proof of this, the output is a direct representation of 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 )
Incidentally,the PHP: var_export - Manual the sample source code in
shows how to define var_exported data again as a variable using eval, although its purpose is unclear. That's essentially what it does.
The true power of var_export
var_export truly shines in API development, an area in which we excel.
In API development, it's common to process received requests and output them in JSON format. However, using var_dump returns JSON data with var_dump information attached as the response, which inevitably causes errors when the client tries to parse the JSON.
In such cases, you'll often check the operation by tailing the log file, but as mentioned earlier, writing var_dump information to a log file requires a bit of a trick.
With var_export, you can store the output in a variable simply by adding true as the second argument, making outputting to a log file very easy.
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, you'll get used to it.
That's all.
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)
1
