PHP callback using a reference [duplicate] - php

This question already has answers here:
Why does PHP's call_user_func() function not support passing by reference?
(4 answers)
Closed 8 years ago.
I have:
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a."\n";
Why does this return:
Warning: Parameter 1 to increment() expected to be a reference, value given in
and $a is still 0. Why is this?
Any references to official documentation would help.

Documentation says "Note: Note that the parameters for call_user_func() are not passed by reference."
You might use call_user_func_array instead.
function increment(&$a) {
$a++;
}
$x = 1;
call_user_func_array("increment", array(&$x));
echo $x;

From the documentation of call_user_func:
Calls the callback given by the first parameter and passes the remaining parameters as arguments.
This is what you want using call_user_func_array instead of call_user_func:
<?php
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func_array("increment", array(&$a));
echo $a."\n";

Related

how the value incresed without increasing in the this php code [duplicate]

This question already has answers here:
What does "&" mean in '&$var' in PHP? [duplicate]
(2 answers)
Closed 3 months ago.
<?php
function doSomething( &$arg ){
$return = $arg;
$arg += 1;
return $return;
}
$a = 3;
$b = doSomething( $a );
echo $a.' ';
echo $b;
?>
I know the answer a=4 and b=3 I understand how b= 3 but how come value of the a increased
The & operator tells PHP not to copy the variable when passing it to the function. Instead, a reference to the variable is passed into the function, thus the function modifies the original variable instead of a copy.
Therefore $arg += 1; will increase $a

Does PHP declares variables passed to functions args by reference? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 10 years ago.
I was trying to find this answer on Google, but I guess the symbol & works as some operator, or is just not generally a searchable term for any reason.. anyhow. I saw this code snippet while learning how to create WordPress plugins, so I just need to know what the & means when it precedes a variable that holds a class object.
//Actions and Filters
if (isset($dl_pluginSeries)) {
//Actions
add_action('wp_head', array(&$dl_pluginSeries, 'addHeaderCode'), 1);
//Filters
add_filter('the_content', array(&$dl_pluginSeries, 'addContent'));
}
This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:
$a = 1;
function inc(&$input)
{
$input++;
}
inc($a);
echo $a; // 2
Objects will be passed by reference automatically.
If you like to handle a copy over to a function, use
clone $object;
Then, the original object is not altered, eg:
$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1
The ampersand preceding a variable represents a reference to the original, instead of a copy or just the value.
See here: http://www.phpreferencebook.com/samples/php-pass-by-reference/
This passes something by reference instead of value.
See:
http://php.net/manual/en/language.references.php
http://php.net/manual/en/language.references.pass.php
I used it for sending a variable to a function, and have the function change the variable around. After the function is done, I don't need to return the function to the return value and set the new value to my variable.
Example
function fixString(&$str) {
$str = "World";
}
$str = "Hello";
fixString($str);
echo $str; //Outputs World;
Code without the &
function fixString($str) {
$str = "World";
return $str;
}
$str = "Hello";
$str = fixString($str);
echo $str; //Outputs World;

Multiple functions using array_map [duplicate]

This question already has answers here:
Array_Map using multiple native callbacks?
(3 answers)
Closed 1 year ago.
array_map accepts string as its first argument. Is there a way, to use arrays instead of strings, like:
.... array_map( array('trim','urlencode'), $my_array);
so I could attach multiple functions.
You can define a function to combine these trim and urlencode functions. Then use the new function name or the new function as the first parameter of the array_map() function.
array_map(function($v){
$v = trim($v);
$v = urlencode($v);
return $v
}, $array);
You can do it this way also. Reference: create_function()
Warning: This function has been DEPRECATED as of PHP 7.2.0. Relying on this
function is highly discouraged.
Try this here code snippet here
$newfunc = create_function('$value', 'return urlencode(trim($value));');
$array=array_map($newfunc, $array);

PHP variable scope manual "The global keyword" [duplicate]

This question already has an answer here:
Global variables aren't working as expected with frameworks
(1 answer)
Closed 7 years ago.
According to the PHP variable scope manual in the "The global keyword" section we have the following piece of code:
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
The above script will output 3.
but in my case is echo out 2.
I misunderstand something?
You have to call the Sum() function in order to modify the value of $b. Make sure you call it before you echo. If you encapsulated your $a and $b inside a function or class or namespace, it will not work like in that example.

What does the "&" sign mean in PHP? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 10 years ago.
I was trying to find this answer on Google, but I guess the symbol & works as some operator, or is just not generally a searchable term for any reason.. anyhow. I saw this code snippet while learning how to create WordPress plugins, so I just need to know what the & means when it precedes a variable that holds a class object.
//Actions and Filters
if (isset($dl_pluginSeries)) {
//Actions
add_action('wp_head', array(&$dl_pluginSeries, 'addHeaderCode'), 1);
//Filters
add_filter('the_content', array(&$dl_pluginSeries, 'addContent'));
}
This will force the variable to be passed by reference. Normally, a hard copy would be created for simple types. This can come handy for large strings (performance gain) or if you want to manipulate the variable without using the return statement, eg:
$a = 1;
function inc(&$input)
{
$input++;
}
inc($a);
echo $a; // 2
Objects will be passed by reference automatically.
If you like to handle a copy over to a function, use
clone $object;
Then, the original object is not altered, eg:
$a = new Obj;
$a->prop = 1;
$b = clone $a;
$b->prop = 2; // $a->prop remains at 1
The ampersand preceding a variable represents a reference to the original, instead of a copy or just the value.
See here: http://www.phpreferencebook.com/samples/php-pass-by-reference/
This passes something by reference instead of value.
See:
http://php.net/manual/en/language.references.php
http://php.net/manual/en/language.references.pass.php
I used it for sending a variable to a function, and have the function change the variable around. After the function is done, I don't need to return the function to the return value and set the new value to my variable.
Example
function fixString(&$str) {
$str = "World";
}
$str = "Hello";
fixString($str);
echo $str; //Outputs World;
Code without the &
function fixString($str) {
$str = "World";
return $str;
}
$str = "Hello";
$str = fixString($str);
echo $str; //Outputs World;

Categories