PHP Operators
- Assignment Operators
- Arithmetic Operators
- Comparison Operators
- String Operators
- Combination Arithmetic & Assignment Operators
Operators -
In all programming languages, operators are used to manipulate or perform operations on variables and values.
There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all.
There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all.
Assignment operator
Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the "=", or equal character.
Example:
Example:
$my_var = 4;
$another_var = $my_var;
$another_var = $my_var;
Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.
Arithmetic operators
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
| x+y | Addition | Sum of x and y | 2+2 | 4 |
| x-y | Substraction | Diffrence of x and y | 10-4 | 6 |
| x*y | Multiplication | Product of x and y | 12*8 | 96 |
| x/y | Division | Quotient of x and y | 15/5 | 3 |
| x%y | Modulus | Remainder of x divided by y | 20%6 | 2 |
Comparison operators
| Operator | Name | Descryption | Example |
|---|---|---|---|
| x==y | Equal | True if x is equals to y | 5==8 return false |
| x===y | Identical | True if x is equal to y, and they are of sametype | 5==="5" returns false |
| x != y | Not equal | True if x is not equal to y | 5!=8 returns true |
| x <> y | Not equal | True if x is not equal to y | 5<>8 returns true |
| x !== y | Not identical | True if x is not equal toy, or they are not of same type | 5!=="5" returns true |
| x > y | Greater than | True if x is greater than y | 5>8 returns false |
| x < y Less than | Less than | True if x is less than y | 5<8 returns true |
| x >= y | Greater than or equal to | True if x is greater than or equal to y | 5>=8 returns false |
| x <= y | Less than or equal to | True if x is less than or equal to y | 5<=8 returns true |
String operators
As we have already seen in the Echo Lesson, the period "." is used to add two strings together, or more technically, the period is the concatenation operator for strings.
PHP Code:
$a_string = "Hello";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";
Display:
Hello Billy!PHP Incrementing/Decrementing Operators
PHP Code:
$x = 4;
echo 'The value of x with post-plusplus = ' . $x++;
echo ' The value of x after the post-plusplus is ' . $x;
$x = 4;
echo '
The value of x with with pre-plusplus = ' . ++$x;
echo ' The value of x after the pre-plusplus is ' . $x;
echo 'The value of x with post-plusplus = ' . $x++;
echo ' The value of x after the post-plusplus is ' . $x;
$x = 4;
echo '
The value of x with with pre-plusplus = ' . ++$x;
echo ' The value of x after the pre-plusplus is ' . $x;
Display:
The value of x with post-plusplus = 4The value of x after the post-plusplus is = 5
The value of x with with pre-plusplus = 5
The value of x after the pre-plusplus is = 5
PHP Logical Operators
| Operator | Name | Descryption |
|---|---|---|
| x and y | And | True if both x and y |
| x or y | or | True if either or both x and y |
| x && y | And | True if both x and y are true |
| x || y | Or | True if either or both x and y are true |
| ! x | Not | True if x is not true x=6 |
No comments:
Post a Comment