Please note, this is a STATIC archive of website webdevelopmenttutorials.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.

Tutorials

Learn web design and programming with our free video and text tutorials.

Web Designer? Design and market your own professional website with easy-to-use tools.

PHP Operators

In PHP, operators are used to manipulate or perform operations on values and expressions. Operators allow you to assign values to variables, perform arithmetic operations, concatenate strings, compare values and perform Boolean operations.

PHP Assignment Operators

The assignment operator (= or equals), is used to assign a value to a variable or to assign a variable to another variable's value. The assignment operator can also be used in combination with other mathematical operators to change the value of a variable.

= (assigment) operator

The = (assignment) operator sets the value of the first operand to the value of the second operand.

<?php
$x = 1;
echo $x;
?>

The code above outputs:

1

+= (addition-assignment) operator

The += (addition-assigment) operator sets the value of the first operand to its value plus the second value.

<?php
$x = 1;
$x += 2;
echo $x;
?>

The code outputs:

3

-= (subtraction-assignment) operator

The -= (subtraction-assignment) operator sets the value of the first operand to its value minus the secode value.

<?php
$x = 3;
$x -= 2;
echo $x;
?>

The code above outputs:

1

*= (multiplication-assignment) operator

The *= (multiplication-assignment) operator sets the value of the first operand to its value multiplied by the second value.

<?php
$x = 2;
$x *= 2;
echo $x;
?>

The code above outputs:

4

/= (division-assignment) operator

The /= (division-assignment) operator sets the value of the first operancd to its value divided by the second value.

<?php
$x = 4;
$x /= 2;
echo $x;
?>

The code above outputs:

2

.= (concatenation-assignment) operator

The .= (concatenation-assignment) operator sets the value of the first operand to a string containing its value with the second value appended at the end.

<?php
$x = "WebDevelopmentTutorials.com ";
$x .= "is the best.";
echo $x;
?>

The code above outputs:

WebDevelopmentTutorials.com is the best.

%= (modulo-assignment) operator

The %= (modulo-assignment) operator sets the value of the first operand to the remainder of its value divided by the second value.

<?php
$x = 3;
$x %= 2;
echo $x;
?>

The code above outputs:

1

PHP Arithmetic Operators

PHP arithmetic operators are used to perform mathematical operations.

+(addition) operator

The +(addition) operator calculates the sum of 2 values.

<?php
echo 1 + 1;
?>

The code above outputs:

2

-(subtraction) operator

The -(subtraction) operator calculates the difference between 2 values.

<?php
echo 2 - 1;
?>

The code above outputs:

1

*(multiplication) operator

The PHP *(multiplication) operator multiplies 2 values.

<?php
echo 2 * 2;
?>

The code above outputs:

4

/(division) operator

The PHP /(division) operator divides the first value by the second value.

<?php
echo 4 / 2;
?>

The code above outputs:

2

%(modulus) operator

The PHP %(modulus) operator calculates the remainder of the division of the first value by the second.

<?php
echo 3 % 2;
?>

The code above outputs:

1

PHP String Concatenation Operator

The PHP concatenation operator (.) is used to combine 2 sting values to create one string. The concatenation operator us usefull when you are combining strings with values from constants and arrays.

<?php
$variable1="Hello";
$variable2="World";
echo $variable1 . " " . $variable2;
?>

The code above outputs:

Hello World

Notice that the blank space between the . (concatenation) operator and the double quote (") is kept intact.

PHP Increment and Decrement Operators

PHP increment and decrement operators are used to increment and decrement numeric values.

++$value(pre-increment) operator

The PHP ++$value(pre-increment) operator adds 1 to a value before the value is used in the expression in which it is contained.

<?php
$value = 1;
echo ++$value;
?>

The code above outputs:

2

$value++(post-increment) operator

The PHP $value++(post-increment) operator adds 1 to a value after the value is used in the expression in which it is contained.

<?php
$value = 1;
echo $value++;
echo "<br />";
echo $value;
?>

The code above outputs:

1
2

--$value(pre-decrement) operator

The PHP --$value(pre-decrement) operator subtracts 1 from a value before the value is used in the expression in which it is contained.

<?php
$value = 2;
echo --$value;
?>

The code above outputs:

1

$value--(post-drecrement) operator

The PHP $value--(post-drecrement) operator subtracts 1 from a value after the value is used in the expression in which it is contained.

<?php
$value = 2;
echo $value--;
echo "<br />;
echo $value;
?>

The code above outputs:

2
1

PHP Logical Operators

PHP logical operators are used to perform Boolean operations to compare one value to another to determine if a statement is true or false.

and operator

The PHP and operator checks if all values are true.

<?php
$a = 1;
$b = 2;
if (($a == 1) and ($b == 2)) echo "True";
?>

The code above outputs:

True

&& operator

The PHP && operator checks if all values are true. This operator does the same thing as the and operator.

<?php
$a = 1;
$b = 2;
if (($a == 1) && ($b == 2)) echo "True";
?>

or operator

The PHP or operator checks if any values are true.

<?php
$a = 1;
$b = 2;
if (($a == 1) or ($b == 100)) echo "True";
?>

The code above outputs:

True

|| operator

The PHP || operator checks if any values are true. This operator does the same thing as the and operator.

<?php
$a = 1;
$b = 2;
if (($a == 1) || ($b == 100)) echo "True";
?>

The code above outputs:

True

xor

The PHP xor operator checks if only one value is true.

<?php
$a = 1;
$b = 2;
if (($a == 1) xor ($b == 100)) echo "True";
?>

The code above outputs:

True

! operator

The PHP ! operator checks if a statement is true

<?php
$a = 1;
if (!($a == 1)) echo "True";
?>

The code above outputs:

True

PHP Comparison Operators

PHP comparison operators compare values and return Boolean true or false values depending on the result.

== (equal to) operator

The == (equl to) operator checks if the first operand is equal to the second operand.

<?php
$a == 1;
$b == 1;
if ($a == $b) echo "$a is equal to $b.";
?>

The code above outputs:

<?php
1 is equal to 1.
?>

=== (identical to) operator

The PHP === (identical to) operator checks if the first operand has the same value and type as the second operand.

<?php
$a = 1;
$b = 1;
if ($a === $b) echo "1 is identical to 1.";
?>

The code above outputs:

<?php
1 is identical to 1.
?>

!= (not equal to) operator

The PHP != (equal to) operator checks if the first operand is not equal to the second operand.

<?php
$a = 1;
$b = 2;
if ($a != $b) echo "$a is not equal to $b.";
?>

The code above outputs:

1 is not equal to 2.

!== (not identical to) operator

The PHP !== (not identical to) operator checks if the first operand is not equal to the second operand.

<?php
$a = 1;
$b = 2;
if ($a !== $b) echo "$a is not identical to $b.";
?>

The code above outputs:

1 is not equal to 2.

< (less than) operator

The PHP < (less than) operator checks if the first operand is less than the second operand.

<?php
$a = 1;
$b = 2;
if ($a < $b) echo "$a is less than $b.";
?>

The code above outputs:

1 is less than 2.

> (greater than) operator

The PHP > (greater than) operator checks if the first operand is greater than the second operator.

<?php
$a = 2;
$b = 1;
if ($a > $b) echo "$a is greater than $b.";
?>

The code above outputs:

2 is greater than 1.

<= (less than or equal to) operator

The PHP <= (less than or equal to) operator checks if the first operand is less than or equal to the second operand.

<?php
$a = 1;
$b = 20;
if ($a <= $b) echo "$a is less than or equal to $b.";
?>

The code above outputs:

1 is equal to or less than 20.

>= (greater or equal to) operator

The PHP >= (greater or equal to) operator checks if the first operand is greater than or equal to the second operand.

<?php
$a = 5;
$b = 1;
if ($a >= $b) echo "$a is greater than or equal to $b.";
?>

The code above outputs:

5 is greater than or equal to 1.