Previous | Home | Next |

In all programming languages, The operators are using to manipulated or performing operations on variables and values.

In Operators are using in PHP Programming for many:

- Comparison Operators
- Assignment Operators
- Arithmetic Operators
- String Operators
- Combination Arithmetic & Assignment Operators

### Comparison operators:

In Programming field the Comparisons are using to check the relationship between variables and/or values. The simple example of a comparison operator in action, to check out our If Statement Lesson. Basically Comparison operators are uses by the inside conditional statements and evaluating to either true or false. Here are the most important comparison operators of PHP.

*Assume:* $x = 10 and $y = 20;

- == Equal To
- != Not Equal To
- < Less Than
- > Greater Than
- < = Less Than or Equal To
- > = Greater Than or Equal To

### Assignment operators:

The Assignment operators are using to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character.

### Example:

$my_var = 4; $another_var = $my_var;

Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.

### Arithmetic operators:

- + Addition
- - Minus
- * Multiplication
- / Division
- % Modulus

### Example:

PHP Code: <?php $addition = 20 + 20; $subtraction = 20 - 12; $multiplication = 15 * 13; $division = 15 / 5; $modulus = 7 % 2; echo "Perform addition: 20 + 20 = ".$addition."<br />"; echo "Perform subtraction: 20 - 12 = ".$subtraction."<br />"; echo "Perform multiplication: 15 * 13 = ".$multiplication."<br />"; echo "Perform division: 15 / 5 = ".$division."<br />"; echo "Perform modulus: 7 % 2 = " . $modulus ?>

### String Operator:

The String operator is using in programming for two string are together. the period is the concatenation operator for strings.

PHP Code: <?php $a_string = "the r4rtechsoft"; $another_string = " is the best sites for programming field"; $new_string = $a_string . $another_string; echo $new_string . "!"; ?>

### Combination arithmetic & Assignment operators:

In programming it is a very common task to have to increment a variable by some fixed amount. The most common example of this is a counter. Say you want to increment a counter by 1, you would have:

$counter = $counter + 1;

However, there is a shorthand for doing this.

$counter += 1;

This combination assignment/arithmetic operator would accomplish the same task. The downside to this combination operator is that it reduces code readability to those programmers who are not used to such an operator. Here are some examples of other common shorthand operators. In general, "+=" and "-=" are the most widely used combination operators.

- += Plus Equals
- -= Minus Equals
- *= Multiply Equals
- /= Divide Equals
- %= Modulo Equals
- .= Concatenate Equals

### pre/post-increment & pre/post-decrement

This may seem a bit absurd, but there is even a shorter shorthand for the common task of adding 1 or subtracting 1 from a variable. To add one to a variable or "increment" use the "++" operator:

$x++; Which is equivalent to $x += 1; or $x = $x + 1;To subtract 1 from a variable, or "decrement" use the "--" operator:

$x--; Which is equivalent to $x -= 1; or $x = $x - 1;In addition to this "shorterhand" technique, you can specify whether you want to increment before the line of code is being executed or after the line has executed. Our PHP code below will display the difference.

Previous | Home | Next |