$number = 1;
This is valid:
$number = ($number) + 1;
But this is invalid:
$number = ($number) ++;
So why I can use + 1 and increase it but I cannot use ++ to increase it?
$number = ($number) + 1;
This is valid because you add 1 to an expression.
++ as increment operator cannot be used for expressions it can be used only for variables.
From manual:
The increment/decrement operators only affect numbers and strings.
Increment operator ++ increments numbers or string variables. ($number) is not a variable but an expression.
For the same reason that these are valid:
isset($_GET['foo'])
$bar++;
$data = array(1, 5, 6);
sort($data);
... and these are not:
isset('hi');
'hi'++;
33++;
sort(array(1, 5, 6));
Some functions, operators and constructs operate on variables and don't make sense elsewhere. Parenthesis here are basically a red herring.
Related
I've seen comparison operators used straight after assigning values to variables in codes such as this:
($i = array_search($v, $b)) !== false // If $v is not in array, outputs false
Or something like this:
$n = 5 <= 5;
echo $n; // Outputs 1;
In the first example, does the comparison operator directly compare the value to array_search(...) or does it compare it to $i, since both of them are in brackets? Would it make a difference if there were no brackets around "$i = array_search(...)?
I've tried looking in the PHP manual on comparison operators, but it does not seem to mention using comparison operators in this way.
Also, in the second example, if there are no brackets, is the comparison operator comparing the value to 5 or to $n?
Could someone please link any documents or articles relating to the usage of comparison operators after assigning variables?
does the comparison operator directly compare the value to array_search(...) or does it compare it to $i
It assigns the value from array_search to $i first, and then evaluates a comparison to that value second.
In your example, array_search will return false on failure.
if( ($i = array_search($v, $b)) !== false ){}
Is totally equivalent to:
$i = array_search($v, $b);
if($i !== false){}
Or:
if( array_search($v, $b) !== false ){}
It's just a convenient shortcut to also assigning the value of $i for use later.
I have variables $var1, $var2, $var3. Each day they are being fetched from a MYSQL query and being displayed in a row in a table. Sometimes they are positive and sometimes there are negative values. I would like one column to show the sum of only the POSITIVE values. How do I write a php statement to add only the positive values?
Do I need to write 6 if statements or is there an easier way?
You can use PHP Ternary Operator to do it with 4 lines of code.
Simply, if variable is greater than zero, positive (add to sum), else negative (skip, no need to add).
Use ternary operator.
<?php
$sum = 0; // Initialise $sum
$sum += ($var1 > 0) ? $var1 : 0; // If $var1 is greater than 0, add to $sum.
$sum += ($var2 > 0) ? $var2 : 0; // Same as of $var1
$sum += ($var3 > 0) ? $var3 : 0; // Same as of $var1
?>
http://www.php.net/manual/en/language.operators.precedence.php#example-115
<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
?>
The example from the php manual doesn't explain very well. Why isn't $a++ evaluated to 2, and then added to 1, so that it always becomes echo 1 + 2 // equals 3? I don't understand how it "may print either 2 or 3". I thought incremental ++ has "higher precedence" than addition +?
In other words, I don't understand why isn't it...
$a = 1;
1) echo $a + $a++;
2) echo 1 + ($a = 1 + 1);
3) echo 1 + (2);
4) echo 3;
It can be either 2 or 3. However in most of the time it will be 3. So why it MIGHT be 2? Because PHP is not describing in which order expressions are evaluated, since it might depends on the PHP version.
Operator precedence in PHP is a mess, and it's liable to change between versions. For that reason, it's always best to use parentheses to group your in-line equations so that there is no ambiguity in their execution.
The example I usually give when asked this question is to ask in turn what the answer to this equation would be:
$a = 2;
$b = 4;
$c = 6;
$val = $a++ + ++$b - 0 - $c - -++$a;
echo $val;
:)
Depending where I run it now, I get anything between 4 and 7, or a parser error.
This will load $a (1) into memory, then load it into memory again and increment it (1 + 1), then it will add the two together, giving you 3:
$a = 1;
$val = $a + ($a++);
This, however, is a parser error:
$a = 1;
$val = ($a + $a)++;
Anyway, long story short, your example 2) is the way that most versions will interpret it unless you add parenthesis around ($a++) as in the example above, which will make it run the same way in all PHP versions that support the incrementation operator. :)
Order of evaluation isn't a precedence issue. It has nothing to do with operators. The problem also happens with function calls.
By the way, $a++ returns the old value of $a. In your example, $a++ evaluates to 1, not 2.
In the following example, PHP does not define which subexpression is evaluated first: $a or $a++.
$a = 1;
f($a, $a++); //either f(1,1) or f(2,1)
Precedence is about where you put in parentheses. Order of evaluation can't be changed by parentheses. To fix order of evaluation problems, you need to break the code up into multiple lines.
$a = 1;
$a0 = $a;
$a1 = $a++;
f($a0, $a1); //only f(1,1)
Order of evaluation only matters when your subexpressions can have side-effects on each other: the value of one subexpression can change if another subexpression is evaluated first.
I know that =& usually means "assign by reference", but what happens if we reverse these two characters, since I've seen this in plenty of PHP scripts?
$a &= $b is short for $a = $a & $b which is the bitwise-and operator.
It's the compound bitwise AND/assignment operator:
$x = 0x01;
$y = 0x11;
$y &= $x; // bitwise AND $y and $x, assign result back to $y
var_dump($y == 0x01); // true
&= is a compound assignment operator, whereas =& is actually two separate operators (= and &), pushed together. This is legal syntax because PHP doesn't demand whitespace between them.
&= performs a bitwise AND operation between the left hand side and right hand side, then assigns the result to the left hand side variable.
$x = 1;
$x &= 0; // $x === 0 now. A more verbose syntax would be "$x = $x & 0;"
On the other hand
=& should really be expanded to = & as the operators are seperate. This is known as assignment by reference. The = is your standard assignment operator, and the & when prefixed before a variable name returns the reference to the variable.
$y = "foobar";
$x = &$y; // $x now holds a reference to $y.
PHP's or is an weird keyword. Here it is in a code snippet that makes me confused:
echo 0 or 1; // prints 1
$foo = (0 or 1);
echo $foo; // prints 1
$foo = 0 or 1;
echo $foo; // prints 0 for some reason
Why does the last one print 0 and not 1?
This is because of different operator precedence. In the third case, the assignment is handled first. It will be interpreted like this:
($foo = 0) or 1;
The || operator has a different precedence. If you use
$foo = 0 ||1;
It will work as you expect.
See the manual on logical operators
No, I wouldn't, that's because of operator precedence:
$foo = 0 or 1;
// is same as
($foo = 0) or 1;
// because or has lower precedence than =
$foo = 0 || 1;
// is same as
$foo = (0 || 1);
// because || has higher precedence than =
// where is this useful? here:
$result = mysql_query() or die(mysql_error());
// displays error on failed mysql_query.
// I don't like it, but it's okay for debugging whilst development.
It's ($foo = 0) or 1;. or has a lower operator precedence than = .
You should use || in this case, since it has a higher precedence than =, and thus will evaluate as you'd expect.
IIRC, the assignment operator (=) has higher precedence than or. Thus, the last line would be interpreted as:
($foo = 0) or 1;
Which is a statement that assigns 0 to $foo, but returns 1. The fist statement is interpreted as:
echo(0 or 1);
An as such will print 1.
Order of operations. The word "or" has much lower precedence than the corresponding "||". Lower, even, than the assignment operator. So the assignment happens first, and the value of the assignment is the first operand to the "or".
"or" is meant more to be used for flow control than for logical operations. It lets you say something like
$x = get_something() or die("Couldn't do it!");
if get_something is coded to return false or 0 on failure.
In the first two snippets, you are comparing 0 or 1 (essentially true or false). In the third snippet you are assigning 0, which works, and thus is true, so therefore the or condition is not executed.emphasized text
In your third example, the = operator has a higher precedence than or, and thus gets done first. The || operator, superficially the same, has a higher precedence than =. As you say, interesting.