Operators in PHP
operator is a character that represents an action or a process. Operators take one or more values, process them, and produce results. It is used to operate on the variables.
Types of Operators
1. Arithmetic operators
Arithmetic operators are used to perform arithmetic operations. Example addition, subtraction, multiplication etc
Operator | Description | Example |
---|---|---|
+ | Addition Operator, Adds two operands | $A +$B |
– | Subtraction Operator, Subtracts second operand from the first | $A – $B |
* | Multiplication Operator, Multiply both operands | $A * $B |
/ | Division Operator, Divide numerator by d enumerator | $B / $A |
% | Modulus Operator and the remainder of after an integer division | $B % $A |
++ | The increment operator increases the integer value by one | $A++ |
— | The decrement operator decreases the integer value by one | $A– |
2. Comparison Operators
Comparison operators are used to compare the two variables. The following comparison operators are supported by PHP
Let A=10 and B=50
Operator | Description | Example |
---|---|---|
== | Equal operator, It checks if the value of two operands are equal or not, if yes then it becomes true. | ($A == $B) is not true. |
!= | Not equal to, It checks if the value of two operands are equal or not, if values are not equal then it becomes true. | ($A != $B) is true. |
> | Greater than, It checks if the value of the left operand is greater than the value of the right operand, if yes then it becomes true. | ($A > $B) is not true. |
< | Less than operator, It checks if the value of the left operand is less than the value of the right operand, if yes then it becomes true. | ($A < $B) is true. |
>= | Greater than or equal to the operator, It checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then it becomes true. | ($A >= $B) is not true. |
<= | Less than or equal operator, It checks if the value of the left operand is less than or equal to the value of the right operand, if yes then it becomes true. | ($A <= $B) is true. |
3. Logical operators
The logical operators are used to perform bit-level operations on operands. The following Logical operators are supported by PHP.
Operator | Name | Example | Explanation |
---|---|---|---|
and | And | $a and $b | Return TRUE if both $a and $b are true |
Or | Or | $a or $b | Return TRUE if either $a or $b is true |
xor | Xor | $a xor $b | Return TRUE if either $ or $b is true but not both |
! | Not | ! $a | Return TRUE if $a is not true |
&& | And | $a && $b | Return TRUE if either $a and $b are true |
|| | Or | $a || $b | Return TRUE if either $a or $b is true |
4. Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands.
Operator | Name | Example | Explanation |
---|---|---|---|
& | And | $a & $b | Bits that are 1 in both $a and $b are set to 1, otherwise 0. |
| | Or (Inclusive or) | $a | $b | Bits that are 1 in either $a or $b are set to 1 |
^ | Xor (Exclusive or) | $a ^ $b | Bits that are 1 in either $a or $b are set to 0. |
~ | Not | ~$a | Bits that are 1 are set to 0 and bits that are 0 are set to 1 |
<< | Shift left | $a << $b | Left shift the bits of operand $a $b steps |
>> | Shift right | $a >> $b | Right shift the bits of $ an operand by $b number of places |
5. String Operators
The string operators are used to operate on strings.
Operator | Name | Example | Explanation |
---|---|---|---|
. | Concatenation | $a . $b | Concatenate both $a and $b |
.= | Concatenation and Assignment | $a .= $b | First, concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a. $b |
6. Array Operators
Array operators are used to operate on the array.
Operator | Name | Example | Explanation |
---|---|---|---|
+ | Union | $a + $y | Union of $a and $b |
== | Equality | $a == $b | TRUE if $a and $b have the same key/value pair |
!= | Inequality | $a != $b | TRUE if $a is not equal to $b |
=== | Identity | $a === $b | TRUE if $a and $b have the same key/value pair of the same type in the same order |
!== | Non-Identity | $a !== $b | TRUE if $a is not identical to $b |
<> | Inequality | $a <> $b | TRUE if $a is not equal to $b |
Expressions in PHP
Expression is one of the most important building blocks in PHP. Expression is anything that has value. Expressions are evaluated to produce value that value can be string, number, boolean, etc. The expression often uses the operators and function calls there is an order of evaluation when more than one operator is present in the expression. Expression can also produce an object-like array.
Examples:
- $a = 10, you’re assigning ’10’ into $a
- $b=$a+5, Here you’re assigning addition of $a and 5 into $b
- Slightly more complex examples
<?php function foo () { return 5; } ?>
Writing $c = foo() ,is just like writing $c = 5
-
<?php function double($i) { return $i*2; } $b = $a = 10; /* assign the value 10 into the variable $a and $b */ $c = $a++; /* post-increment, assign original value of $a (10) to $c and the increment the $a */ $e = $d = ++$b; /* pre-increment, assign the incremented value of $b (11) to $d and $e */ $f = double($d++); /* assign twice the value of $d before the increment, 2*11 = 22 to $f */ $g = double(++$e); /* assign twice the value of $e after the increment, 2*12 = 24 to $g */ ?>
Constants in PHP
A constant is an identifier (name) for a simple value. The value of the constant can not be changed during the script execution. The constant is case-sensitive. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.
The constant is defined by using function define() and You can retrieve the value of a constant, you have to simply specifying its name. You can also use the function constant(). Only boolean, integer, float, and string can be contained in constants.
<?php define("MINSIZE", 50); echo MINSIZE; echo constant("MINSIZE"); // same thing as the previous line ?>
Valid and invalid constant names
// Valid constant names define("NAME", "Any value"); define("NAME2", "Any value"); define("NAME_3", "Any value"); define("__NAME__", "Any value"); // Invalid constant names define("2NAME", "Any value");
PHP Magic constants
PHP provides predefined constants. Five magical constants change depending on where they are used.
Sr.No | Name & Description |
---|---|
1 | __LINE__
The current line number of the file. |
2 | __FILE__
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained a relative path under some circumstances. |
3 | __FUNCTION__
The function name. (Added in PHP 4.3.0) As of PHP 5, this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercase. |
4 | __CLASS__
The class name. (Added in PHP 4.3.0) As of PHP 5, this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercase. |
5 | __METHOD__
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive). |