Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Operators

PHP Operators

Operators are utilized to perform operations on variables and values.

PHP categorizes operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

PHP arithmetic operators are used with numeric values to carry out basic arithmetic operations, including addition, subtraction, multiplication, and more.

 

Operator

Name

Example

Result

+

Addition

$x + $y

Sum of $x and $y

Subtraction

$x – $y

Difference of $x and $y

*

Multiplication

$x * $y

Product of $x and $y

/

Division

$x / $y

Quotient of $x and $y

%

Modulus

$x % $y

Remainder of $x divided by $y

**

Exponentiation

$x ** $y

Result of raising $x to the $y’th power

 

PHP Assignment Operators

PHP assignment operators are used to assign a value to a variable.

The fundamental assignment operator in PHP is “=”, which means the left operand is set to the value of the expression on the right.

 

 

Assignment

Same as…

Description

x = y

x = y

The left operand gets set to the value of the expression on the right

x += y

x = x + y

Addition

x -= y

x = x – y

Subtraction

x *= y

x = x * y

Multiplication

x /= y

x = x / y

Division

x %= y

x = x % y

Modulus

 

PHP Comparison Operators

PHP comparison operators are used to compare two values, whether they are numbers or strings.

 

Operator

Name

Example

Result

==

Equal

$x == $y

Returns true if $x is equal to $y

===

Identical

$x === $y

Returns true if $x is equal to $y, and they are of the same type

!=

Not equal

$x != $y

Returns true if $x is not equal to $y

<> 

Not equal

$x <> $y

Returns true if $x is not equal to $y

!==

Not identical

$x !== $y

Returns true if $x is not equal to $y, or they are not of the same type

Greater than

$x > $y

Returns true if $x is greater than $y

Less than

$x < $y

Returns true if $x is less than $y

>=

Greater than or equal to

$x >= $y

Returns true if $x is greater than or equal to $y

<=

Less than or equal to

$x <= $y

Returns true if $x is less than or equal to $y

<=>

Spaceship

$x <=> $y

Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.

 

PHP Increment / Decrement Operators

The PHP increment operators are used to increase a variable’s value.

The PHP decrement operators are used to decrease a variable’s value.

Operator

Same as…

Description

++$x

Pre-increment

Increases xx by one and then returns xx.

$x++

Post-increment

Returns xx, then increases xx by one.

–$x

Pre-decrement

Decreases xx by one and then returns xx.

$x–

Post-decrement

Returns xx, then reduces xx by one.

PHP Logical Operators

PHP logical operators are utilized to combine conditional statements.

Operator

Name

Example

Result

and

And

$x and $y

True if both xx and yy are true.

or

Or

$x or $y

True if either xx or yy is true.

xor

Xor

$x xor $y

True if either xx or yy is true, but not both.

&&

And

$x && $y

True if both xx and yy are true.

||

Or

$x || $y

True if at least one of xx or yy is true.

!

Not

!$x

True if xx is false.

PHP String Operators

PHP has two operators specifically designed for working with strings.

Operator

Name

Example

Result

.

Concatenation

$txt1 . $txt2

The concatenation of txt1\text{txt1} and txt2\text{txt2}.

.=

Concatenation assignment

$txt1 .= $txt2

Appends txt2\text{txt2} to txt1\text{txt1}.

PHP Array Operators

PHP array operators.

Operator

Name

Example

Result

+

Union

$x + $y

The union of xx and yy can be expressed as xy.

 

==

Equality

$x == $y

Returns true if xx and yy contain identical key/value pairs.

===

Identity

$x === $y

Returns true if xx and yy have identical key/value pairs in the same order and of the same types.

!=

Inequality

$x != $y

Returns true if xx is not equal to yy.

<> 

Inequality

$x <> $y

Returns true if xx is different from yy.

!==

Non-identity

$x !== $y

Returns true if xx is not identical to yy.

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to assign a value based on specific conditions.

Operator

Name

Example

Result

?:

Ternary

$x = expr1 ? expr2 : expr3

Returns the value of xx.
The value of xx is expr2\text{expr2} if expr1\text{expr1} is TRUE.
The value of xx is expr3\text{expr3} if expr1\text{expr1} is FALSE.

??

Null coalescing

$x = expr1 ?? expr2

Returns the value of xx.
The value of xx is expr1\text{expr1} if expr1\text{expr1} exists and is not NULL.
If expr1\text{expr1} does not exist or is NULL, the value of xx is expr2\text{expr2}.
This feature was introduced in PHP 7.