PHP OPERATORS:-
Operators are used to perform operation on variable and values.
PHP have the 7-Operators :-
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Increment/Decrement Operator
- Logical Operator
- String Operator
- Array Operator
PHP Arithmetic Operator:-
PHP Arithmetic Operator are used with numeric value to perform arithmetic operation like:- Addition, Subtraction, Multiplication, Division, Remainder ..
Opertaor | Name | Example | Result |
---|---|---|---|
+ | addition | $x + $y | Sum of $x and $y |
- | Subtraction | $x - $y | Difference of $x and $y |
/ | Division | $x / $y | Division of $x and $y |
* | Multiplication | $x * $y | Multiplication of $x and $y |
% | Modulus | $x % $y | Remainder of $x and $y |
** | Exponentiation | $x ** $y | Raising $x to the $y'th power |
PHP Assignment Operator:-
PHP assignment operator is used with numeric value to assign value of one variable to other variable.
Opertaor | Name | Example | Result |
---|---|---|---|
= | Equal | $x = $y | Value of $y is set to variable $x. |
+ = | Addition ($x + = $y) | $x = $x + $y | Addition of $x and $y and result store to $x. |
- = | Subtraction ($x - = $y) | $x = $x - $y | Subtraction of $x and $y and result store to $x. |
* = | Multiplication ($x * = $y) | $x = $x * $y | Multiplication of $x and $y and result store to $x. |
/ = | Division ($x / = $y) | $x = $x / $y | Division of $x and $y and result store to $x. |
% | Modulus ($x % = $y) | $x = $x % $y | Remainder of $x and $y and result store to $x. |
Example:-
<?php
$x = 20;
$x += 100;
echo $x;
?>
Output: 120
PHP Comparison Operator: -
PHP Comparison operator are used to compare two values (value would be either number or string).
Opertaor | Name | Example | Operation |
---|---|---|---|
= = | Equal | $x = = $y | Check the value of $x is equal to $y or not. |
= = = | Identical | $x = = = $y | Check the value of $x is equal to $y and of same data-types(string, int char, etc..). |
! = | Not Equal | $x ! = $y | Returns true if $x is not equal to $y. |
< > | Not Equal | $x < > $y | Returns true if $x is not equal to $y. |
! = = | Not Identical | $x ! = = $y | Check the value of $x is not equal to $y or they are not of the same data-types(string, int char, etc..). |
> | Greater than | $x > $y | Returns true if $x is greater than $y. |
< | Less than | $x < $y | Returns true if $x is less than $y. |
> = | Greater than or Equal to | $x > = $y | Returns true if $x is either greater than or equal to $y. |
< = | Less than or Equal to | $x < = $y | Returns true if $x is either less than or equal to $y. |
One of the most important operator in php is 'SCOPE RESOLUTION OPERATOR(::)'
Define :- Scope Resolution Operator or double Colon is a Token that allows access to Static, Contant, Overridden property or method of a class.
Ex 1: :: from outside the class {DefineClass.php}
<?phpclass dog
{
const color = 'My dog is of white color{form DefineClass.php}';
}
echo dog::color;
?>
OUTPUT :-My dog is of white color
Ex 1: :: from inside the class {AccessDefineClassMember.php}
<?php include'DefineClass.php' ?><?php
class snake extends dog
{
public static $length='snake is longer than my dog';
public static function doublecolon() {
echo parent::color.'<br>';
echo self::$length.'<br/>';
}
}
//$classname = 'snake';
//$classname::doublecolon(); // for PHP5.3
snake::doublecolon();
?>
OUTPUT :- My dog is of white color{form DefineClass.php}
snake is longer than my dog
snake is longer than my dog
Three special keywords self, parent and static are used to access properties or methods from inside the class definition.
No comments:
Post a Comment