Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would cause a print_r and/or a var_dump to fail debugging a variable?

I'm attempting to debug the PayPal review process in Magento. Every time I try to dump the following variable I get a white page:

//the variable declaration:
$shippingAddress = $this->getShippingAddress();

//the dump that breaks the page:
 <?php echo '<pre>';print_r($shippingAddress);echo '</pre>'; ?>

I also tried with a variable on the page that was being used for something other than if statements.

//this variable displays results
<?php echo '<pre>';print_r($billingBlock->setShowAsShippingCheckbox(true)->toHtml());echo '</pre>'; ?>

//however, this one does not:
<?php echo '<pre>';print_r($billingBlock);echo '</pre>'; ?>

I was just wondering what might cause my var_dump to break the page? How do I see what is in the object if I can't dump it?

like image 701
Front_End_Dev Avatar asked Feb 05 '14 21:02

Front_End_Dev


People also ask

What is the difference between Var_dump () and Print_r ()?

var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.

What is Var_dump () used for?

The function var_dump() displays structured information (type and value) about one or more expressions/variables. Arrays and objects are explored recursively with values indented to show structure. All public, private and protected properties of objects will be returned in the output.

Why Var_dump () is preferable over Print_r ()?

It's too simple. The var_dump() function displays structured information about variables/expressions including its type and value. Whereas The print_r() displays information about a variable in a way that's readable by humans. Example: Say we have got the following array and we want to display its contents.

What is Var_dump in Javascript?

The var_dump() function is a built-in function of PHP that dumps the information about the variables. This information includes the data type and value of the variable. In case of string, it also includes the size of the string passed inside the function.


1 Answers

First, PHP never "just white pages". When you get a blank screen, that means PHP's execution has halted fro some reason. However, unless your server has been configured to not log errors, the PHP error log or the Magento exception log should have an error for you.

As far as your specific problem goes, many of Magento's objects contain reference to a large amount of information — and sometimes the references are circular. PHP's var_dump and print_r functions will blindly follow these circular references and attempt to print everything out. This eventually leads to PHP using more memory than is allowed by the memory_limit ini setting, and execution halts.

Most PHP professionals use the xDebug extension to work around this. The xDebug extension has a modified var_dump that will limit the amount of information dumped, which prevents the above memory limit problems. The xdebug.var_display_max_children, xdebug.var_display_max_data, and xdebug.var_display_max_depth ini settings are the ones you'll want to tweak if xDebug's still not helping with the memory limit problem. (some PHP distributions have these set too high initially)

If that's not a possibility, a little caution with your var_dump's can still help.

Use this to figure out the variable type

var_dump(get_class($thing));

If it's a Magento object, use this to see its data keys

var_dump(array_keys($thing->getData()));

And then output individual data members with

var_dump($thing->getData('key_name'));
var_dump($thing->getKeyName()));
like image 123
Alan Storm Avatar answered Nov 15 '22 06:11

Alan Storm