Introduction
An operator is a symbol that is used to perform some operation between two operands. The $ operator lets PHP know you are using a variable. Most operators work on 2 operands and it is called binary operators. Others operate on only 1 operand and it is referred to as unary operator. PHP also has one operator that works with three operands and it is referred as ternary operartor. With some exception most operations fall into five categories: arithmetic, logical, bitwise, assignment, control.
Arithmetic Operators
Addition, subtration, multiplication, and division are performed by the arithmetic operators. They may be applied to any numbers, including integers and floating point numbers and decimal values. PHP first converts them to numeric values and then performs the operation. The result type of an arithmetic expression can be either an integer or floating-point numbers. PHP determines the result type based on whether a decimal point is necessary to describe the result or not.
- <?php
-
- print(5 + 2 *2)
- print("<br>\n");
-
-
- print(6 / 2);
- print("<br>\n");
-
-
- print(9 % 2);
- print("<br>\n");
-
-
- print(" 7 little Indians " * 5);
- print("<br>\n");
- ?>
Output
9
3
1
35
The increment and decrement operators are shorthand for adding or else subtracting 1 from a variable. They cannot be used with anything other than a variable, so something like 5++ is illegal. These operators which work for integers are floating point numbers. The increment operators also work with strings. PHP increments the last character in the string to the next character in the character set. Decrement operators do not work with strings, but they do not produe an error.
Assignment operator
There is only one assignment operator, but PHP offers a handful of shortcut operators for combining with another operator, and it is referred as assign-op.= - Assign right side to left side. += - Add right side to left side. -= -subtract right side from left side. All the assignment operators put a value into a variable and each variable has a unique name. They put values on the right side into a variable on the left side. The operators with an assignment operator on both the right and left side then put the result in the variable on the left.
- <?php
-
- $Count = 0;
- $Count = $Count +8;
-
-
- $count = 0;
- $Count +=8;
-
-
- print($a = $b =13);
- print("<br>\n");
-
-
- $Count = 3;
- print($Count +=5);
- print("<br>\n");
- ?>
Assignment operator resolves to the value being assigned. This allows us to use an assignment expression.The value of the count is assigned in two ways.
Logical and Relational Operators
Relational operators compare values and return either TRUE or FALSE. Logical operators are converted into booleans prior to being evaluated. Empty strings are considered to be False, and any non empty string is True. < - is less than,> - is greater than, <= -is less than or equal to.
Bitwise Operator
Bitwise operators are similar to logical operators, except they perform on the binary representation of their arguments. In case both the arguments are strings, the operation is performed between parallel character offsets, and the result is stored in the same offset in the result string. When using logical operator 1 and 10 are both considered TRUE. A logical AND of 1 and 10 results TRUE. & - AND, | - OR,>> - Shift all bits to the right , << - Shift all bits to the left.
Concatenation operator
Curly braces ({ and }) group variables as parenthesis are used for arithmetic. This eliminates the ambiguity that can arise when referencing variables. They allow us to specify elements of multidimensional arrays inside strings.
- <?php
- $Query = "SELECT Lastname, FirstName" ,
- "From Clients"
- "Where Disposition = 'Pleasant'"
- "ORDER BY LastName";
- print($QUERy);
- ?>
Silence Operator
The ? operator is equivalent to an if statement. It is called a ternary operator because it take three parameters: an expression that is evaluated if the first is TRUE, and an expression that is evauated if the first is FALSE.