Операторы
Содержание
Оператором называется то, что принимает одно или больше значений (или выражений,
если говорить на жаргоне программирования), и вычисляет новое
значение; поэтому, всю конструкцию рассматривают как выражение.
Операторы группируются по количеству принимаемых значений. Унарные
операторы принимают только одно значение, например, !
(оператор логического отрицания)
или ++
(инкремент).
Бинарные операторы принимают два значения; это, например, знакомые
каждому арифметические операторы
+
(плюс) и -
(минус), бо́льшая часть поддерживаемых
в PHP операторов входит в эту категорию. И на последок, в языке предусмотрели только один
тернарный оператор,
? :
, который принимает три значения, и его часто называют просто — «тернарный
оператор»; хотя, возможно, точнее было бы назвать его «условный оператор».
Полный список PHP-операторов перечисляет раздел
«Приоритет оператора».
Раздел также описывает порядок выполнения и ассоциативность операторов. Порядок и ассоциативность
точно определяют, как вычисляются выражения с несколькими разными операторами.
Anonymous ¶21 years ago
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 cthe seconda 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 $cmaybe usefull for some tricky coding and helpfull to prevent bugs :Dgreetz, Warhog
anisgazig at gmail dot com ¶5 years ago
Operator are used to perform operation.Operator are mainly divided by three groups.1.Uniary Operators that takes one values2.Binary Operators that takes two values3.ternary operators that takes three valuesOperator are mainly divided by three groups that are totally seventeen types.1.Arithmetic Operator+ = Addition- = Subtraction* = Multiplication/ = Division% = Modulo** = Exponentiation2.Assignment Operator = "equal to3.Array Operator + = Union == = Equality === = Identity != = Inequality <> = Inequality !== = Non-identity4.Bitwise Operator& = and ^ = xor| = not<< = shift left>> = shift right5.Comparison Operator== = equal=== = identical!= = not equal!== = not identical<> = not equal< = less than<= less than or equal> = greater than>= = greater than or equal<=> = spaceship operator6.Execution Operator `` = backticks 7.Error Control Operator @ = at sign8.Incrementing/Decrementing Operator ++$a = PreIncrement $a++ = PostIncrement --$a = PreDecrement $a-- = Postdecrement9.Logical Operator && = And || = Or ! = Not and = And xor = Xor or = Or10.string Operator . = concatenation operator .= concatenating assignment operator11.Type Operator instanceof = instanceof12.Ternary or Conditional operator ?: = Ternary operator13.Null Coalescing Operator ??" = null coalescing 14.Clone new Operator clone new = clone new15.yield from Operator yield from = yield from16.yield Operator yield = yield17.print Operator print = print
yasuo_ohgaki at hotmail dot com ¶24 years ago
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.
<?php
$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.