运算符是可以通过给出的一或多个值(用编程行话来说,表达式)来产生另一个值(因而整个结构成为一个表达式)的东西。

运算符可按照其能接受几个值来分组。一元运算符只能接受一个值,例如!(逻辑取反运算符)或++(递增运算符)。 二元运算符可接受两个值,例如熟悉的算术运算符+(加)和-(减),大多数 PHP 运算符都是这种。最后是唯一的三元运算符? :,可接受三个值;通常就简单称之为“三元运算符”(尽管称之为条件运算符可能更合适)。

PHP 的运算符完整列表见下节运算符优先级。该节也解释了运算符优先级和结合方向,这控制着在表达式包含有若干个不同运算符时究竟怎样对其求值。of course this should be clear, but i think it has to be mentioned espacially:

AND is not the same like &&

for example:

is not the same like

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:

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, WarhogIf you use "AND" and "OR", you'll eventually get tripped up by something like this:

$this_one = true;

$that = false;

$truthiness = $this_one and $that;

?>

Want to guess what $truthiness equals?

If you said "false" ...it's wrong!

"$truthiness" above has the value "true". Why? "=" has a higher precedence than "and". The addition of parentheses to show the implicit order makes this clearer:

($truthiness = $this_one) and $that;

?>

If you used "&&" instead of and in the first code example, it would work as expected and be "false".

This also works to get the correct value, as parentheses have higher precedence than "=":

$truthiness = ($this_one and $that);

?>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.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. [http://www.php.net/manual/en/language.variables.variable.php]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++):

$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:

$a = 2;

echo ($a == 1 ? 'one':

($a == 2 ? 'two':

($a == 3 ? 'three':

($a == 4 ? 'four': 'other') ) ) );

echo "\n";

//prints 'two'

?>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.A quick note to any C developers out there, assignment expressions are not interpreted as you may expect - take the following code ;-

$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.Simple POST and PRE incremnt sample:

$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

php7+结合比较运算符,php7运算符相关推荐

  1. SQL基础学习总结:3(select语句基础算术运算符比较运算符)

    select语句基础 列的查询 从表中选取数据时需要使用select语句,通过select语句查询并选取出必要数据的过程称为匹配查询或查询. 语法结构如下: select <列名1>,&l ...

  2. php null运算符,PHP7 Null合并运算符

    在PHP7,一个新的功能,空合并运算符(??)已被引入.它被用来代替三元运算并与 isset()函数功能结合一起使用.如果它存在并且它不是空的,空合并运算符返回它的第一个操作数;否则返回第二个操作数. ...

  3. 简述PHP中有哪些运算符,PHP运算符简述

    PHP的运算符号和操作符号 按运算符号的功能划分为 算术运算符,用于处理常用的数学运算 加+ 减- 乘* 除/ 取余运算符% 通常运用于整除运算(如判断是否闰年)和控制范围.尽量不要使用小数和负数进行 ...

  4. java 运算符_java 运算符

    运算符是一些特殊的符号,主要用于数学函数,一些类型的赋值语句或逻辑比较方面.Java 中的运算符分为以下几类. 一.算数运算符 算数运算符包括: + 加法运算,字符串连接运算 - 减法运算 * 乘法运 ...

  5. VBS基础篇 - 运算符(1) - 运算符优先级

    VBScript 有一套完整的运算符,包括算术运算符.字符串运算符.关系运算符和逻辑运算符. 运算符优先级 运算符优先级:在一个表达式中进行多个运算时,每一部分都会按预先确定的顺序进行计算求解. 括号 ...

  6. (4)javascript的运算符以及运算符的优先级

    运算符的使用方法 在javascript的程序中要完成各种各样的运算,是离不开运算符的. 在javascript中,按运算符类型可以分为算术运算符.赋值运算符.比较运算符.逻辑运算符.条件运算符等. ...

  7. 【C++ 语言】面向对象 ( 函数重载 | 运算符重载 | 运算符重载两种定义方式 | 拷贝构造方法 | RVO 优化 | NRVO 优化 )

    文章目录 函数重载 运算符重载 ( 类内部定义云算符重载 ) 运算符重载 ( 类外部定义运算符重载 ) 可重载的运算符 拷贝构造方法 编译器优化 ( RVO 优化 | NRVO 优化 ) 完整代码示例 ...

  8. 在学习Python基础中需要知道的知识点:运算符大全,收藏,以后方面查询(算术运算符、赋值运算符、比较运算符、位运算符、逻辑运算符、成员运算符、身份运算符、运算符优先级))

    一.算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘或是返回一个被 ...

  9. java 逗号运算符_Java 运算符

    Java 运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 ...

  10. 二元运算符的运算符重载

    运算符重载的两种方式使用成员函数或者使用全局函数(友元函数) 不过是成员函数还是友元函数,最终使用方法是想用的,但是实现的过程不同,实现的代码也不一样 一个简单的运算符重载 其实运算符重载的难点在于如 ...

最新文章

  1. 用 Handler 轻松实现专属Android定时器
  2. Redis5.0:这些场景下使用,高效还降低成本!
  3. C# 计算时间差 用timespan函数
  4. REUSE_ALV_GRID_DISPLAY显示ALV,设置可编辑时,与内表数据同步问题
  5. 使用Apache Commons IO组件读取大文件
  6. 简述使jdbc连接mysql数据库,关于JDBC的六个步骤
  7. jquery基本过滤选择器(jquery筛选选择器)
  8. linux基本命令操作(二)
  9. cups支持的打印机列表_使用CUPS打印管理器管理打印机
  10. FreeCodeCamp学习--Falsy Bouncer
  11. marvin java_java-与MarvinFramework比较图像
  12. 白杨SEO:SEO转型难吗?SEO如何转型ASO(应用商店搜索优化)?
  13. 利用全球定位改进导航
  14. 【字源大挪移—读书笔记】 第一部分:字首
  15. 联机版的连连看源代码
  16. 【Linux】Tomcat优化
  17. 打字游戏之暂停与结束
  18. 谷歌浏览器开发者工具使用
  19. Java实现 LeetCode 233 数字 1 的个数
  20. “好奇号”顺利完成”火星第一勺“

热门文章

  1. db2如何锁定一张表_如何通过一张表,提高20%的工作效率?
  2. 在php里怎么安装composer,怎么安装composer
  3. C程序设计 -- 随笔
  4. php是一种,php是一种什么类型的语言
  5. 指纹识别 python实现_Python还真当是无所不能!利用Python做指纹识别播报!闻所未闻!-站长资讯中心...
  6. java 字符串数组排序_Java 使用泛型为不同类型数组排序
  7. 【UVA11059】Maximum Product(set+set默认从大到小排列---水题)
  8. string 操作 java_Java中String类的一些常见问题
  9. java comparable排序_java_Java使用Comparable解决排序问题,本文实例讲述了Java使用Comparabl - phpStudy...
  10. 翻译: Swift 中信号量的美妙之处