How to swap the values of two variable without using third variable. I tried doing it with array but it doesn't work. Does anyone have a quick solution
You can try like this
This will work for any variable type
$a = 10;
$b = 15;
list($a, $b) = array($b, $a);
print $a . ',' . $b;
This below method will work only for numbers not for strings
$a = $a + $b; // 10 + 15 = 25
$b = $a - $b; // 25 - 15 = 10
$a = $a - $b; // 25 - 10 = 15
print $a . ',' . $b;
Related
Can someone explain to me in a simple language why $a = 21 in the final output?
$a = '1';
echo $a . "<br>"; // result 1
$b = &$a;
echo $b . "<br>"; // result 1
$b = "2$b";
echo $b . "<br>"; // result 21
echo $a . "<br>"; // result 21 WHY?
echo $a . ", " . $b; // result 21, 21
Thank you. I appreciate the help very much.
It's because when you do
$b = "2$b";
it means "Set the value of $b to the string "2" followed by whatever the current value of $b is.
Earlier you put
$b = &$a;
This means "create a new reference for $a and call it $b", or in other words make $b point at the same thing in memory that $a is pointing at.
When you update the value of $b you're really updating the value that's stored in the memory block that both $a and $b point at, so once you've set $b to a particular value $a will be the same value because they both reference the same thing.
Is
$a = 1;
$b = $a;
equal to writing this?
$a = $b = 1;
Will the second example always put 1 as value to both $a and $b, even if $a and $b already has a value assigned to them?
Quoting the documentation:
The value of an assignment expression is the value assigned. That is,
the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
So, to answer your question, the result of the assignment $b = 1 is 1, and therefore, $a = $b = 1 would assign the value of $b = 1--which is to say 1--to $a.
That being said, abusing this can lead to code that is hard to read.
Yes, PHP will put 1 in $b then put $b value in $a, i.e. 1.
There is no ambiguity as the first assignment is $b = 1, the next is $a = $b.
I'm trying to understand passing by reference. This is probably a bad analogy, but is it like Newton's 3rd law (action-reaction pairs)?
For example, for the following code
$a = 4;
$b = 2;
$n = 42;
$a = &$b;
is
$a=$n the same as $b=$n? Isn't the value of $a and $b stored in the same address?
If you assign these variables normally:
$a = 1;
$b = 2;
$c = 3;
Then the associations will look like this:
a --> 1
b --> 2
c --> 3
Now, if you make $c into a reference of $a, like this:
$a = 1;
$b = 2;
$c = &$a;
Then the associations will look like this:
a --> 1 <--.
b --> 2 |
c --------/
In other words, $a and $c point to the same value. Because they both point to the same value we can change either of the variables and they will both point to the new value.
$a = 5;
echo "$a $c"; // Output: "5 5"
$c = 10;
echo "$a $c"; // Output: "10 10"
Yes, After assigning $n to $a, $b will point $n.
This is because after executing $a=&$b both $a and $b references a same memory location and become reference variable (is_ref=1). And the reference count (refcount) of that specific memory location increases by 1. Now whatever value you assign to any of those references both will point to same value.
Executing $a=$n means value of $n will be store to the location referenced by $a. And this is same location as $b.
See the example here.
$a, $b, $n are pointing different locations
php > $a = 4;
php > $b = 2;
php > xdebug_debug_zval('a'); // they are pointing different location
a: (refcount=1, is_ref=0)=int(4)
php > xdebug_debug_zval('b'); // they are pointing different location
b: (refcount=1, is_ref=0)=int(2)
php > $n = 42;
php > xdebug_debug_zval('n');
n: (refcount=1, is_ref=0)=int(42)
$a and $b both becomes a reference now
php > $a = &$b;
php > xdebug_debug_zval('b');
b: (refcount=2, is_ref=1)=int(2)
php > xdebug_debug_zval('a'); // a too
a: (refcount=2, is_ref=1)=int(2)
Assigning new value, NOT references to any of $a and $b
php > $a = $n;
php > xdebug_debug_zval('a'); // a holds $n's value '42' now
a: (refcount=2, is_ref=1)=int(42)
php > xdebug_debug_zval('b'); // same for b
b: (refcount=2, is_ref=1)=int(42)
Usually when you create a variable $a and $b, each has a unique memory address where their data is stored.
However, if you tell the interpreter that $a = &$b, that means that $a and $b now have the same memory address. If you assign anything to $a, or to $b, they will both echo the same value, because they store data in the same place in memory.
If you want to experiment, I suggest starting up the PHP interactive interpreter from the command line:
php -a
php > $a = 1;
php > $b = 2;
php > echo $a . ' ' . $b;
1 2
php > $a = &$b;
php > echo $a . ' ' . $b;
2 2
php > $a = 1;
php > echo $a . ' ' . $b;
1 1
php > $b = 2;
php > echo $a . ' ' . $b;
2 2
$a = $b & $b = $n has the same value but are different because the value is a primitive type and primitive types pass by value.
The most quick way to test if the code is using references or not is change the value of the source var and see if the target vars change his value or not.
$n = 1;
$a = $n;
$b = $n;
echo $a; // 1
echo $b; // 1
echo $n; // 1
$n = 2;
echo $a; // 1
echo $b; // 1
echo $n; // 2
However objects always are passed by reference
$n = new Object(1);
$a = $n;
$b = $n;
$n->newValue(5);
$a->printValue(); // 5
$b->printValue(); // 5
$a->newValue(7);
$b->printValue(); // 7
I wonder if there is any way can short sentence to state a variable.
Purpose: only for if you are in a situation have to state 20 variables at the time. more convenience
$a = 1; $b = 2;
//Imagination like below
$a, $b = 1, 2;
$a = 1, $b = 2;
Thank you very much for your advice of alternatives.
(If you do not have any ideas, please do not accuse the way of why have to think about this), because arrray, (object) are alternatives, but not match what I need on my question
The closest you can get to that syntax is using list():
<?php
list ($a, $b) = array(1, 2);
echo $a . ' ' . $b; // prints "1 2"
Its magic!
EDIT:
For even more magic, you can use short array notation from PHP 5.4 onwards:
<?php
list($a, $b) = [1, 2];
print $a . ', ' . $b; // prints 1, 2
How is $a = 1, $b = 2; shorter than $a = 1; $b = 2; ?
If for any reason you need two assignments in one statement, you could to it with list:
list($a,$b) = array(1,2);
However, shortening is not a valid reason for this.
Answer: 100, it’s a reference to existing variable.
But I don't understand why the result is 100? who can explain this to me?
Thanks a lot!!!
Not sure what exactly you're asking, but when I run this code:
<?php
$a = 5;
$b = 'a';
echo $$b;
?>
I get output of:
5
This code gives me "5".
$a = 5;
$b = "a";
echo ($$b);
I think you may have a problem with your code/logic?
$a=5;
$b=a;
echo $$b;
Output: 5
$a=5;
$b='a';
echo $$b;
Output: 5
$a=5;
$b="a";
echo $$b;
Output: 5
$$ is a variable variable because all of the above are looking for a variable a they will all assume $a - unless you have another reference to different variable somewhere in your code which is 100.
Of course it should be 5 !!
$a = 5 ;
$b = 'a' ;
$$b = $( $b ) = $ ( 'a' ) = $a = 5 ;
If $b was ever declared to be a reference to another variable elsewhere in your code, then varaible variables won't work as expected.
<?php
$a = 5;
$b = 'a';
echo $$b, "\n"; // echoes 5 as expected
$b = &$a;
$b = 'a';
echo $$b, "\n"; // echoes 'a'