开发者

PHP Losing variable data

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 ther开发者_如何学JAVAe 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


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. 


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.


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.)


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.


Strange I see the same behaviour. But now only for a specific value

// print "Entering getFieldVisibilityPermission(".$fld_module.",". $userid.",". $fieldname.") method ...";
// next line looses $userid only when it has value 12 for value 11 it has no problem
// require('user_privileges/user_privileges_' .$userid. '.php'); -> results in user_privileges_.php 
// require('user_privileges/user_privileges_' .$userid. '12.php'); -> results in user_privileges_1212.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜