PHPKonf Istanbul PHP Conference 2019 - Call for Papers

11选五湖北开奖结果:Operators

Table of Contents

湖北十一选五官网 www.na503.cn An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

Operators can be grouped according to the number of values they take. Unary operators take only one value, for example ! (the logical not operator) or ++ (the increment operator). Binary operators take two values, such as the familiar arithmetical operators + (plus) and - (minus), and the majority of PHP operators fall into this category. Finally, there is a single ternary operator, ? :, which takes three values; this is usually referred to simply as "the ternary operator" (although it could perhaps more properly be called the conditional operator).

A full list of PHP operators follows in the section Operator Precedence. The section also explains operator precedence and associativity, which govern exactly how expressions containing several different operators are evaluated.

add a note add a note

User Contributed Notes 7 notes

up
209
Anonymous
14 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 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
up
20
yasuo_ohgaki at hotmail dot com
17 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.
up
-1
figroc at gmail dot com
10 years ago
The variable symbol '$' should be considered as the highest-precedence operator, so that the variable variables such as $$a[0] won't confuse the parser.  [//www.na503.cn/manual/en/language.variables.variable.php]
up
-1
phpnet dot 20 dot dpnsubs at xoxy dot net
11 years ago
Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity.

You cannot write code like this (as you may have accustomed to in C/C++):

<?php
$a
= 2;
echo (
   
$a == 1 ? 'one' :
   
$a == 2 ? 'two' :
   
$a == 3 ? 'three' :
   
$a == 4 ? 'four' : 'other');
echo
"\n";
// prints 'four'
?>

You need to add brackets to get the results you want:

<?php
$a
= 2;

echo (
$a == 1 ? 'one' :
        (
$a == 2 ? 'two' :
        (
$a == 3 ? 'three' :
        (
$a == 4 ? 'four' : 'other') ) ) );
echo
"\n";
//prints 'two'
?>
up
-26
me at robrosenbaum dot com
11 years ago
The scope resolution operator ::, which is missing from the list above, has higher precedence than [], and lower precedence than 'new'. This means that self::$array[$var] works as expected.
up
-31
golotyuk at gmail dot com
12 years ago
Simple POST and PRE incremnt sample:

<?php

$b
= 5;
$a = ( ( ++$b ) > 5 ); // Pre-increment test
echo (int)$a;

$b = 5;
$a = ( ( $b++ ) > 5 ); // Post-increment test
echo (int)$a;

?>

This will output 10, because of the difference in post- and pre-increment operations
up
-38
rick at nomorespam dot fourfront dot ltd dot uk
13 years ago
A quick note to any C developers out there, assignment expressions are not interpreted as you may expect - take the following code ;-

<?php
$a
=array(1,2,3);
$b=array(4,5,6);
$c=1;

$a[$c++]=$b[$c++];

print_r( $a ) ;
?>

This will output;-
Array ( [0] => 1 [1] => 6 [2] => 3 )
as if the code said;-
$a[1]=$b[2];

Under a C compiler the result is;-
Array ( [0] => 1 [1] => 5 [2] => 3 )
as if the code said;-
$a[1]=$b[1];

It would appear that in php the increment in the left side of the assignment is processed prior to processing the right side of the assignment, whereas in C, neither increment occurs until after the assignment.
To Top
  • 候选案例:国际甲状腺知识宣传周义诊 2018-12-13
  • 大家都误解蔡英文了?台专家酸讽:台湾的问题出在2300万人身上 2018-12-13
  • 外国领导人祝贺我新一届领导人 2018-12-13
  • 浙江一母亲去医生儿子那看病 监控看了让人落泪 2018-12-12
  • 回复@老老保老张工:计划不要批准?那不还是你自己做主?有必要走形式么? 2018-12-12
  • 一语惊坛(6月15日):人民日报和共和国共同成长。 2018-12-11
  • 围挡施工一年多 据说完工尚无期(图) 2018-12-11
  • 12306网站用户信息外泄?铁总深夜“辟谣” 2018-12-11
  • 第二届加强创新和社会管理案例理论论坛暨社会管理创新案例颁奖典礼 2018-12-10
  • 中国共产党第十九次全国代表大会 2018-12-10
  • 女性之声——全国妇联 2018-12-10
  • NBA总决赛4比0横扫骑士问鼎 4年夺3冠勇士王朝! 2018-12-09
  • 图解:关于中国梦,习近平总书记这十句话直抵人心 2018-12-09
  • 【十九大·理论新视野】动漫:“美丽中国”如何绘就 2018-12-09
  • 宁波制造分享俄罗斯世界杯经济蛋糕 2018-12-08
  • 333| 359| 570| 672| 45| 903| 349| 209| 34| 146|