tags:

views:

89

answers:

4

Hi, I'm having an issue with PHP losing data in a variable. There is quite a bit of data in the variable, because it basically contains a binary file, but I'm wondering if this is cause for it to completely lose it's information.

Looking at a snippet from my code which is used to deal with email attachments:

var_dump($data);  
if (array_key_exists('filename', $params) || array_key_exists('name', $params)) {  
    var_dump($data);  
 ...
}

The first var_dump gives the desired output of the file:

"string(283155) "
--Apple-Mail-5-930065543 ... etc

while the second gives an output of:

string(0) ""
...
string(0) ""

Any idea why this is happening? Does PHP just drop data in variables if they are really large? (I didn't think so, as I've never had this problem before)

If so, any workaround?

Thanks!

Edit: also worth mentioning that there is no code missing between these statements. I also just tried it with a shorter string and I'm getting the correct output for both var_dump calls

+3  A: 

No, PHP is definitely not deleting arbitrary variables. Some reasons that could lead to that behaviour:

  • You are using the variable $data for something else between the two calls to var_dump()
  • If $data is global it might be modified by another function.
  • You have another var_dump() that you think is the one printing $data, but that actually prints another variable
  • $data is a reference (assigned by =&) and the referenced variable is modified.
soulmerge
+2  A: 

It should never happen. $data gets purified somewhere between var_dumps. It's hard to say where $data gets changed without seeing the code, but I had similar problems when I worked with old code that overused include. For example,

$data = "my_data";
include "file1.php"  // $data can be changed here !
print($data); // not "my_data"  anymore. 
a1ex07
This was close to my issue...the variable was being used outside of the places I had thought it was being used and this was causing issues. Thanks
Conor B
+2  A: 

Smells like one of:

  1. You use $data outside its scope. E.g. it is global but you use it in a function() block. In that case you must declare access to this global in your function block: globabl $data.
  2. You use references. You will probably want to convert this into ordinary variable semantics, but you'll have to be careful that code which assumes reference semantics is fixed accordingly. (The PHP manual has a section on references, it's worth reading.)
A: 

Possible reasons:

  • $params is not an array - the script you thought was supposed to assign it as an array failed - so the if expression resolves to false. Check your error reporting level to make sure it includes E_ALL & E_NOTICE and see if 'not an array' errors show up in your log. You are also calling var_dump() on $data (after you empty out all array values) or on some other variable again later on down your script, making you think the second var_dump() is being called.
  • $params is an array, but neither of those keys exist within it, so the if expression resolves to false. You are also calling var_dump() on $data (after you empty out all array values) or on some other variable again later on down your script, making you think the second var_dump() is being called.
  • Your PHP build is not the official build and contains added functionality of var_dump() to take the variable by reference, and empty all array values while preserving keys. Or perhaps it makes if() change scope. This doesn't seem like a value-added feature, so best not use anything but the official build.
  • You are not correctly describing the behavior or have excluded code in between the lines which would help explain it. You are missing semicolons and the list of known keys in $param, so it's safe to assume this isn't the real, full code.
bob-the-destroyer