PHP: 演算子 - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<代数演算子>
view the version of this page
Last updated: Tue, 21 Dec 2004

第 15章演算子

演算子の優先順位

演算子の優先順位は、二つの式が"緊密に"結合している度合いを指定します。 例えば、式 1 + 5 * 3 の答えは 16になり、18とはなりません。 これは乗算演算子("*")は、加算演算子("+")より高い優先順位を有するか らです。必要に応じて強制的に優先順位を設定するために括弧を使用する ことが可能です。例えば、18と評価するためには、 (1 + 5) * 3 とします。

次の表に示すリストは優先順位が最低ものから並べた演算子の優先順位です。

表 15-1. 演算子の優先順位

結合時の評価演算子
,
or
xor
and
print
= += -= *= /= .= %= &= |= ^= ~= <<= >>=
? :
||
&&
|
^
&
結合しない== != === !==
結合しない< <= > >=
<< >>
+ - .
* / %
! ~ ++ -- (int) (double) (string) (array) (object) @
[
結合しないnew

注意: !=よりも優先されるはずなの にもかかわらず、PHPは依然としてif (!$a = foo()) のような式も許します。この場合はfoo()の出力が $aに代入されます。



add a note add a note User Contributed Notes
演算子
npeelman at cfl dot rr dot com
29-Dec-2004 11:22
Update to message by yasuo_ohgaki at hotmail dot com:

  I know this is an old message but when using an Associative array in a string you do not have to use { and  } to resolve ambiguity.

ex:

Associative Array in string:

$arr['item'] = 'string';

echo "This is {$arr['item']}"; //prints "This is string".

...does work but, so does:

echo "This is $arr[item]"; //prints "This is string".

... simply enclose the whole string with double quotes and leave out the single quotes from around the index name. This simplifies the code and makes things easier to read.
Stopping at the dot completely
01-Sep-2004 08:33
The low precedence of the OR operator is useful for error control statements such as this one:
$my_file = @file ('non_existent_file') or die ("Failed opening file: error was '$php_errormsg'");

Notice the good readability of the code.
22-Aug-2004 04:51
I think warhog's note about the differing precedence between && / AND and || / OR is worth repeating.  Since && and || evaluate before the assignment operator (=) while AND and OR evaluate after it, you can get COMPLETELY different results if you don't fully parenthesise.

I cannot imagine when it would ever be important that those two pairs have differing precedence, but they do.  And I just spent two hours discovering that the hard way because I broke my career-long rule:

*Always fully parenthesise!*
11-Jun-2004 02:22
Warhog wrote: "maybe usefull for some tricky coding and helpfull to prevent bugs :D"

I'm sure Warhog was being facetious, but for the new programmers in the audience I'd like to point out that 'tricky coding' and relying on precedence/order of evaluation are both well-known ways to *produce* bugs.

Use parentheses instead.
10-Jun-2004 12:58
of course this should be clear, but i think it has to be mentioned espacially:

AND is not the same like &&

for example:

<?php $a && $b || $c; ?>
is not the same like
<?php $a AND $b || $c; ?>

the first thing is
(a and b) or c

the second
a and (b or c)

'cause || has got a higher priority than and, but less than &&

of course, using always [ && and || ] or [ AND and OR ] would be okay, but than you should at least respect the following:

<?php $a = $b && $c; ?>
<?php $a
= $b AND $c; ?>

the first code will set $a to the result of the comparison $b with $c, both have to be true, while the second code line will set $a like $b and THAN - after that - compare the success of this with the value of $c

maybe usefull for some tricky coding and helpfull to prevent bugs :D

greetz, Warhog
yasuo_ohgaki at hotmail dot com
26-Mar-2001 08:34
About "{" and "}".
Sometimes PHP programmers need to use "{" and "}" to resolve ambiguity. Here is some examples.

Variable Variables:

$foo = "test";
$$bar = "this is";

echo "${$bar} $foo"; // prints "this is test"
Note: it is the same as
echo "$test $foo";

Array in string:

$arr[10][10][10] = "string";

echo "This is {$arr[10][10][10]}"; // prints "This is string"

Associative Array in string:

$arr['item'] = 'string';

echo "This is {$arr['item']}"; //prints "This is string".
yasuo_ohgaki at hotmail dot com
26-Mar-2001 07:53
Other Language books' operator precedence section usually include "(" and ")" - with exception of a Perl book that I have. (In PHP "{" and "}" should also be considered also). However, PHP Manual is not listed "(" and ")" in precedence list. It looks like "(" and ")" has higher precedence as it should be.

Note: If you write following code, you would need "()" to get expected value.

$bar = true;
$str = "TEST". ($bar ? 'true' : 'false') ."TEST";

Without "(" and ")" you will get only "true" in $str.
(PHP4.0.4pl1/Apache DSO/Linux, PHP4.0.5RC1/Apache DSO/W2K Server)
It's due to precedence, probably.
yasuo_ohgaki at hotmail dot com
09-Mar-2001 09:58
Pay additional attention that precedence is listed from LOWER to HIGHER.  Compiler language books list precedence opposite order. (At least, language books that I have, C/C++/Java. Perl book is the same order. I prefer precedence listed from higher to lower, not a big deal though)

<代数演算子>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page