Why php takes by default NULL as zero - php

<?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.

Related

PHP, assigning a value to a variable by reference

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])

Increase Value of Array Index?

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
?>

How can I put the result of the data in variable to the other variable?

<?php
$a = "time(0)";
$b = eval($a);
Like this, I want to put the result of the data....
I can put the string with eval code.. But how can I do this?
1) your string must contain completed php code, ie has ; at the end
2) code in the string doesn't produce any output and it results in NULL value of $b
So, to make it working, write:
$a = "time(0)";
$b = eval("echo ".$a.";");
echo $b; // 1433582861
I think this will help you,
$a = "time(0)";
$b = call_user_func($a);
$c = new DateTime($b);
var_dump($c);

php variable $a contains variable $b in it update $b and $a should automatically update

I have two variables in PHP, say $a and $b. $a is a string variable. It contains $b. I want to update $a automatically if $b is updated.
$b = 4;
$a = "value is ".$b;
echo $a; // value is 4
$b = 5;
echo $a; // should print value is 5
Yes, $a can be updated automatically if you assign $b to $a by reference, but there should not be any string concatenation assigned to $a.
Try:
$b = 4;
$a = &$b;
$c = 'Value is ';
echo $c.$a;
$b = 5;
echo $c.$a;
Here is a demo
Not possible the way you want it. You see, variables can be passed by reference, like so:
$a = &$b;
Which will cause $a to automatically update when $b changes, however, it may not contain any other value, (like the string you want), so you'll have to use a function or another variable to do it.
$b = &$a;
echo "Value is $b";
or
$b = &$a;
$description = "Value is ";
echo $description . $b;
PHP doesn't have that feature. Related features you could use are:
References, which let you alias one variable to another. The value of each variable is the same, since they're simply symbol table aliases.
$b = "I'm b."
$a =& $b;
echo $a;
Variable variables, in which one variable holds the name of the other.
$b = "I'm b."
$a = 'b';
echo $$a;
However, variable variables should generally be avoided as they generally cause needless obfuscation.
Functions (as mithunsatheesh suggests). This is closest to what you want, as a function call is an expression that will have the value you're looking for. The only place a function wouldn't work where a variable would is when interpolating the value into a double-quoted string or a heredoc. Instead, you'd have to use string concatenation, or assign the result of the function call to a local variable and interpolate that.
You should pass it by reference. How to do it ?
Make a function:
function showValue(&$b)
{
return 'value is ' . $b;
}
echo showValue($b);
I think this should work.
Take a look at http://www.php.net/manual/en/language.references.whatdo.php
$a = 4;
$b =& $a;
$a = 5;
echo $b; // should print 5;
When a php script runs it runs "line after line". When you assign like this
$b = 4;
$a = "value is ".$b;
Value of $b is already assigned to $a as a integer 4 (not $b). So, if next $b is updated to some other value. Variable $a has no idea about it.
In this kind of case you have to use function or variable reference as describe in some other answers
$a = 4;
$b =& $a;
$a = 5;
echo $b;

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