Operators - PHP Manual Notes

WBOY
Release: 2016-08-08 09:29:20
Original
770 people have browsed it

Operator precedence

Every programming language has operators, and operators must be learned to use flexibly.

Operators have different priorities and combination directions.

<code><?php 
var_dump(1 <= 1 == 1);  // true
var_dump(true ? 0 : true ? 1 : 2);  // 2
$a = 1;
var_dump(++$a + $a++);  // may print 4 or 5</code>
Copy after login

Use parentheses when necessary to enhance the readability of your code.

Arithmetic operators

The result of the modulo operator has the same sign as the dividend.

The

assignment operator copies the value of the original variable to the new variable. The exception is that when an object is encountered, the value is assigned by reference unless the clone keyword is explicitly used to copy.

The new operator automatically returns a reference.

bit operators

Displacement has the following rules:

  • Bits moved out in any direction are discarded.
  • Padding with zeros when shifting left, and the sign is not preserved.
  • The sign bit is filled when shifting right, which means the positive and negative signs are retained.

The focus of this section is to understand the several example programs that are the focus of the manual. The XOR operation of strings in the example is difficult to understand. We will look at this later. There are also integer displacements, I feel like I understand them well.

Comparison operators

Ordinary equal sign==As long as the two values ​​​​are equal after type conversion, it will return true.

If comparing a number and a string or comparing strings involving numeric content, the string will be converted to a numeric value and the comparison will be performed as a numeric value.

<code><?php 
var_dump(0 == "a");  // true
var_dump("1" == "01");  // true
var_dump("10" == "1e1");  // true</code>
Copy after login

Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 is true, otherwise it returns expr3. Ternary operators are evaluated from left to right.

Error Control Operator

PHP supports an error control operator @, which is only valid for expressions. By placing it before an expression, any error messages the expression may produce are ignored.

It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and foreach.

Execution operator

PHP supports an execution operator: the backtick, which is the one in the upper left corner of the keyboard. The effect is the same as the function shell_exec().

<code><?php 
$output = `systeminfo`;
$outip = shell_exec(&#39;ipconfig&#39;);
echo "<pre class="brush:php;toolbar:false">$outip
"; echo "
$output
";
Copy after login
The

backtick operator is invalid when safe mode is activated or shell_exec() is turned off.

Attention! Backticks cannot be used in double-quoted strings.

increment decrement operator

Increment/decrement operators do not affect boolean values.
Decreasing the NULL value has no effect, but the result of incrementing NULL is 1.

When dealing with arithmetic operations on character variables, PHP follows Perl's habits instead of C's. For example, in Perl $a = 'Z'; $a++; will turn $a into 'AA'.

Attention! Character variables can only be incremented, not decremented, and only support pure letters (a-z and A-Z). Incrementing/decrementing other character character variables will be invalid, and the original string will not change.

<code><?php 
$z = &#39;z&#39;;
$Z = &#39;Z&#39;;
var_dump(++$z);  // &#39;aa&#39;
var_dump(++$Z);  // &#39;AA&#39;</code>
Copy after login

Logical operators

|| has higher priority than or. && has higher priority than and.

String operators

The first one is the connection operator ., and the second one is the connection assignment operator .=.

Array operators

  1. Union: $a + $b. Append the right array elements to the left array. If the keys in both arrays are present, only the keys in the left array will be used, and any changes will be ignored.

  2. Equal: $a == $b. have the same key-value pairs.

  3. Congruent: $a === $b. Have the same key-value pairs, in the same order and type.

  4. does not vary: $a != $b or $a <> $b.

  5. Not congruent: $a !== $b.

  6. If the cells in the array have the same key name and value, they will be equal when compared. Don't care about the order and type.

    <code><?php 
    $a = array("apple", "banana");
    $b = array(1 => "banana", "0" => "apple");
    var_dump($a);
    var_dump($b);
    var_dump($a == $b);
    var_dump($a === $b);</code>
    Copy after login

    The output results are as follows.

    <code>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 true
    
    boolean false</code>
    Copy after login

    Type operators

    There is a type operator instanceof in PHP, which is used to determine whether a PHP variable belongs to an instance of a certain class.

    <code><?php 
    class MyParent {}
    class MyClass extends MyParent {}
    class NotMyClass {}
    interface MyInterface {}
    class InClass implements MyInterface {}
    $a = new MyClass;
    var_dump($a instanceof MyClass);  // true
    var_dump($a instanceof NotMyClass);  // false
    var_dump($a instanceof MyParent);  // true
    $b = new InClass;
    var_dump($b instanceof MyInterface);  // true
    $c = 'InClass';
    var_dump($b instanceof $c);  // true
    var_dump($c instanceof stdClass);  // false</code>
    Copy after login

    Note that instanceof is not allowed to be used to detect constants.

    (Full text ends)

    The above introduces the operators - PHP manual notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!