Almost Everything about Operators in PHP – Part VI

In the previous part of the course, we have learned about error reporting and various decision making statements available in PHP. But that is obviously not enough for us to create useful and dynamic applications as we need to learn about various operators available in PHP and how to make proper use of them to bring various actions into play.

You should be a bit familiar with the word “operator” as I have already used it while discussing about concatenation.

In this part of the course, we will learn almost everything we can do with operators in PHP. But before moving on to the various types of operators, let us learn what “operators” actually mean. Operators are language constructs (composed of special characters or combination of characters) which behaves typically like functions but differ syntactically and semantically from ordinary functions.

They can be used to perform various nifty “operations” quickly and easily, and that’s why they are known as “Operators”.

Let us take the simple example of an addition operation – 10 + 5 = 15. It is clear from the statement, that the operation being performed here is a simple addition operation, but how do we really understand or identify the type of the operation being performed?

It is from the “+” (plus symbol) used between 10 and 5. Thus here, the “+” (plus symbol) is the operator (capable of performing addition operation) and the operands are 10 and 5 (on which the operator operates or thee operation is being performed).

Unary, Binary and Ternary Operators in PHP

Depending on the number of operands an “operator” operates on, they can be classified into three categories –

1)      Unary Operators – The operators which operates only on a single operand (or variable) are known as Unary operators.

For example, “!” (negation), “++” (increment by 1) and “—“ (decrement by 1).

2)      Binary Operators – The operators which operates on two operands (which are either variables or constants) are known as Binary Operators.

For example, “+” (addition), “-“ (subtraction), “=” (assignment) are some of the many binary operators available.

3)      Ternary Operators – The operator which operates on three operands (which can be anything from variables, constants to boolean values) is known as Ternary Operator.

I know of only one ternary operator in PHP which is known as “conditional operator” and can also be used to reduce an if-else statement (which you have learned in the previous part) to just a single line. Let us discuss the ternary operator in a bit more detail with the help of an example (the other operators will be discussed in detail just a few moments later).

The syntax of the ternary operator looks something like this,

 CONDITION ? TRUE : FALSE;

The ternary operator will either return the first value (after the “?” symbol) if the condition is true or the second value (after the “:”) if the condition is false.

For example,

[php] <?php

$age = 10;

$status = ($age <18) ? "Child" : "Adult";

echo $status;

?> [/php]

In his example, as the condition turns out to be true, so “Child” is assigned to the $status variable which is later echoed out. We could have also performed this exact same operation using if-else statements that could have taken more than 4 lines of code (which is now reduced to 1 only).

Types of Operators in PHP

Now we would be discussing about the operators in more detail and with the help of examples. Here we are classifying the variables on the basis of the types of operations being performed.

1)      Assignment Operators

2)      Arithmetic Operators

3)      Comparison Operators

4)      Logical (or Relational) Operators

5)      Conditional (or Ternary) Operators (which have already been discussed above)

Assignment Operators

There are 6 assignment operators which are supported in PHP to assign a value to a particular variable.

i)    “=” – Simple Assignment

ii)    “+=” – Addition & Assignment Operator

iii)    “-=” – Subtraction & Assignment Operator

iv)    “*=” – Multiplication & Assignment Operator

v)    “/=” – Division & Assignment Operator

vi)    “%=” – Modulus & Assignment Operator

Let us illustrate this with the help of an example,

[php] <?php

$x = 100;

$y = 50;

&nbsp;

$z = $x + $y;

echo "Result of Addition Operation (Simple Assignment) is: $z </br>";

$z += $x;

echo "Result of Addition & Assignment Operation is: $z </br>";

$z -= $x;

echo "Result of Subtraction & Assignment Operation is: $z </br>";

$z *= $x;

echo "Result of Multiplication & Assignment Operation is: $z </br>";

$z /= $x;

echo "Result of Division & Assignment Operation is: $z </br>";

$z %= $x;

echo "Result of Modulus & Assignment Operation is: $z </br>";

?> [/php]

assignment-operators

Arithmetic Operators

There are 7 arithmetic operators which are supported in PHP to perform various arithmetic operations.

i)    “+” – Addition Operation

ii)    “-“ – Subtraction Operation

iii)    “*” – Multiplication Operation

iv)    “/” – Division Operation

v)    “%” – Modulus Operation

vi)    “++” – Increment Operator

vii)    “- -“ – Decrement Operator

Let us illustrate this with the help of an example –

[php] <?php

$x = 100;

$y = 50;

&nbsp;

$z = $x + $y;

echo "Result of Addition Operation is: $z </br>";

$z = $x – $y;

echo "Result of Subtraction Operation is: $z </br>";

$z = $x * $y;

echo "Result of Multiplication Operation is: $z </br>";

$z = $x / $y;

echo "Result of Division Operation is: $z </br>";

$z = $x % $y;

echo "Result of Modulus Operation is: $z </br>";

$z = $x++;

echo "Result of Increment Operation is: $z </br>";

$z = $x–;

echo "Result of Decrement Operation is: $z </br>";

?> [/php]

arithmetic-operators

Comparison Operators

There are 6 comparison operators which are supported in PHP to compare one value with another and take various decisions on the basis of that.

i)    “==” – Equality

ii)    “!=” – Inequality

iii)    “>” – Greater than

iv)    “<” – Less than

v)    “>=” – Greater than or equal to

vi)    “<=” – Less than or equal to

Let us illustrate this with the help of an example,

[php] <?php

$x = 100;

$y = 50;

&nbsp;

if( $x == $y ){

echo "x is equal to y.<br/>";

}else{

echo "x is not equal to y.<br/>";

}

if( $x != $y ){

echo "x is not equal to y.<br/>";

}else{

echo "x is equal to y.<br/>";

}

if( $x > $y ){

echo "x is greater than  y.<br/>";

}else{

echo "x is not greater than y.<br/>";

}

if( $x < $y ){

echo "x is less than  y.<br/>";

}else{

echo "x is not less than y.<br/>";

}

if( $x >= $y ){

echo "x is either greater than or equal to y.<br/>";

}else{

echo "x is neither greater than nor equal to y.<br/>";

}

if( $x <= $y ){

echo "x is either less than or equal to y.<br/>";

}else{

echo "x is neither less than nor equal to y.<br/>";

}

?> [/php]

conditional-operators

Logical (or Relational) Operators

There are 5 logical operators supported in PHP which are used to perform various logical decisions.

i)    “&&” – Logical AND Operator

ii)    “||” – Logical OR Operator

iii)    “!” – Logical NOT Operator

iv)    “and” – Logical AND Operator

v)    “or” – Logical OR Operator

Let us illustrate this with the help of an example,

[php] <?php

$x = 10;

$y = 0;

&nbsp;

if( $x && $y ){

echo "Both x and y are true.<br/>";

}else{

echo "Either x or y or both are false.<br/>";

}

&nbsp;

if( $x || $y ){

echo "Either x or y is true.<br/>";

}else{

echo "Both x and y are false.<br/>";

}

&nbsp;

if( !$a ){

echo "x is false.<br/>";

}else{

echo "x is true.<br/>";

}

&nbsp;

if( $x and $y ){

echo "Both x and y are true.<br/>";

}else{

echo "Either x or y is false.<br/>";

}

&nbsp;

if( $x or $y ){

echo "Either x or y is true.<br/>";

}else{

echo "Both x and y are false.<br/>";

}

?> [/php]

logical-operators

The Final Words

In this part of the course, we have learned almost everything about various operators and how they work in PHP. But I advise you to go and practice a bit with these operators to make your foundation even stronger.

You should obviously go and create a simple calculator application using some arithmetic and assignment operators or can also try to create a simple application generating a student’s report card based on his/her marks. These are just some of the most general suggestions which you should try out, but you can also expand using your own creativity.

The next part is going to be even more interesting as we are going to learn about various looping constructs available in PHP and how to make proper use of them. We will also learn about various interesting and cool things we can do with loops and how important they are. See you in the next part of the course. Till then happy coding.

STAY UP TO DATE

Sign up today to stay informed with industry news & trends

[mc4wp_form id=26062]