If $a = 5, $b = 'a', what is the value of $$b? - php

Explain this interview question to me:
Q: If the variable $a is equal to 5 and variable $b is equal to character a, what’s the value of $$b?
A: 5, it’s a reference to existing variable.

That's a variable variable. PHP will look up the variable with the name stored in the string $b. So if $b == 'a' then $$b == $a.
It's a lot like pointers in C, except they use variable name strings instead of memory addresses to point to each other. And you can dereference as many times as you want:
$a = 5;
foreach (range('b', 'z') as $L) {
$$L = chr(ord($L) - 1);
}
echo $$$$$$$$$$$$$$$$$$$$$$$$$$z;
Output:
5

-95 is the answer as if u will echo $b u will get output as
"a"
and if u echo $a u will get out but as "5"
hence in this sense when u $(echo $b) which same as $(a) hence u will get it as "5-100" which is "-95"

$$b - 100
= $a - 100 // substituting $b=a
= 5 - 100
= -95

I don't know if the '?' is erroneous in the statement '$$b? - 100' but I don't think that will compile.
However:
$a = 5
$b = 'a';
$c = $$b - 100;
$c will equal -95, because $$b is a variable variable reference and given that $a = 5 it resolves to $a (5) - 100, or -95.

the answer is -95
$a - 100

The following is a good reference on PHP variables
http://php.net/manual/en/language.variables.variable.php

Related

Php variable references

Why in this code is $a equal to 21? I am giving to $b the value of $a by reference, why is $a changing as well??
$a = '1';
$b = &$a;
$b = "2$b";
echo $a.", ".$b;
Notice that you define the variables with single or double quotes (both are ok)
That's how Php knows you mean a string
Regarding the reference - the meaning of reference, is that $b points to $a, so its not really a variable
and lastly, this: $b = "2$b"; is basically string concatanation
Here's a simple explanation
$a = '1';
$b = &$a; // Sets $b to a reference to $a
echo $b."<br>"; // $b value is still one
$b = "2$b"; // here u write "2 and $b = 1" which means b = 21 and also Sets $a to 21
echo $a.", ".$b;
So your output is 21,21 hope you understand

PHP check | (OR) operator in variable assignment

I want to check two variables a and b and assign both the variable to new variable "c" and want to get the OR result from both the variables. for example if a=1 and b=0, c must be 1, while if a=0 and b=1, c must be 1, if a=0 and b=0 then c=0, for this purpose i am using the following | operator, which returns the required result, but i am not sure if i am doing it correct or not
<?php
$a = 0;
$b = 1;
$c = $a | $b;
echo("Value in $c = ".$c);
?>
EDIT: i have gone through the PHP.NET website and find that:
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
Reference: http://php.net/manual/en/language.operators.bitwise.php
Assuming you only have the states you have in your question, you can use a ternary to do this. It might help others understand what you're doing in the future
$c = ($a || $b) ? 1 : 0;
There's nothing wrong with the way you did it in your question, tho.

php syntax $a = $b = 1;

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.

Confusion over the value of a variable after running a PHP program

I'm studying for my finals and I came across this question:
consider this following PHP code, write the output after executing it
<?php
$a=3;
$b=$a++;
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
When I output it in my text editor I get a < 3, but I don't understand why though?
I thought a is assigned to 3 and also b is assigned to a++ 3 and 3==3 so should a==3 be printed out?
No, you are using post-increment operator on $a. So, $b will be assigned a value of 3, and later, when the statement is executed, $a will increment itself by one, and become 4. So, you'll now be comparing $a as 4 and $b as 3.
Hence you get the result a > 3
The $a++ incrementation happens after the expression gets evaluated, while ++$a would happen before.
So in your case, $b was first set to 3, and then $a was increased.
$a++ tells the variable $a explicitely to increase, no matter if you assigning to another variable or not!
This gives the possibility to do things like if ($a++ > 10) { // ... in loops.
For your case you would have to take $b = $a + 1;
<?php
$a=3;
$b=$a++;
// $b = 3 and $a = 4 now
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
I tested your code and I get:
a>3
which makes sense
$a is 3 but is increased to 4 when you do $a++
$b is just $a before the ++ action so it stays 3
Think of $a++ as $a = $a + 1 then it makes sense

If the variable $a is equal to 5 and variable $b is equal to character a, what’s the value of $$b?

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'

Categories