Using xdebug to find out why a variable is mysteriously set to 0 Thanks in advance for your help! I'm new to xdebug and trying to figure it out. I have a situation I'm trying to debug where a variable is mysteriously set to 0.

I know from xdebug (I'm using PHPStorm) that it happens at this specific line:

// breakpoint here: $id_key is 'my_entity_id_key'
$e_entity_ids = my_module_fetch_col_keyed($query, $id_key, $created_column);
// breakpoint here: $id_key is 0


It's weird, because $id_key is not a reference, nor does the function take a reference as a parameter. Also, after the value has changed to 0, $id_key is blue in the variables pane of the debugger. I should note that $e_entity_ids comes out here with the proper value.

I've tried making a copy of the variable and using it to pass to the function instead:

$id_key2 = $id_key;
$e_entity_ids = my_module_fetch_col_keyed($query, $id_key2, $created_column);
// breakpoint here: $id_key is 0, and so is $id_key2


I'm wondering:


How could the value of $id_key be mysteriously changing to 0?
How can I create a copy of $id_key so that it's not changed when I pass the copy to my_module_fetch_col_keyed()?
What does it mean when a variable name is blue in the variables pane in PHPStorm?


When I step into my_module_fetch_col_keyed(), there's a line after which $column is mysteriously 0, as shown:

function my_module_fetch_col_keyed(Query $query, $column, $key_column = NULL) {
  try {
    $result = $query-&gt;execute();
  }
  catch (Exception $e) {
    return FALSE;
  }

  if (!isset($key_column)) {
    try {
      // breakpoint here: $column is 'my_entity_id_key'
      $column_results = $result-&gt;fetchCol($column);
      // breakpoint here: $column is mysteriously 0
    }
    catch (Exception $e) {
      return FALSE;
    }
    return $column_results;
  }

  try {
    $assoc = $result-&gt;fetchAllAssoc($key_column);
  }
  catch (Exception $e) {
    return FALSE;
  }

  $keyed = array();
  foreach ($assoc as $key =&gt; $result) {
    $keyed[$key] = $result-&gt;{$column};
  }

  return $keyed;
}
The key idea here is to use the "step into" functionality of PHPStorm to figure out when the second argument ($column within the function's declaration) gets changed.  $id_key shouldn't change at all if you're describing the calling conventions correctly, the second argument would have to be declared as &amp;$column, i.e., as a reference to the variable.  So I'm assuming that's what you mean. Otherwise, PHP passes a scalar variable "by value", and won't let any changes made to the variable in the body of the function change the original variable outside of it.

And there's nothing inside of the function that will change it either.  Here J. Reynolds is right; you're passing the wrong thing to fetchCol(), which wants an integer. But fetchCol() doesn't modify its argument either.

Since you're using xdebug, this then raises an important question: how are you using it?  Is your php process running on the same server as PHPStorm?

I ask this because one thing can definitely go wrong using xdebug: you can be debugging a different file than you think you are, if you're "mapping" a file on your server to a local file. If the file isn't the same, weird things can happen like what you describe. You may not really be on the line you think you are, since xdebug will tell PHPStorm the file line on the server, which may be a different line entirely on your local file. And the values of variables, if they display at all, may have different values than what the code displaying in PHPStorm allows.

Without knowing how you've set up PHPStorm to get to your PHP process, it's hard to know what exactly's going on. But you should describe what technique you're using, so folks can give you a better answer.