PHP Operators


PHP operators include:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. The ternary operator
  5. Logical operators
  6. String operators
  7. Error Control Operators

Arithmetic Operators

operator Description Example result
+ Add $x = 2;
$y = $x + 1;
$y = 3
- Subtract $x = 2;
$y = $x-1;
$y = 1
* multiplied $x = 2;
$y = $x * 3;
$y = 6
/ except that the result is a floating point type $x = 6;
$y = $x /3;
$y = 2
% for more than, the return result is a floating point type $x = 6;
$y = $x% 4;
$y = 2
++ Add, divided into before and after adding
on the Boolean and NULL is invalid
$x++;(after the addition) $x = 2;
++ $x;(before)
$x = 3
- Decrement, divided into pre-decrement and post-decrement
is invalid for Boolean and NULL
$x = 2;
- $x;(before subtraction)
$x-;(after subtraction)
$x = 1

For the former plus after the increase, after the implementation of the results are variables plus 1, the difference is that the implementation of the results are not the same return, refer to the following two examples:

$x = 2;
echo $x++; // output 2
echo $x; // Output 3

$x = 2;
echo ++$x; // Output 3
echo $x; // Output 3

Decrease the same token.


Assignment operator

Assignment operator "=" for assignment operations, the assignment of the role of the operator is to the right of the value assigned to the left variables:

$x = 2;

This example assigns the integer value 2 to the variable $x.

Assignment operator other usage

Assignment operators can be nested for use:


$y = ($x = 2) + 5; // The result is $x = 2, $y = 7

Some Variants of the Assignment Operator Usage:

$y += $x;

In fact it is equivalent to:

$y = $y + $x;

Similarly there are:

$y -= $x;
$y *= $x;
$y /= $x;
$y %= $x;
$y .= $x; // For the concatenation operator, see the string operator

Comparison operator

operator Description Example result
== equals 2 == 3 FALSE
!= is not equal or can be written as < > 2 == 3 TRUE
> greater than 2 > 3 FALSE
< less than 2 < 3 TRUE
>= greater than or equal to 2 > = 3 FALSE
<= less than or equal 2 < = 3 TRUE

The comparison operators can also be used for string comparisons.


Ternary operator

Ternary can be regarded as a special comparison operator:

(Expr1)?(Expr2):(expr3)

Syntax Explanation: The value of expr2 is expr2 when expr1 evaluates to TRUE, or expr3 otherwise.

example:

$y = ($x == 2) $x: 1;

This example determines whether the value of the variable $x is equal to 2, and if $x is equal to 2, then the value of $y is equal to $x(that is, equal to 2) and vice versa.

Tip

In order to avoid errors, it is a good idea to enclose the expressions of the ternary operator in parentheses.


Logical operator

operator Description Example result
&& Logical AND, can also be written and $x = 2;
$y = 6;
$x &&$y > 5
FALSE
|| Logical OR, can also be written as or $x = 2;
$y = 6;
$x &&$y > 5
TRUE
! logical not, take the logical opposite $x = 2;
$y = 6;
!($x > $y)
TRUE

Join operator

String operator mainly refers to the connection operator ".", It will connect its left and right parameter string.

example:

$x = " London ";
$y = $x. &Quot; Hello " // $y = " London hello "

As shown in this example, the concatenation operator is required when outputting characters and variables together.

The concatenation operator, along with the assignment operator, forms the join assignment operator:

$x = " London ";
$x. = &Quot; Hello " // $x = " London hello "

Error Control Operators

The error control operator: @, when placed in a PHP expression before the expression may have any error messages are ignored.

/* To read the file * /
$my_file = @file('file1') or
    die(" open file failed, error message: '$php_errormsg' ");

The error control operator is often used to block PHP system errors in the places where errors may occur and to prompt a friendly error message.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook