|
|
Hoofdstuk 11. Operators
Operator Precedence
The precedence of an operator specifies how "tightly" it binds two
expressions together. For example, in the expression 1 +
5 * 3, the answer is 16 and not
18 because the multiplication ("*") operator
has a higher precedence than the addition ("+") operator.
Parentheses may be used to force precedence, if necessary. For
instance: (1 + 5) * 3 evaluates to
18.
The following table lists the precedence of operators with the
lowest-precedence operators listed first.
Tabel 11-1. Operator Precedence Associativity | Operators |
---|
left | , | left | or | left | xor | left | and | right | print | left |
= += -= *= /= .= %= &= |= ^= ~= <<= >>=
| left | ? : | left | || | left | && | left | | | left | ^ | left | & | non-associative | == != === !== | non-associative | < <= > >= | left | << >> | left | + - . | left | * / % | right | ! ~ ++ -- (int) (float) (string) (array) (object) @ | right | [ | non-associative | new |
User Contributed Notes Operators |
|
stanis at sibfair dot nsk dot su
28-Mar-2000 07:47 |
|
In case you are just beginning to code in PHP and this is your first
experience in programming, the Modulus (%) is a great way to code photo
albums based on a table (it returns the remainder of integer divided by
integer). The modulus helps to wrap data to next row based on the result
of '$currentrow(from db)' % 'number of columns in database'. The only
thing to look out for is that 1 % 4 is 1, 2 % 4 is 2, etc. and not zero or
some other number, like you might think at first. Hope this will help some
newbie programmers like myself.
|
|
kennon at audiogalaxy dot com
15-Aug-2000 01:41 |
|
You can also use the floor() and ceil() functions to give the nearest
integer values of a double. So, for instance, to get the integer part of
the quotient, you can do this:
floor( $a / $b );
|
|
kees at tweakers dot net
04-Jan-2001 02:54 |
|
Normally, a negative value modulo a positive number generates a negaive
number, if you don't want that (for example, the number has to be between
0 and 9 and not negative) then you can use a function like
this:
function mod($a,$b)
{
if ($a <= 0) return (int)
mod($b-abs($a), $b); else return (int) $a % $b;
}
for example:
-2 % 10 = -2
but mod(-2, 10) will return 8.
|
|
bdiaz at aullox dot com
25-Jan-2001 02:57 |
|
Yet another IWBNI posting...
I used the caret ( ^ )
"operator" in a truncate() function of mine with a result
similar to a substraction. So, I had to implement an even weirder function
which would work better. Should anyone know of a better way to do this, I
thank in advance.
//The function expects a double $num and an int
$pos indicating the decimal
//positions to allow
function
truncate($num,$pos){
$pos=(int)$pos;
$m="1e$pos"; //this
is our divider, ten at $pos power
$t=(double)$num*(double)$m; //just
to be sure
$t=(int)$t; //yes, we need to cut superfluous
decimals
$t=(double)$t/(double)$m;
return $t;
}
|
|
yasuo_ohgaki at hotmail dot com
09-Mar-2001 08:38 |
|
There are some operator descriptions missing. (I'm not sure what is
considered as operator in PHP, but anyway)
::
Scope
resolution operator for class
->
Member selection operator
for class
new
Object creation operator
?:
It is
described in Comparison Operator Section. I think it worth to make a new
sub section for ternary operator.
[]
I think Array
"[]" is considered as operator. It mentioned in Operator
Precedence Section as [, but no description. (Or is there any other use
for single "["?)
()
Grouping expressions. It might
not considered as operator in PHP, but it is better to be included at
least in Operator Precedence section.
{}
Resolve ambiguous
reference to variables. For example, {} is needed to resolve
$array1['array_stored_as_array_element']['value']. {} is also needed to
resolve ambiguity such as echo " $array_3d[1][2][3] "
It
might not considered as operator in PHP, but it is better to be included
at least in Operator Precedence section.
&
Reference
operator. There is a section for reference, but it is better to be
explained briefly in this section and direct reader to Reference
section.
(type)
Cast operator like (int)$v to get integer
value. I suppose these are operators also. Mentioned in Precedence
section, but no description.
+= -= *= /= .= %= &= |= ^= ~=
<<= >>=
Some assignment operators that are supported are
not described. Mentioned in Precedence section,
though.
print
Looks like "print" is considered as
operator in PHP. (I thought "echo" is) It mentioned in
Precedence section, but no description. Description for these may be
helpful for programmers to distinguish what is "expression" and
what is not.
For some reason, PHP4.0.4pl1 dose not allow. (I
posted as bug, I got reply "echo" is not a valid expression.
Therefore, PHP complained for following code as a parse
error)
($val) ? echo('true') : echo('false');
but PHP
does ALLOW
($val) ? print('true') : print('false');
($val) ?
include('true.inc') : include('false.inc');
I'm confused with
these. i.e. What is a expression, what is not an expression.
If
there are other operators like "print" (maybe echo?), all of
them should be described or mentioned, at least, in this section. (I think
it would be nice to have a section describing what is statements/language
construct)
It is not about operator, but it would be nice to have
BNF of PHP (Zend Script Engine) as a reference.
|
|
yasuo_ohgaki at hotmail dot com
09-Mar-2001 10:25 |
|
For type casting is described at
(I
suppose cast is operator, isn't it?)
|
|
jeroen at php dot net
31-Jul-2001 09:38 |
|
There is no power-operator. Use pow() instead.
See:
There
might come a power-operator in a future release, but that is still being
discussed
|
|
mahmoud.at.infoscience.it
12-Dec-2001 04:24 |
|
You can use the shorthand:
$a = 5;
$a += 10; // $a is now
15
$a -= 3; // $a is now 12
|
|
php.at.michrev.com
08-Mar-2002 09:42 |
|
If this doesn't work: ($val) ? echo('true') : echo('false'); ...you
should use this: if($val) { echo('true'); } else { echo('false');
} ...or, better yet, this: echo($val ? 'true' :
'false'); ...because $val, 'true', and 'false' are all expressions, but
echo() is not.
The "expr?expr:expr" operator is used to
select between expressions in the middle of a command. If you need to
select between different commands instead, it's probably better to use
if() instead. That's what it's there for.
|
|
ronm1958 at adelphia dot net
15-Apr-2002 04:59 |
|
The PHP modulus function will (interestingly) not return the decimal
portion of the result. I wrote a little function that will do so, and
mimics the results obtained by using the Windows calculator mod
function:
function modphp($x, $y, $z) {
for ($y = 0; $y
<= abs($z); $y++) { $w = abs($z) - (abs($x) * $y); if ($w
< abs($x)) { break; } }
if ($z < 0)
{ return(-$w); } else
{ return($w); }
}
Here's how to pull the answer out
of the mod function above:
$answer = modphp($var_x, $var_y,
$var_z);
|
|
msopacua at idg dot nl
09-Jun-2002 12:56 |
|
Remember that when you're reading from a socket, all output is of type
string.
Use ord() on each 'character', to get the byte values and
use bitwise arithmetics.
|
|
18-Jun-2002 11:21 |
|
The =& operator can be found in section 14 -- returning references.
|
|
zontar at mindless dot com
04-Aug-2002 11:53 |
|
It appears that the shortcut logical operators &&= and ||= are not
supported. Drag. :(
|
|
28-Aug-2002 09:43 |
|
It should be noted that PHP's ?: operator associates left to right. In C
and in C++, the ?: operator associates right to left. Thus:
1 ? 2
: 3 ? 4 : 5;
has the value 4 in PHP and the value 2 in C and in
C++. I find it curious that they would reverse the associativity of an
operator from its "accepted" sense, but yet they didn't fix the
precedence of the bitwise operators. Thus:
if(A & 7 ==
5)
groups like:
if(A & (7 == 5))
It is a trap for
the unlucky.
|
|
|
| |