Operators In PHP

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.
  1. <?php      
  2.    //prints 9 (not 14!)      
  3.    print(5 + 2 *2)      
  4.    print("<br>\n");      
  5.       
  6.    //print 3      
  7.    print(6 / 2);      
  8.    print("<br>\n");      
  9.       
  10.    //prints 1      
  11.    print(9 % 2);      
  12.    print("<br>\n");      
  13.       
  14.    //print 35      
  15.    print(" 7 little Indians " * 5);      
  16.    print("<br>\n");      
  17. ?>     
Output
 
9
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.
  1. <?php  
  2.    //Add 8 to Count  
  3.    $Count = 0;  
  4.    $Count = $Count +8;  
  5.   
  6.    //Add 8 to count  
  7.    $count = 0;  
  8.    $Count +=8;  
  9.   
  10.    //prints 13  
  11.    print($a = $b =13);  
  12.    print("<br>\n");  
  13.   
  14.    //prints 8 
  15.    $Count = 3;  
  16.    print($Count +=5);  
  17.    print("<br>\n");  
  18. ?>         
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.
  1. <?php  
  2.    $Query = "SELECT Lastname, FirstName" ,  
  3.    "From Clients"  
  4.    "Where Disposition = 'Pleasant'"  
  5.    "ORDER BY LastName";  
  6.    print($QUERy);  
  7. ?>  

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.