Jump to content

Pass by reference Deprecated?


Drongo_III

Recommended Posts

Hi Guys

 

Probably a simple answer to this.

 

Writing a script where I have a foreach to escape data in a multi dimensionsal array - destined for the database.

 

I want to preserve the the escaped values in the array so I've passed in the value by referece. See code below:

 

foreach($myarray as $key=>$value){

foreach($value as $k=>$v){

$v =   $mysqli->real_escape_string(&$v);
echo  $v ."<br/>";

}
  
  }

 

 

I've switched on error_reporting(E_STRICT), because i read it was good practice to build your scripts with this on.

 

Anyway - when i pass by reference I get a message as follows:

 

Deprecated: Call-time pass-by-reference has been deprecated in C:\wamp\www\wh\C.php on line 105

 

So if pass by reference is deprecated, what's the alternative?

 

I realise i could pass the new values to a new array. But does this mean i shouldn't pass by reference anymore?

 

Many thanks and sorry for going round the planet to ask such a simple question.

 

Drongo

 

 

Link to comment
https://forums.phpfreaks.com/topic/259148-pass-by-reference-deprecated/
Share on other sites

The property way to use a parameter as a reference is to declare that in the function, then just pass the variable as normal.  Example:

 


function myFunc(&$ref){
    $ref = 567;
}


$v = 123;
myFunc($v);

 

 

You should only be using the reference if you need too.  Your pasted code doesn't look like anything that would need to use a reference so just remove it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.