Verification: a143cc29221c9be0

Operators and functions in php

PHP Arithmetic Operators

This is almost the same as the basic arithmetic operators you learned in your school.

PHP Assignments Operators

The basic assignment operator in PHP is =. It is actually not the "equal" mark in PHP. It works as assignment operator, which assigns a value to a variable.

PHP Basic Assignment Operator Example


Run Example ››

In this example,

  • 5 is assigned to $a.
  • 7 is assigned to $b.
  • Then, the value of $b (7) is assigned to $a.

There are some other assignment operators to learn.

Operator Example Long Form Description
= $a = $b $a = $b $a gets set to the value of $b Run Example ››
+= $a += $b $a = $a + $b $a gets set to $a + $b (Addition) Run Example ››
-= $a -= $b $a = $a - $b $a gets set to $a - $b (Subtraction) Run Example ››
*= $a *= $b $a = $a * $b $a gets set to $a * $b (Multiplication) Run Example ››
/= $a /= $b $a = $a / $b $a gets set to $a / $b (Division) Run Example ››
%= $a %= $b $a = $a % $b $a gets set to $a % $b (Modulus) Run Example ››
**= $a **= $b $a = $a ** $b $a gets set to $a ** $b (Exponentiation) Run Example ››

PHP Comparison Operators

Comparison Operators allow you to compare two values.

Operator Name Example Result
== Equal $a == $b True if values of $a and $b are equal Run Example ››
=== Identical $a === $b True if both values and data types of $a and $b are equal Run Example ››
!= Not equal $a != $b True if $a is not equal to $b Run Example ››
Not equal $a $b True if $a is not equal to $b Run Example ››
!== Not identical $a !== $b True if $a is not equal to $b or they are not in the same data type Run Example ››
> Greater than $a > $b True if $a is greater than $b Run Example ››
Less than $a True if $a is less than $b Run Example ››
>= Greater than or equal to $a >= $b True if $a is greater than or equal to $b Run Example ››
Less than or equal to $a True if $a is less than or equal to $b Run Example ››

!== and have no difference. Both work in the same way.

PHP Logical Operators

A logical operator is a kind used in logic. In PHP, all logical operators return a boolean value.

Note: We will discuss more about the if blocks we have used in this examples in the next lesson.

PHP Incrementing/Decrementing Operators

PHP's incrementing and decrementing operators are used to increment and decrement values respectively.

Note:

  • Only strings and numbers (integers and floats) are affected by these operators.
  • Arrays and objects are not affected.
  • Decrementing null has no effect, but incrementing results in 1.
Example Name Result
++$a Pre-increment Increments $a by one, then returns $a Run Example ››
$a++ Post-increment Returns $a, then increments $a by one. Run Example ››
--$a Pre-decrement Decrements $a by one, then returns $a Run Example ››
$a-- Post-decrement Returns $a, then decrements $a by one. Run Example ››

PHP String Operators

There are two special string operators what help us to operate strings.

Operator Name Example Result
. Concatenation $a . $b Returns the concatenation of its right and left arguments Run Example ››
.= Concatenation Assignment $a .= $b $a gets set to $a . $b Run Example ››

PHP Array Operators

PHP array operators are used to perform operations on arrays.

Operator Name Example Result
+ Union $a + $b Returns the union of $a and $b arrays. Run Example ››
+= Union Assignment $a += $b $a gets set to $a + $b Run Example ››
== Equal $a == $b True if $a and $b has equal key/value pairs Run Example ››
=== Identical $a === $b True if $a and $b has equal key/value pairs in the same order and of same data type. Run Example ››
!= Not equal $a != $b True if $a and $b are not equal. Run Example ››
Not equal $a $b True if $a and $b are not equal. Run Example ››
!== Not identical $a !== $b True if $a and $b has different orders, different key/value pairs or values of different data types. Run Example ››