Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why decremented of NULL not negative in this array?

Tags:

php

I have tried this code

$a = array_fill(0, 4, NULL); $a[0]++; ++$a[1]; $a[2]--; --$a[3]; var_dump($a); 

Result:

array(4) {     [0]=> int(1)     [1]=> int(1)     [2]=> NULL     [3]=> NULL } 

Why value of 2 and 3 index is not negative?

like image 530
mrsrinivas Avatar asked Jul 11 '13 11:07

mrsrinivas


People also ask

Is ++ OK in JavaScript?

The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces.

How do you decrement in JavaScript?

JavaScript Arithmetic (Math) Decrementing (--)The decrement operator ( -- ) decrements numbers by one. If used as a postfix to n , the operator returns the current n and then assigns the decremented the value. If used as a prefix to n , the operator assigns the decremented n and then returns the changed value.

How does decrement operator work?

2) Decrement Operators: The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, the value is first decremented and then used inside the expression. Whereas in the Post-Decrement, the value is first used inside the expression and then decremented.

What is ++ and -- in JavaScript?

The increment operator ( ++ ) increments its operand by 1 ; that is, it adds 1 to the existing value. There's a corresponding decrement operator ( -- ) that decrements a variable's value by 1 . That is, it subtracts 1 from the value.


1 Answers

Weird, but documented on the Incrementing/Decrementing Operators php doc page:

Note: The increment/decrement operators do not affect boolean values. Decrementing NULL values has no effect too, but incrementing them results in 1.

like image 196
fvu Avatar answered Oct 14 '22 07:10

fvu