PHP Operators
PHP Operators
PHP has different types of operators for different operations. They are as follows:
1. Arithmetic Operators
2. Assignment Operators
Sign Evaluated as
= a=b
+= a=a+b
-= a=a-b
*= a=a*b
/= a=a/b
%= a=a%b
3. Comparison Operators
Name Operator
Pre-increment ++$a
Post-increment $a++
Pre-decrement --$a
Post-decrement $a--
Pre/post Increment
In pre increment operator the value is incremented and than executed but in the post I
increment the value is executed than it is incremented.
Pre/post decrement
In pre decrement operator the value is decremented than it is executed but in post decrement
operator the value is decremented after the execution.
6.String Operator
Concatenation Operator
In PHP, the concatenation operator is the "dot" (.) which is used to join two strings together to
create a single string; essentially combining them end-to-end. for example:
Str=String1;
Str2=string2;
Str3=str1.str3;
Concatenation assignment
n PHP, the concatenation assignment operator is ".=" which allows you to append a string to an
existing string variable, essentially combining them together; it is a shorthand way of
writing $variable = $variable . "new string";. For example
$name = "John";
$name .= " Doe"; // This will add " Doe" to the end of $name, making it "John Doe"
echo $name;
These operators assign values to operands based on the outcome of a certain condition.
Ternary Operator
ternary operator (?:) is a conditional operator used to perform a simple comparison or check
on a condition having simple statements. It decreases the length of the code performing
conditional operations. The order of operation of this operator is from left to right. It is called
a ternary operator because it takes three operands- a condition, a result statement for true,
and a result statement for false. The syntax for the ternary operator is as follows.
Syntax:
For example, if the value of $a is greater than 15, then 20 will be returned and will be assigned
to $b, else 5 will be returned and assigned to $b.
php
<?php
$a = 10;
$b = $a > 15 ? 20 : 5;
?>
Output:
Value of b is 5