Difference between revisions of "PHP Operators"
(→PHP Arithmetic Operators) |
(→Assignment Operators) |
||
Line 3: | Line 3: | ||
Operators in PHP work with ''operands'' that specify the variables and values that are to be used in the particalar operation. The number and location of tyhese operands in relate to the operators (ie before and/or after the operator) depends on the type of operator in question. In this chapter of PHP Essentials we will exlore each type of operator and explain how they are used in relation to their operands. | Operators in PHP work with ''operands'' that specify the variables and values that are to be used in the particalar operation. The number and location of tyhese operands in relate to the operators (ie before and/or after the operator) depends on the type of operator in question. In this chapter of PHP Essentials we will exlore each type of operator and explain how they are used in relation to their operands. | ||
− | == Assignment Operators == | + | == PHP Assignment Operators == |
The ''assignment operator'' is used to assign a value to a variable and is represented by the equals (=) sign. The assignment operator can be combined with arithmetic operators to combine an assignment with a mathemtatical operation (for example to multiple one value by another and assigning the result to the variable. | The ''assignment operator'' is used to assign a value to a variable and is represented by the equals (=) sign. The assignment operator can be combined with arithmetic operators to combine an assignment with a mathemtatical operation (for example to multiple one value by another and assigning the result to the variable. |
Revision as of 20:01, 29 May 2007
Opertors in PHP, and any other prgramming language for that matter, enable us to perfrom tasks on variables and values such as assign, multiply, add, subtract and concatenate them. Operators take the form of symbols (such as + and -) and combinations of symbols (such as ++ and +=).
Operators in PHP work with operands that specify the variables and values that are to be used in the particalar operation. The number and location of tyhese operands in relate to the operators (ie before and/or after the operator) depends on the type of operator in question. In this chapter of PHP Essentials we will exlore each type of operator and explain how they are used in relation to their operands.
PHP Assignment Operators
The assignment operator is used to assign a value to a variable and is represented by the equals (=) sign. The assignment operator can be combined with arithmetic operators to combine an assignment with a mathemtatical operation (for example to multiple one value by another and assigning the result to the variable.
The following table lists the seven assigmnet operators available in PHP, togetehr with descriptions and examples of their use:
Operator | Type | Description | Example |
---|---|---|---|
= | Assignment | Sets the value of the left hand operand to the value of the right | $myVar = 30; |
+= | Addition-Assignment | Adds the value of left hand operand to the value of the right hand operand and assigns result to left hand operand | $myVar = 10; $myVar += 5; |
-= | Subtraction-Assignment | Subtracts the value of right hand operand from the value of the left hand operand and assigns result to left hand operand | $myVar = 10; $myVar -= 5; |
*= | Multiplication-Assignment | Multiplies the left hand operand by value of the right hand operand assigning result to left hand operand | $myVar = 10; $myVar *= 5; |
/= | Division-Assignment | Divides the left hand operand by value of the right hand operand assigning result to left hand operand | $myVar = 10; $myVar /= 5; |
%= | Modulo-Assignment | Sets the value of the left hand operand to the remainder of the value after being divided by the right hand operand | $myVar = 10; $myVar %= 5; |
.= | Concatenation-Operand | Sets the value of the left hand operand to a string containing a concatenation of its value appended with the string in the right hand operand | $sampleString="My color is "; $sampleString += "blue"; |
PHP Arithmetic Operators
As the name suggests PHP arithmetic operators provide a mecahnism for performing mathematical operations:
Operator | Type | Description | Example |
---|---|---|---|
+ | Addition | Calculates the sum of two operands | $total = 10 + 20; |
- | Subtraction | Calculates the difference between two operands | $total = 10 - 20; |
* | Multiplication | Multiplies two operands | $total = 10 * 20; |
/ | Division | Divides two operands | $total = 10 / 20; |
% | Modulus | Returns the reminader from dividing the first operand by the second | $total = 20%10; |