Operator-PHP Manual notes

Source: Internet
Author: User
Operator Precedence

Each programming language has operators, and operators learn to use them flexibly.

Operators have different precedence and binding orientations.


  
   

在需要的时候使用括号,可以增强代码的可读性。

算术运算符

取模运算符的结果和被除数的符号相同。

赋值运算符将原变量的值拷贝到新变量中,有个例外就是碰到对象时,是以引用赋值的,除非明确使用clone关键字来拷贝。

new运算符自动返回一个引用。

位运算符

位移有一下几个规律:

    • 向任何方向移出去的位都被丢弃。
    • 左移时以零填充,且正负号不被保留。
    • 右移时以符号位填充,意味着正负号保留。

这一节的重点就是把手册重点的几个示例程序看懂吧。示例中字符串的XOR运算比较难理解,这个以后再看。还有整数的位移,感觉理解就好吧。

比较运算符

普通等号==只需类型转换后两值相等就返回真。

如果比较一个数字和字符串或者比较涉及到数字内容的字符串,则字符串会被转换为数值并且比较按照数值来进行。

 

自PHP 5.3起,可以省略三元运算符中间那部分。表达式expr1 ?: expr3在expr1真时返回expr1,否则返回expr3。三元运算符是从左往右计算的。

错误控制运算符

PHP支持一个错误控制运算符@,@运算符只对表达式有效。将其放在表达式前,该表达式可能产生的任何错误信息都被忽略掉。

不能把它放在函数或类的定义之前,也不能用于条件结构例如if和foreach等。

执行运算符

PHP支持一个执行运算符:反引号,就是键盘左上角那个。效果与函数shell_exec()相同。

 $outip
";echo "
$output
";

The anti-quote operator shell_exec() is not valid when Safe mode is activated or closed.

Attention! The anti-quote cannot be used in a double-quote string.

Increment decrement operator

The increment/decrement operator does not affect the Boolean value.
Decreasing the null value also has no effect, but the result of incrementing null is 1.

When dealing with the arithmetic operations of character variables, PHP inherits the Perl habit, not the C. For example, in Perl, $a = 'Z'; $a++; a $ A will be turned into a ' AA '.

Attention! Character variables can only be incremented, not decremented, and only plain letters (A-Z and A-Z) are supported. Increment/Decrement Other character variables are not valid and the original string does not change.


  
     

逻辑运算符

||or的优先级高。&&and的优先级高。

字符串运算符

第一个是连接运算符.,第二个是连接赋值运算符.=

数组运算符

    1. 联合:$a + $b。把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,有变动额被忽略。

    2. 相等:$a == $b。具有相同的键值对。

    3. 全等:$a === $b。具有相同的键值对,并且顺序和类型都相同。

    4. 不等:$a != $b$a <> $b

    5. 不全等:$a !== $b

数组中的单元如果具有相同的键名和值则比较时相等,不要在意顺序和类型。

  "banana", "0" => "apple");var_dump($a);var_dump($b);var_dump($a == $b);var_dump($a === $b);

输出结果如下所示。

array (size=2) 0 => string 'apple' (length=5) 1 => string 'banana' (length=6)array (size=2) 1 => string 'banana' (length=6) 0 => string 'apple' (length=5)boolean trueboolean false

类型运算符

PHP中有一个类型运算符instanceof,用于确定一个PHP变量是否属于某一类的实例。

 

注意,instanceof不允许用来检测常量。

(全文完)

以上就介绍了运算符 - PHP手册笔记,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.