PHP operators

PHP operators

PHP has a wide variety of operators

These operators can be distinguished into two groups: binaries, which act on two operands and unaries, which only require one operand

Thus, its general syntax is:

operand1 operator_Binary operand2

operand1 operator_unary

operator_unary operand1

Arithmetic operators

PHP supplies basic operations with the only additions of operators that return the rest of the division between the left and right operators; and the exponentiation, which allows to raise the left operator to the power of the right operator

Arithmetic operators
+ Addition
Subtraction
* Multiplication
/ Division
% Rest
** Exponentiation

Note In other languages the + operator applied to strings concatenates both strings into a single

However, in PHP to perform this operation the operator is used.

Operators of increase (++) and decrease (- -)

These operators are unary operators and they autoincremento and the autodecremento to the variable that applies to them

In addition to modifying the variable, they return the value of the variable

The operator increment or decrement can go before p behind the variable having a different meaning

If the operator is ++, it is placed after the variable is called post-increment, by first taking the value and then increasing the variable by one unit

If the ++ operator is placed before the variable, it is called pre-increment and makes that first to increase in a unit variable and then take the value

If the operator is - - it is placed after the variable, it is called post-decrement, making it take the value first and then the variable is decremented by one unit

If the operator - - is placed before the variable, it is called pre-decrement and makes that first decremente in a unit variable and then take the value

Relational operators

Are used typically in the conditional expression

The relational operators return boolean values

The operands can be numerical or strings

Relational operators
>

Greater than

<

Less than

> =

Greater than or equal to

< =

Less than or equal to

! =

Other than that

< >

It's equivalent to ! =

! = =

Different than and of the same type

= =

Like

= = = Same as y of the same type

< = >

Order Comparison (Added in PHP 7)

  • 0 when they're the same
  • -1 when the first operator is less than the second operator
  • 1 when the first operator is larger than the second
? ?

Returns its first operand if it exists and is not NULL

Otherwise it returns its second operand (Added in PHP 7)

Logical operators

The logical operands is related to the relational as they are normally the operands used are the result of expressions in relational

The resulting values are boolean

Logical operators
AND True if both are true
& & It is equivalent to AND
OR True if one of them is true
| | It's equivalent to OR
XOR True if one of them is true, but not both
! If it was true it goes to false and vice versa

Bitwise operators

The way of working of these operators is to convert to the binary operands and then operate with them bitwise

Bitwise operators
& AND
| OR
^ XOR
~ NOT
<< Propagation to the left
Shift the value to the left by entering zeros, if it goes out of range, values are lost
>> Spread to the right
Moves the value to the right entering by the left, the sign bit and eliminating the values that are out by the right

Note Propagation operators take two operands: the first is the variable to propagate and the second is the number of positions to propagate

Assignment operators

The assignment is also an operator that returns the variable modified

The assignment operator in PHP is =

The assignment operators shown below are but abbreviations that make expressions more comfortable and simple, even if they are sometimes more unreadable

Assignment operators
Operator Expression Equivalence
=

$A = $B = $C;

$D = ‘Text’;

$A = $C;

$B = $C;

$D = ‘Text’;

+ = $A + = 4; $A = $A + 4;
– = $A – = 3 * $B; $A = $A – (3 * $B);
* = $A * = 2; $A = $A * 2;
/ = $A / = 35 + $B; $A = $A / (35 + $B);
% = $A % = $B; $A = $A % $B;
. = $D . = ‘ of test’; $D = $D . ‘ of test’;
>> = $A >> = 1; $A = $A >> 1;
<< = $A << = $B; $A = $A << $B;
& = $A & = ($C + = 3); $C = $C +3;
$A = $A & $C;
^ = $A ^ = 2; $A = $A ^ 2;
| = $A | = $C; $A = $A | $C;

Other operators

Selection operator

This operator is used to execute an operation or another depending on the condition

The format is as follows:

Condition ? Exp1 : Exp2

If the condition is met it evaluates and returns the expression Exp1 if not the Exp2

We can put a just value

Example:

New operator

This operator is used to create an instance of a type of objects previously defined

The syntax to follow is as follows:

These parameters are passed to the constructor of that object in question

Operador clone

This operator will be used to create a copy of a previously defined object (which invokes, if possible, the method __clone() object)

The syntax to follow is as follows:

When cloning an object, PHP will make a shallow copy of the object's properties

Properties that are references to other variables will keep the references

Once cloning is complete, the method will be called __clone() of the new object (if the method __clone() defined), to allow you to make the necessary changes to your properties

gettype operator

This operator applied to a variable returns the type of object to which belongs the data contained by that variable

Its syntax is:

The values you can return are:

  • boolean

  • integer

  • double

    For historical reasons it is returned double also when it's kind of float

  • string

  • array

  • object

  • resource

  • NULL

  • unknown type

Preference

The operator precedence will determine the order in which they are running in a given expression

Using parentheses will check that the operations are carried out according to us we want to

In PHP the preference of the operators from highest to lowest is as follows:

Preference
Other operators clone new
Array [ ]
Exponentiation **
(in / de) crease /types ++ – – ^ (int) (float) (string) (array) (object) (bool) @
Other operators gettype
Denial !
Mul / Div / Rest * / %
Addition / Subtraction +
Spread << >>
Relational < < = > > =
Equality = = != = = = ! = = < > < = >
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
AND logic &&
A logical OR | |
Equality ? ?
Other operators ?:
Assignment = + = – = * = * * = / = . = % = & = | = ^ = >> = << =
AND logic AND
XOR lógica XOR
A logical OR OR