0% found this document useful (0 votes)
3 views

PHP Operators

The document provides an overview of various PHP operators, including arithmetic, assignment, comparison, increment/decrement, logical, string, array, and conditional operators. Each type of operator is explained with its name, symbol, and examples of usage. Additionally, it details the functionality of the ternary operator and its syntax for conditional operations.

Uploaded by

dethspiker2849
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PHP Operators

The document provides an overview of various PHP operators, including arithmetic, assignment, comparison, increment/decrement, logical, string, array, and conditional operators. Each type of operator is explained with its name, symbol, and examples of usage. Additionally, it details the functionality of the ternary operator and its syntax for conditional operations.

Uploaded by

dethspiker2849
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP Operators

PHP has different types of operators for different operations. They are as follows:

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations.

Name operator Example


Addition + $a+$b
Subtraction = $a-$b
Multiplication * $a*$b
Division / $a/$b
Modulus % $a%$b
Exponentation ** $a**$b

2. Assignment Operators

These operators are used to assign values to variables.

Sign Evaluated as
= a=b
+= a=a+b
-= a=a-b
*= a=a*b
/= a=a/b
%= a=a%b

3. Comparison Operators

These operators are used to compare two values.

Name Operator Example


Equal == $x==$y
Identical === $x===$y
Not equal != $x!=$y
Not equal <> $x<>$y
Not identical !=== $x!===$y
Greater than > $x>$y
Less than < $x<$y
Greater than or equal to >= $x>=$y
Less than or equal to <= $x<=$y
Spaceship <=> $x$y
4. Increment/decrement operator

These operators are used to increment/ decrement variable’s value.

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.

5. PHP Logical Operators

These are the logical operators that combine conditional statements.

Name Operator Example


And And(&&) $a && $b
Or Or(||) $a || $b
Xor Xor $a xor $b
Not ! $a != $b

6.String Operator

PHP has these two operators designed for strings.

Name Operator Example


Concatenation . $text1. $text2
Concatenation assignment .= $text.=$text2

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;

7. PHP Array Operators

These Operators are used to compare arrays.

Name Operator Example


Union + $x+$y
Equality == $x==$y
Identity === $x===$y
Inequality != $x!=$y
Inequality <> $x<>$y
Non-identity !== $x!==$y

8. PHP Conditional Operators

These operators assign values to operands based on the outcome of a certain condition.

Name Operator Example


Ternary ?: $x= exp1?exp2:exp3
Null Coalescing ?? $x= exp1??exp2

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:

(Condition) ? (Statement1) : (Statement2);

 Condition: It is the expression to be evaluated and returns a boolean value.

 Statement 1: It is the statement to be executed if the condition results in a true state.

 Statement 2: It is the statement to be executed if the condition results in a false state.

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;

print ("Value of b is " . $b);

?>

Output:

Value of b is 5

You might also like