THE WORLD'S LARGEST WEB DEVELOPER SITE

PHP7 Tutorial

PHP7 HOME PHP7 Intro PHP7 Install PHP7 Syntax PHP7 Variables PHP7 Echo / Print PHP7 Data Types PHP7 Strings PHP7 Constants PHP7 Operators PHP7 If...Else...Elseif PHP7 Switch PHP7 While Loops PHP7 For Loops PHP7 Functions PHP7 Arrays PHP7 Sorting Arrays PHP7 Superglobals

PHP7 Forms

PHP7 Form Handling PHP7 Form Validation PHP7 Form Required PHP7 Form URL/E-mail PHP7 Form Complete

PHP7 Advanced

PHP7 Arrays Multi PHP7 Date and Time PHP7 Include PHP7 File Handling PHP7 File Open/Read PHP7 File Create/Write PHP7 File Upload PHP7 Cookies PHP7 Sessions PHP7 Filters PHP7 Filters Advanced

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP7 XML

PHP7 XML Parsers PHP7 SimpleXML Parser PHP7 SimpleXML - Get PHP7 XML Expat PHP7 XML DOM

PHP7 - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP7 Reference

PHP7 Overview PHP7 Array PHP7 Calendar PHP7 Date PHP7 Directory PHP7 Error PHP7 Filesystem PHP7 Filter PHP7 FTP PHP7 Libxml PHP7 Mail PHP7 Math PHP7 Misc PHP7 MySQLi PHP7 Network PHP7 SimpleXML PHP7 Stream PHP7 String PHP7 XML Parser PHP7 Zip PHP7 Timezones

PHP 7 Operators


PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

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

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result Show it
+ Addition $x + $y Sum of $x and $y Try it »
- Subtraction $x - $y Difference of $x and $y Try it »
* Multiplication $x * $y Product of $x and $y Try it »
/ Division $x / $y Quotient of $x and $y Try it »
% Modulus $x % $y Remainder of $x divided by $y Try it »
** Exponentiation $x ** $y Result of raising $x to the $y'th power Try it »

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as... Description Show it
x = y x = y The left operand gets set to the value of the expression on the right Try it »
x += y x = x + y Addition Try it »
x -= y x = x - y Subtraction Try it »
x *= y x = x * y Multiplication Try it »
x /= y x = x / y Division Try it »
x %= y x = x % y Modulus Try it »


PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result Show it
== Equal $x == $y Returns true if $x is equal to $y Try it »
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type Try it »
!= Not equal $x != $y Returns true if $x is not equal to $y Try it »
<> Not equal $x <> $y Returns true if $x is not equal to $y Try it »
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type Try it »
> Greater than $x > $y Returns true if $x is greater than $y Try it »
< Less than $x < $y Returns true if $x is less than $y Try it »
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y Try it »
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y Try it »
<=> 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. Try it »

PHP Increment / Decrement Operators

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

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

Operator Name Description Show it
++$x Pre-increment Increments $x by one, then returns $x Try it »
$x++ Post-increment Returns $x, then increments $x by one Try it »
--$x Pre-decrement Decrements $x by one, then returns $x Try it »
$x-- Post-decrement Returns $x, then decrements $x by one Try it »

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result Show it
and And $x and $y True if both $x and $y are true Try it »
or Or $x or $y True if either $x or $y is true Try it »
xor Xor $x xor $y True if either $x or $y is true, but not both Try it »
&& And $x && $y True if both $x and $y are true Try it »
|| Or $x || $y True if either $x or $y is true Try it »
! Not !$x True if $x is not true Try it »

PHP String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result Show it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2 Try it »
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1 Try it »

PHP Array Operators

The PHP array operators are used to compare arrays.

Operator Name Example Result Show it
+ Union $x + $y Union of $x and $y Try it »
== Equality $x == $y Returns true if $x and $y have the same key/value pairs Try it »
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types Try it »
!= Inequality $x != $y Returns true if $x is not equal to $y Try it »
<> Inequality $x <> $y Returns true if $x is not equal to $y Try it »
!== Non-identity $x !== $y Returns true if $x is not identical to $y Try it »

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

Operator Name Example Result Show it
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE
Try it »
?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7
Try it »