运算符号
运算符号可以用来处理数字、字符串及其它需要比较运算的条件。php 的运算符号和 c 语言的运算符号与很类似,对于有经验的程序设计人员,应可以很顺利的掌握 php 的运算符号。
不同的运算符号,其实还是有优先顺序,就像小时候在学数学时,老师会教:先乘除、后加减。在 php 的运算优先顺序可以参考下面的表格,在混合式的情形下,愈往下表示优先权愈高。
左至右or左至右xor左至右and左至右.= &= |= /= %= ^= = += -= *=左至右? :左至右||左至右&&左至右|左至右^左至右&左至右== !=不限= >不限>左至右+ - .左至右* / %左至右! ~ ++ -- @右至左[]右至左 运算符号 结合规则
就像先哲说的:物有本末、事有终始,知所先后,则近道矣,在运算时只要照着运算优先顺序写出来的程序,应该不会发生结果和预期不同的情形。在写作时多注意细节,可以减少调试的痛苦!
逻辑运算
逻辑运算 (logical operators) 通常用来测试真假值。最常见到的逻辑运算就是循环的处理,用来判断是否该离开循环或继续执行循环内的指令。
大于=大于或等于==等于!=不等于&&而且 (and)and而且 (and)||或者 (or)or或者 (or)xor异或 (xor)!不 (not)
$a = 5;
if ($a != 5) {
echo "$a 不是 5";
} else {
echo "$a 是 5";
}
?>
PHP 位运算
php 的位运算子 (bitwise operators) 共有六个,提供数字做一些快速而低阶的运算。要了解更多有关位运算的信息,可以参考离散数学方面的书籍。
&且 (and)|或 (or)^异或 (xor)>向右移位~取 1 的补数 符号 意义
赋值运算
赋值运算 (assignment operator) 有时会让人搞得一头雾水,不过它可以让程序更精简,增加程序的执行效率。
=将右边的值连到左边+=将右边的值加到左边-=将右边的值减到左边*=将左边的值乘以右边/=将左边的值除以右边%=将左边的值对右边取余数.=将右边的字符串加到左边
$a = 5;
$a += 2; // 即 $a = $a + 2;
echo $a."
\n";
$b = "哇";
$b .= "哈"; // $b = "哇哈";
$b .= "哈"; // $b = "哇哈哈";
echo "$b
\n";
?>
字符串运算符
字符串运算 (string operator) 的运算符号只有一个,就是英文的句号 .。它可以将字符串连接起来,变成合并的新字符串。
以下是字符串运算的例子
$a = "php 4";
$b = "功能强大";
echo $a.": ".$b;
?>
算术运算
算术运算 (arithmetic operators) 符号,就是用来处理四则运算的符号,这是最简单,也最常用的符号,尤其是数字的处理,几乎都会使用到算术运算符号。
+加法运算-减法运算*乘法运算/除法运算%取余数++累加--递减 符号 意义
以下为简单的算术运算范例
$a = 8;
$b = 2;
$c = 3;
echo $a+$b."
\n";
echo $a-$b."
\n";
echo $a*$b."
\n";
echo $a/$b."
\n";
echo $a%$c."
\n";
$a++;
echo $a."
\n";
$c--;
echo $c;
?>
其它运算符号
除了上述的运算符号之外,还有一些运算符号难以归类。
$变量
&变量的地址 (加在变量前)
@不显示错误信息 (加在函数前)-
>类的方法或者属性
=>数组的元素值
? :三元运算子
其中比较特殊的是三元运算子 ? :,以下例来解释
(expr1) ? (expr2) : (expr3);
若 expr1 的运算结果为 true,则执行 expr2;否则执行 expr3。实际上它有点类以 if...else 循环,但可以让程序较精简有效率。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
