PHP Operators

PHP has the following types of operators.

  • Arithmetic Operators
  • String Operators
  • Assignment Operators
  • Comparison Operators
  • Bitwise Operators
  • Logical Operators

Arithmetic Operators

Following arithematic operators supported by PHP.

Assume variable A holds 5 and variable B holds 10 then

OperatorDescriptionExample
+Adds two operands A + B will give 15
-Subtracts second operand from the first A - B will give -5
*Multiply both operands A * B will give 50
/Divide numerator by denumerator B / A will give 2
%Modulus Operator and remainder of after an integer division B % A will give 0
++Increment operator, increases integer value by one A++ will give 6
--Decrement operator, decreases integer value by one A-- will give 4

String Operators

In addition to the comparison operators, which can be used on string values, PHP supports two String Operators:

First, the concatenation operator (.) which concatenates two string values together, returning another string that is the union of the two operand strings. For example, "my " . "string" returns the string "my string".

Second, The concatenation assignment operator .= which can also be used to concatenate strings. For example, if the variable firstName has the value "Charlie", then the expression firstName .= " Write" evaluates to "Charlie Write" and assigns this value to firstName.

Assignment Operators

In PHP, an assignment operator assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

Following assignment operators supported by PHP.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
=It assigns values from right side operands to left side operand Z = X + Y will assigne value of X + Y into Z
+=It adds right operand to the left operand and assign the result to left operand. Z += X is equivalent to Z = Z + X
-=It subtracts right operand from the left operand and assign the result to left operand. Z -= X is equivalent to Z = Z - X
*=It multiplies right operand with the left operand and assign the result to left operand. Z *= X is equivalent to Z = Z * X
/=It divides left operand with the right operand and assign the result to left operand. Z /= X is equivalent to Z = Z / X
%=It takes modulus using two operands and assign the result to left operand. Z %= X is equivalent to Z = Z % X

Comparison Operators

In PHP a comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be string,numerical, logical, or object values. Strings are compared on lexicographical ordering, using Unicode values. In all cases, if the two operands are not of the same type, PHP attempts to convert them to an appropriate type for the comparison.

Following comparison operators supported by PHP.

Assume variable A holds 5 and variable B holds 10 then:

OperatorDescriptionExample
== Checks if the value of two operands are equal or not. (A == B) is not true.
!= Checks if the value of two operands are equal or not. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. (A <= B) is true.

Bitwise Operators

In PHP bitwise operators work with 32-bit integers.Bitwise operators perform an operation on the bit (0 or 1).

Following bitwise operators supported by PHP.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
Bitwise ANDIt performs a Boolean AND operation on each bit of its integer arguments. (X & Y) is 2 .
Bitwise ORIt performs a Boolean OR operation on each bit of its integer arguments. (X | Y) is 3.
Bitwise XORIt performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both. (X ^ Y) is 1.
Bitwise NOTIt is a is a unary operator and operates by reversing all bits in the operand. (~Y) is -4 .
Left shiftIt moves all bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. (X << 1) is 4.
Right shiftIt moves all bits in its first operand to the right by the number of places specified in the second operand. The bits filled in on the left depend on the sign bit of the original operand, in order to preserve the sign of the result. (X >> 1) is 1.

Logical Operators

In PHP logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value.

Following logical operators supported by PHP.

Assume variable X holds 5 and variable Y holds 10 then:

OperatorDescriptionExample
&& Known as Logical AND operator. If both the operands are true then then condition becomes true. (X && Y) is true.
||Known as Logical OR Operator. If any of the two operands is true then then condition becomes true. (X || Y) is true.
!Known as Logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(X && Y) is false.
andKnown as Logical AND operator. If the value of both the operands is true then condition becomes true. (X and Y) is true.
orKnown as Logical OR operator. If the value of any of the operands is true then condition becomes true. (X or Y) is true.


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.