I was Do some Testing on Arrays , but I saw something in my code :
$arr = array();
$arr[0]++;
echo $arr[0];
output = 1 ;
Why is index[0] value is 1 ?
From my code above I don't do an assignment like
$arr[0] = 1 ;
I think this is due to loose types in PHP.
null == false == 0
this means that $arr[0] (null before the ++) is loosely equal to 0. So null (or 0) + 1 = 1.
Because it interprets as $arr[0] = $arr[0] + 1 ;. If you try with var_dump($arr[0]), then you will see that var_dump($arr[0]); returns NULL so NULL + 1 is equal to 1(converts NULL to 0 internally) that's why it returns 1 at the end.
$arr = array();
$arr[0] = $arr[0] + 1 ;
echo $arr[0];
Also you should see a Notice like
Notice: Undefined offset: 0
$arr[0]++;
That expression is execured as:
$arr[0] = $arr[0] + 1;
but your array doesn't contain an element with zero index. That element isn't instantiated and that value is null. That expression can be written as:
$arr[0] = null + 1;
null value converted to integer and have 0 value automaticaly and the expression is executed as
$arr[0] = 0 + 1;
Not that it really matters, because the effect is the same, but there is no type juggling before the increment operation. Incrementing an undefined value directly results in 1. It is not converted to zero and then incremented.
The PHP manual explains this behavior. First, null:
The special NULL value represents a variable with no value. NULL is the only possible value of type null.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
Next, Incrementing/Decrementing operators:
Note: The increment/decrement operators only affect numbers and strings. Arrays, objects, booleans and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.
So, $arr[0] is null because it has not been set to any value yet.
And incrementing null results in 1.
Use 1 of 3 examples:
<?php
$arr = array();
for ($i=0; $i < sizeof($arr)+1; $i++){
$arr[$i] = $i+1;
}
echo $arr[0]; //1
?>
<?php
$arr = array('0');
$arr[0]++;
echo $arr[0]; //1
?>
<?php
$arr[0] = 0;
echo $arr[0]++; //1
?>
Related
a friend asked me to analyze the output of the following simple lines.
I defined a variable x that has the string PHP, and a table called a which is a reference to x, so any changes to one of them will affect the other. First, I assigned the value "Mysql" to the first element of a, the value of x changed too.
But in the second affectation, when the second element of the table a got the value "test", the value of the variable x didn't change, any explanation?
<?php
$x = "PHP";
$a[] = &$x;
$a[0] = "MySql";
echo $a[0]; // Output: MySql
echo $x; // Output: MySql
$a[1] = "test";
echo $a[1]; // Output: test
echo $x; // Output: MySql
// why last output is mySql and not test?
?>
The reference to $x is never in $a[1]. It is only in $a[0] and therefore won't change when you assign 'test' to $a[1].
$a[] = &$x; is just adding &$x to the end of the array $a. $a at that point in your code is empty, so you are assigning &$x to the first index of $a ($a[0])
<?php
$a = NULL;
$a++;
echo "a value is $a";
?>
It outputs:
a value is 1.
<?php
$a = NULL;
echo "a values is $a";
?>
It outputs:
a value is
Am confused about this.. please explain me
It is PHP's Type Casting
http://php.net/manual/en/language.types.type-juggling.php
PHP automatically changes type of variable depending upon the operation.
Explanation:
Your code
<?php
$a = NULL; // $a is NULL
$a++;
?>
But, increment ++ is applicable only to integer values, so when you write $a++, it automatically converts $a to integer and as it is NULL, it is set to 0 and then incremented.
For -
<?php
$a = NULL;
$a++;
echo "a value is $a";
?>
When this operation is performed $a first converted to integer and gets the value 0 in it as it was containing NULL. So it is printing 1 as value.
For -
<?php
$a = NULL;
echo "a values is $a";
?>
No conversions are applied here as it is printed as it is. So it is printing nothing there.
I have acrossed some really weird behaviour in PHP statment when having example below:
Logically it shouldn't return 1 in this context. Why this is happening ? Just was wondering.
$test = 0;
var_dump($test); // gives int 0
$test = ($test == 'test') ? 1 : 0;
var_dump($test); //gives int 1
This is because of type juggling. 'test' is equal to 0 because (int)'test' actually is 0. Thus, your condition is true and 1 is the result.
In your particular case you may want to know how PHP converts strings to numbers.
Just try with === to compare also type of values:
$test = ($test === 'test') ? 1 : 0;
What would this evaluate to? I know it looks funny but I was looking at a practice exam and saw this:
if (number = 1) { echo "C1 is true"; }
Whenever you are assigning variables it always returns true when the assigned variable is not causing false.So it will go to the if and echo the output.And consider that it mainly depends on the value that you are assigning.
Suppose if you do like
if (number = 0) { // if(number = false)
echo "C1 is true";
} else {
echo "C1 is false";
}
It will prints C1 is false.Bec it will indirectly indicate like
if(0) // if(false)
which is a false.
The assignment operator = returns the assigned value. What does that mean? For example, the + operator in 1 + 2 returns the sum of two numbers; the value of the expression 1 + 2 is 3. In the same vein, the value of the expression number = 1 is 1. That's why this works:
a = b = c = 1;
So you're assigning 1 to number, the resulting value of which is 1, which is evaluated by if, which equals true.
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