## PHP Basics: Operators

In PHP, as in all programming languages, operators manipulate or compare values and variables. Operators are very important to all languages and are used for assigning values to variables, comparing a variable to another variable or a value or manipulating strings. There are five different types of operators:

* Assignment Operators

* Arithmetic Operators

* Comparison Operators

* String Operators

* Combination Arithmetic & Assignment Operators

#### Assignment Operators

The assignment operator is pretty simple as it is just the “=”. For example to give the variable ‘myvar’ a value of 10 you would use:

$myvar = 10;

Now $myvar has a value of 10.

#### Arithmetic Operators

Operator | Description | Example |
---|---|---|

+ | Addition | 2 + 4 |

– | Subtraction | 6 – 2 |

* | Multiplication | 5 * 3 |

/ | Division | 15 / 3 |

% | Modulus | 43 % 10 |

$add = 1+2;

$subtract = 5-1;

$multiply = 5*1

$divide = 12/2;

$modulus = 15%8;

echo “Addition: “.$add.”

“;

echo “Subtraction: “.$subtract.”<br />”;

echo “Multiplication: “.$multiply .”<br />”;

echo “Division: “.$divide .”<br />”;

echo “Modulus : “.$modulus .”<br />”;

Will output the following:

Subtraction: 4

Multiplication: 5

Division: 6

Modulus : 7

#### Comparison Operators

Comparison operators are used for conditional statements to compare a variable to a value or a variable to a variable. The comparison will evaluate to either True or False.

Assume: $x = 4 and $y = 5;

Operator | English | Example | Result |
---|---|---|---|

== | Equal To | $x == $y | false |

!= | Not Equal To | $x != $y | true |

< | Less Than | $x < $y | true |

> | Greater Than | $x > $y | false |

<= | Less Than or Equal To | $x <= $y | true |

>= | Greater Than or Equal To | $x >= $y | false |

#### String Operators

The period “.” is the concatenation operator which is used to combine two strings together.

$string2 = “combined with another string “;

echo $string1.$string2.”and yet another string”;

Will output the following:

#### Combination Arithmetic & Assignment Operators

The operators are useful for incrementing a value such as with a counter. For example if you wanted to increase a count by one you would do:

$count = $count+1;

However an easier way to do this would be to use a combination operator:

$count += 1;

While saving 4 characters of code may not seem like much here it adds up over a project and can make it easier to read unless the reader is not familiar with these operators.

Operator | English | Example | Equivalent Operation |
---|---|---|---|

+= | Plus Equals | $x += 2; | $x = $x + 2; |

-= | Minus Equals | $x -= 4; | $x = $x – 4; |

*= | Multiply Equals | $x *= 3; | $x = $x * 3; |

/= | Divide Equals | $x /= 2; | $x = $x / 2; |

%= | Modulo Equals | $x %= 5; | $x = $x % 5; |

.= | Concatenate Equals | $my_str.=”hello”; | $my_str = $my_str . “hello”; |

Source: http://www.tizag.com/phpT/operators.php

Thank you for reading. PHPinformation.org, your home for quality PHP Information.

## Hi, Stranger! Leave Your Comment...

You must be logged in to post a comment.