Operators

Operators are used to perform operation.

Operator are mainly divided by three groups. 1.Uniary Operators that takes one values 2.Binary Operators that takes two values 3.ternary operators that takes three values

Arithmetic Operator

+ => Addition
2 + 2
#=> 4
- => Subtraction
5 - 3
#=> 2
* => Multiplication
4 * 4
#=> 16
/ => Division
10 / 5
#=> 2
% => Modulo

Returns the remainder of a division.

11 % 5
#=> 1

Assignment Operator

= => Equal to

The bellow example sets the value of varibale $a to 2.

 $a = 2

Array Operator

+ => Union

Merges arrays together using their indexes.

['name' => "Alex"] + ["last_name" => "Doe"]
#=> [
#=>     "name" => "Alex",
#=>     "last_name" => "Doe"
#=> ]

[0 => "Books"] + [0 => "Movies", 1 => "Music"]
#=> [
#=>     "Books",
#=>     "Music"
#=> ]
== => Equality
[1,2,3] == [1,2]
#=> FALSE

[1,2,3] == [1,2,3]
#=> TRUE

[1,2] == [0 => "1", 1 => "2"]
#=> TRUE
=== => Identity
[1,2,3] === [1,2,3]
#=> TRUE

[1,2] === [0 => "1", 1 => "2"]
#=> FALSE
!= => Inequality
[1,2,3] != [1,2]
#=> TRUE

[1,2,3] != [1,2,3]
#=> FALSE

[1,2] != [0 => "1", 1 => "2"]
#=> FALSE
!== => Non-Identity
[1,2,3] !== [1,2,3]
#=> FALSE

[1,2] !== [0 => "1", 1 => "2"]
#=> TRUE

Comparison Operator

== => Equality
1 == 1
#=> TRUE

1 == "1"
#=> TRUE

1 == TRUE
#=> TRUE

1 == "a"
#=> FALSE
=== => Identity
1 === 1
#=> TRUE

1 === "1"
#=> FALSE

1 === TRUE
#=> FALSE

1 === "a"
#=> FALSE
!= => Inequality
1 != 1
#=> FALSE

1 != "1"
#=> FALSE

1 != TRUE
#=> FALSE

1 != "a"
#=> TRUE
!== => Non-Identity
1 !== 1
#=> FALSE

1 !== "1"
#=> TRUE

1 !== TRUE
#=> TRUE

1 !== "a"
#=> TRUE
< => Less Than
2 < 4
#=> TRUE

4 < 2
#=> FALSE

2 < 2
#=> FALSE
<= => Less Than or Equal
2 <= 4
#=> TRUE

4 <= 2
#=> FALSE

2 <= 2
#=> TRUE
> => Greater Than
2 > 4
#=> FALSE

4 > 2
#=> TRUE

2 > 2
#=> FALSE
>= => Greater Than or Equal
2 >= 4
#=> FALSE

4 >= 2
#=> TRUE

2 >= 2
#=> TRUE

Incrementing/Decrementing Operator

++$x => Pre-Increment
$x = 2

# increments $x by one, then returns $x
++$x
#=> 3
$x++ => Post-Increment
$x = 2

# returns $x 
$x++
#=> 2

# then increments $x by one
$x
#=> 3
--$x => Pre-Decrement
$x = 2

# decrements $x by one, then returns $x
--$x
#=> 1
$x-- => Post-Decrement
$x = 2

# returns $x 
$x--
#=> 2

# then increments $x by one
$x
#=> 1

Logical Operator

&& => And
IF(2 > 1 && 3 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> TRUE

IF(5 > 1 && 0 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> FALSE
AND => And
IF(2 > 1 AND 3 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> TRUE

IF(5 > 1 AND 0 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> FALSE
|| => Or
IF(2 > 1 || 3 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> TRUE

IF(5 > 1 || 0 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> TRUE
OR => Or
IF(2 > 1 OR 3 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> TRUE

IF(5 > 1 OR 0 > 1) {
    RETURN TRUE
} ELSE {
    RETURN FALSE
}
#=> TRUE
! => Not
$x = TRUE

!$x
#=> FALSE

String Operator

. => Concatenation Operator
"Hello " . "world!"
#=> "Hello world!"
.= => Concatenating Assignment Operator
#===
$x = "Hello"
$x .= " world!"
#=> "Hello world!"

?? => Null Coalescing Operator

??" => null coalescing

$x = NULL
$a = $x ?? " x was null"

$a
#=> " x was null"

$y = "Hello there"
$b = $y ?? " y was null"

$b
#=> "Hello there"


Copyright © SunSed LLC 2013-2021 All rights reserved.