function a(&$c, &$d){
$c = &$d;
}
$a = 1;
$b = 2;
a($a, $b);
echo $a;
output is 1,but shouldn't it be outputting 2 as $c is referencing $d. $c and $a reference to the same value,then $c refer to the value of $d which refer to $b so ultimately $a should refer to $b, isn't it correct?
For the operation you seek you must remove the ampersand reference used within the function a.
Example
function swap (&$one, &$two) {
$tmp = $one; // One is stored temporarily
$one = $two; // Two is stored in One
$two = $tmp; // Temporary data retrieved and stored in two
unset($tmp); // Temporary variable destroyed
}
// Set the variables
$a = 1;
$b = 2;
echo $a . " - " . $b . "<br />"; // See output as: 1 - 2
swap($a, $b);
echo $a . " - " . $b; // See output as: 2 - 1
in line
$c = &$d;
$a is referring to the address of $b but not to $b, then you can try this:
<?php
function foo(&$c, &$d){
$c = $d;
}
$a = 1;
$b = 2;
foo($a, $b);
echo $a;
?>
Related
Here is my situation, let's say i have two variables:
$a = "Peter";
$b = "tall";
Then, i can combine this by
$c = " $a is very $b ";
which give me the string : "Peter is very tall"
However, if i have another variable $e passed to my function, and the string is " $a is very $b "
then, i print out $e, it just show me " $a is very $b ";
What i expected is "Peter is very tall", because $e is equal to $c.
This is the whole logic flow:
$e = " $a is very $b ";
getMsg($e);
function getMsg($e){
$a = "Peter";
$b = "tall";
$c = " $a is very $b ";
echo $c //Peter is very tall
echo $e //$a is very $b
}
How can i achieve this function?
You could use sprintf sprintf documentation. The %s symbols are string tokens that get substituted in the format string (the 1st param to sprintf).
$e = "%s is very %s";
getMsg($e);
function getMsg($e){
$a = "Peter";
$b = "tall";
echo sprintf($e, $a, $b);
}
Or a less coupled version of the getMsg() function:
function getMsg($msg,$name,$is){
echo sprintf($msg, $name, $is);
}
getMsg("%s is very %s", "Peter", "tall");
Please try this code. I hope this is what you want.
<?php
// Your code here!
$a = "Peter";
$b = "tall";
$e = $a."is very".$b;
getMsg($e,$a,$b);
function getMsg($e,$a,$b)
{
$c = $a."is very".$b;
echo $c;
echo $e;
}
?>
Try this. It should work fine. You need to Define $a and $b outside the function.
function getString($a,$b,$e){
$c = " $a is very $b ";
echo $c;
echo '<br/>'.$e;
}
$a = "Peter";
$b = "tall";
$e = " $a is very $b ";
$response = getString($a,$b,$e);
OUTPUT :
Peter is very tall
Peter is very tall
this is a very simple program, but when i run it, it just out put the first echo statement, which is 3, others are not displaying. i declared it global in the function, why it dose not work, strangely, when i was coding another program declaring a global array variable in a function it works perfectly, please explain it in detail, thanks
$a = 1;
$b = 2;
$c = $a + $b;
echo $c ."<br>";
function aaa()
{
global $a;
global $b;
$d = $a + $b;
echo $a ."<br>";
function ccc()
{
global $d;
$e = $c + 1;
echo $e;
}
}
The problem with your code is that you've put the function ccc inside the funciton aaa and that is not the correct way of doing what you're trying to do.
The correct way would be to create a Class aaa and then declaring ccc as its method.
$a = 1;
$b = 2;
$c = $a + $b;
$d = 0;
function aaa() {
global $a;
global $b;
global $d;
$d = $a + $b;
echo $a ."<br>";
}
function ccc() {
global $c;
global $d;
$e = $c + 1;
echo $e;
}
echo $c ."<br>";
aaa();
ccc();
I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;
Let's say I have 3 variables
$a;
$b;
$c = 30;
What I wish to do here is that I have to divide $c and put it in the first 2 variables which can be easily done by doing.
$a = $c / 2;
$b = $c / 2;
However what if in $b there is a maximum value limit of 10 and $a is limitless.
In this case the values have to be.
$a = 20;
$b = 10;
What would be the best solution for this?
If B has a limit, then you would need to calculate B first. Then work out the difference between C & B and set A as the answer.
<?php
$a;
$b;
$c = 100;
$b = min(10, $c / 2);
$a = ($c - $b);
echo "A: " . $a;
echo "B: " . $b;
?>
Try using min
$a = $c / 2;
$b = min(10, $c / 2);
I am trying to figure out the difference between $a=&$b and $a=$b.
I know & make the variable to be a reference variable. But the following test gave me the same result. Can anyone explain the difference? Thanks.
$a=5;
$b=6;
$a=&$b;
echo $a; //6
$a=5;
$b=6;
$a=$b;
echo $a; //6
First of all: you'll hardly ever need references, avoid the confusion of using them if you can.
$a=5; //assign value to a
$b=&$a; //make $b a reference to $a
$b=6; //assigning a value to $b assigns the same value to $a (as they point to the same location
echo $a; //6
$a=5; //assign a value to a
$b=$a; //set $b to the value of $a
$b=6; //set $b to another value leaves $a at it's original value
echo $a; //5
It matters more in a function when you send it in as a parameter.
For example:
<?php
function changeVariableWithReference(&$var)
{
$var += 1;
}
function changeVariableWithoutReference($var)
{
$var += 1;
}
$a = 5;
$b = 5;
changeVariableWithReference($a);
changeVariableWithoutReference($b);
print $a . ' ' . $b;
?>
The difference between $a = $b and $a =& $b is that with the former assignment operator the value is copied while with the latter reference operator the variable $a refers to the same value as $b.
You don’t see any difference when reading the value but you see it when writing the value:
$var = 123;
$copyOfVar = $var;
$referenceToVar =& $var;
$copyOfVar = 456;
echo 'var='.$var.'; copyOfVar='.$copyOfVar;
// "var=123; copyOfVar=456"
$referenceToVar = 456;
echo 'var='.$var.'; referenceToVar='.$referenceToVar;
// "var=456; referenceToVar=456"
When you use the & in an assignment, you can think of the new variable being a 'short-cut' to the original. If you don't use the &, it will be a 'copy' of the original.
$a = 5;
$b =& $a; // this $b is like a shortcut to $a. Change $b and $a will change too
$a = 5;
$b = $a; // this time, $b will be 5 - but not a shortcut to $a. Change $b and $a will still be 5.
A reference can be easily explained by simple graph. When you use copy by value ($a = $b) then something like that happens:
$a = 1000;
# $a -----> 1000
$b = $a;
# $a -----> 1000
# $b -----> 1000
# (two "pieces of memory" has been used)
But when you create a new reference to $a named $b then something like that happens:
$a = 1000;
$b =& $a;
# $a --\
# --> 1000
# $b --/
# (one "piece of memory" has been used but two different names ($a, $b) point on it)
You will get the ans, if you follow the following code (i have add some lines)
$a=5;
$b=6;
$a=&$b;
echo $a; //6
$b = 8;
echo $a; //8
$a=5;
$b=6;
$a=$b;
echo $a; //6
$b = 20;
echo $a; //6
with & simple, variable $a points the variable $b
without &, $b just copy into $a