Use PHP Operators

Operators are generally used in performing the operations on variables and values.

Let’s learn all the operators one by one.

PHP Arithmetic Operators:

As the name says, the PHP arithmetic operators perform a mathematical operation such as addition, subtraction, and multiplication etc.

Here is the List of Arithmetic Operators

1) Plus Sign (+): The plus sign operator is used to perform addition of two value.

For example in the below statement. We are adding two value $x and $y to sum them up. We have defined the values to the variables $x and $y and in echo statement, we have use addition arithmetic operators to sum them up.

[php]<?php
$x = 10;
$y = 6;

echo $x + $y;
?> [/php]

The outcome of the above php script will be as below.

16

2) Minus Sign (-): The minus sign operator is used to perform to deduct the value from one variable to another.

In the below code we are deducting $y from $x by using subtract operator.

[php]<?php
$x = 10;
$y = 6;

echo $x – $y;
?> [/php]

The script will subtract the value and produce below output.

4

3) Multiplication

The Multiplication operator is used to multiply the two values.

[php]<?php
$x = 10;
$y = 6;

echo $x * $y;
?> [/php]

In the above code, we are multiplying the two value.

The outcome of the above script will look like this.

60

4) Division

The Division arithmetic operator is used to divide the one value from other.

[php]<?php
$x = 10;
$y = 6;

echo $x / $y;
?> [/php]

The above script will show the below result.

1.66666666667

5) Modulus

The modulus is used to find the root of the value.

[php]<?php
$x = 10;
$y = 6;

echo $x % $y;
?> [/php]

The above script will produce the below result.

4

PHP Assignment Operators

As the name states, the assignment operator assigns the value of the one variable to another. You can use numeric values to assign the value to the variable.

1) Equal to sign (=): The equal to sign is used to show the two variables are equal to other variable value.

[php]<?php
$x = 10;
echo $x;
?> [/php]

In the above php script, the $x variable has given the value 10 and it represents the equal to sign (=).

The output of the above script will be.

10

2) Plus equal to (+=): The plus equal to sign is used to add the value of the first variable show the final output.

[php]<?php
$x = 20;
$x += 100;

echo $x;
?> [/php]

In the above example we have given $x value of 20 and on the next line, the $x is added (+=) 100 value. When we run the script it will show the following result.

120

3) Subtraction (-=): The subtraction is used to subtract the value of the first variable to the second variable.

[php]<?php
$x = 50;
$x -= 30;

echo $x;
?> [/php]

In the above script, we are subtracting $x value which is 50 from 30. The final result of the above script will show the below result.

20

4) Multiplication (*=): The multiplication operator is used to multiply the two variables.

[php]<?php
$x = 10;
$x *= 6;

echo $x;
?> [/php]

In the above script, we are multiplying the two values of the variable to get the result. The above script will show result.

60

5) Division (/=): This operator is used to divide the two value from each other.

[php]<?php
$x = 10;
$x /= 5;

echo $x;
?> [/php]

In the script, we are dividing two value of the $x from each other to get the below result.

2

6) Modulus (%=): The modulus operate gives you outcome of the module of two variable.

[php]<?php
$x = 15;
$x %= 4;

echo $x;
?> [/php]

In this script, you get the following result.

3

PHP Comparison Operators

The php comparison operators are used to compare the two values such as numbers and string.

1) Equal to (==): It returns true value if both variable are similar to each other.

[php]<?php
$x = 100;
$y = "100";

var_dump($x == $y); // returns true because values are equal
?> [/php]

In the above code, the value of $x is number 100 and the $y has value string 100. The result of the above script will as shown below.

bool(true)

In this, the bool stands for boolean search which indicates the value is true or false. In our case it is true because two of the match together. The Value of the outcome is mentioned in the bracket (true). The var_dump() function is used to display structured information of the type and value of the particular variable.

2) Identical Value (===): When you want to compare the two value with each other by knowing the type of the variable, you use the Identical operator.

[php]<?php
$x = 100;
$y = "100";

var_dump($x === $y); // returns false because types are not equal
?> [/php]

In the above case, the two value $x and $y have two different types. The $x represent pure number and $y is a string. It means both are not of the same type. So when we run the script it will show false value.

The outcome of the above script is as below.

bool(false)

3) Not Equal: (!=): The operator checks whether the value of the first variable is not equal to the second variable.

[php]<?php
$x = 100;
$y = "100";

var_dump($x != $y); // returns false because values are equal
?> [/php]

In the above case, the value of $x is equal to $y so the result will show the false statement. When you use the not equal to sign the value should not match. The result of above statement will be as given below.

bool(false)

4) Not equal (<>): This operator also performs similarly to above. When using in the statement, it will check both the condition and return result true or false on the base of the outcome.

[php]<?php
$x = 100;
$y = "100";

var_dump($x <> $y); // returns false because values are equal
?> [/php]

In the above statement then it will show the below result.

bool(false)

It is showing true because both the variable matches the value and in this condition, it should not match.

5) Not identical (!==): The operator checks the type of the two values. If both the type are identical number or string then it will return the true else false.

[php]<?php
$x = 100;
$y = "100";
var_dump($x !== $y); // returns true because types are not equal
?> [/php]

The result of the above will look like this.

bool(true)

6) Greater than (>): The greater then operator compare show the one value is greater than other value and return true if the condition is true else false.

[php]<?php
$x = 100;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?> [/php]

The outcome of above will look like this.

bool(true)

7) Less than (<): The operator is used to describe the less than the value of the two variables. If the condition is true the outcome of the variable will be true else it will show false statement.

[php]<?php
$x = 10;
$y = 50;

var_dump($x < $y); // returns true because $x is less than $y
?> [/php]

Above will show the result.

bool(true)

8) Greater than or equal to (>=): The operator is used to describe the two values.

[php]<?php
$x = 50;
$y = 50;

var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?> [/php]

The above will show the result.

bool(true)

9) Less than or equal to (<=): The operator checks whether the first entry is less than or equal to the second entry. If the entry matches then it will show the value true else false.

[php]<?php
$x = 50;
$y = 50;

var_dump($x <= $y); // returns true because $x is less than or equal to $y
?> [/php]

Above will show the result.

bool(true)

PHP Increment / Decrement Operators

The php increment operators are used to increase the variable’s value when needed.

The Decrement operators work completely opposite. It decrements variable values.

1) Pre-increment (++$x): The Pre-increment value increment the value first and then run the program.

[php]<?php
$x = 10;
echo ++$x;
?> [/php]

The above will generate an outcome.

11

2) Post-increment ($x++): The post increment returns the $x value then increment further till it ask for stop.

[php]<?php
$x = 10;
echo $x++;
?> [/php]

The above will show this result.

10

3) Pre-decrement (–$x): The Pre Decrement operator decrease the $x value by one and then return the $x.

[php]<?php
$x = 10;
echo –$x;
?> [/php]

The above will show result.

9

4) Post-decrement ($x–): The Post decrement operator decrease the $x value after it is rendered and decrease the value by one.

[php]<?php
$x = 10;
echo $x–;
?> [/php]

The above will show the result.

10

PHP Logical Operators

The php logical operators are used to combine two conditional statement and verify the given condition whether it is true or false.

1) And: The AND operator return value true if both $x and $y are true.

[php]<?php
$x = 100;
$y = 50;

if ($x == 100 and $y == 50) {
echo "Hello world!";
}
?> [/php]

In the above statement, the value of $x and value of $y is checked by using if statement. If the value is similar then it will echo the true statement. In our case, it will return “Hello World!”;

As above statement is true, We will get the true result as shown below.

Hello, world!

2) Or: The above statement will return true if either $x or $y is true. Any of them should have the specified value.

[php]<?php
$x = 100;
$y = 50;

if ($x == 100 or $y == 80) {
echo "Hello world!";
}
?> [/php]

In the above script the if statement checks the if condition to find the value of $x or the value of $y. If any of them matches the value, it will return the echo statement which is “Hello World”.

3) Xor: In the Xor statement the php script checks whether the $x or the $y is true but not the both. If the any one of them matches then it will return a true value.

[php]<?php
$x = 100;
$y = 50;

if ($x == 100 xor $y == 80) {
echo "Hello world!";
}
?> [/php]

In the above script, the script checks the value of both the condition and return the echo statement if it matched the condition.

4) &&: It is similar to the AND operator. It returns true if both the statements are true.

[php]<?php
$x = 100;
$y = 50;

if ($x == 100 && $y == 50) {
echo "Hello world!";
}
?> [/php]

The above statement will return the echo statement “Hello World”;

5) ||: It is similar to OR operator. It checks the statement for one of the value is true and return the echo statement if it matched any of them.

[php]<?php
$x = 100;
$y = 50;

if ($x == 100 || $y == 80) {
echo "Hello world!";
}
?> [/php]

Return the result.

Hello world!

6) Not:
This operator checks whether the $x is not true and then return the echo statement.

[php]<?php
$x = 100;

if ($x !== 90) {
echo "Hello world!";
}
?> [php]

Return the value.

Hello world!

<strong>PHP String Operators</strong>

PHP has made two special operators for the strings usage.

<strong>1) Concatenation:</strong> The concatenation operator is used when you are echoing the two string value at the time. If you place the variable as it is in the echo statement then it will produce an error message. You need to use the concatenation operator to separate them.

[php]<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?> [/php]

The above script shows the below result.

Hello world!

2) Concatenation assignment: This operator also uses to present the variable without interacting them with each other.

[php]<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?> [/php]

The above will generate the following result.

Hello world!

PHP Array Operators

The php array operators are used to compare two arrays with each other.

1) Union: It creates a union of two array values and present as single array statement.

[php]<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");

print_r($x + $y); // union of $x and $y
?> [/php]

The result of the above statement.

Array ( [a] => red [b] => green [c][/c] => blue [d] => yellow )

2) Equality: It returns true if both array key and values match with each other.

[php]<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");

var_dump($x == $y);
?> [/php]

The result of the above statement.

bool(false)

3) Identity; The operator returns true if the key and values are the same and it is aligned in the same order.

[php]<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");

var_dump($x === $y);
?> [/php]

The result of the above statement.

bool(false)

4) Inequality (!=) : It returns true if $x is not equal to $y.

[php]<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");

var_dump($x != $y);
?> [/php]

The result of the above statement.

bool(true)

5) Inequality (<>) : It returns true if $x is not equal to $y.

[php]<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");

var_dump($x <> $y);
?> [/php]

The result of the above statement.

bool(true)

6) Non-identity (!==) : Returns true if $x is not identical to $y,

[php]<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");

var_dump($x !== $y);
?> [/php]

The result of the above statement.

bool(true)

Related Posts

Leave a Reply