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

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

Related

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;

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.

PHP callback using a reference [duplicate]

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";

Name a variable after the content of a variable [duplicate]

This question already has answers here:
PHP variable variables
(6 answers)
Closed 8 years ago.
I'm trying to create a variable and name it after the content of another variable.
For example
$newname = "x";
//dosomething to create the variable $x
They are called variable-variables:
${$newname} = 'stuff';
If you have more, consider using an array and extract variables from it:
$vars = array(...); // keys = variable name, value = variable value
extract($vars, EXTR_SKIP);
Check this :
$a = 'hello';
$$a = 'world';
echo "$a ${$a}";
echo "$a $world";
Hope to be useful.

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