0% found this document useful (0 votes)
26 views10 pages

PHP Operators

The document discusses various PHP operators used to manipulate data values. It describes literals as direct data values in code, and operators that perform operations on variables and values. It then provides details on different types of PHP operators, including arithmetic, assignment, comparison, increment/decrement, logical, string, array, and conditional assignment operators. Examples are given for each operator type.

Uploaded by

Dhanraj pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

PHP Operators

The document discusses various PHP operators used to manipulate data values. It describes literals as direct data values in code, and operators that perform operations on variables and values. It then provides details on different types of PHP operators, including arithmetic, assignment, comparison, increment/decrement, logical, string, array, and conditional assignment operators. Examples are given for each operator type.

Uploaded by

Dhanraj pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Literals

A literal is a data value that appears directly in a program.


Ex.
$marks=99;
$subject=”Internet programming”;

PHP Operators
Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators
 Array operators
 Conditional assignment operators

Precedence and Associativity


In general, operators have a set precedence, or order, in which they are evaluated.
Operators also have associativity, which is the order in which operators of the same
precedence are evaluated. This order is generally left to right (called left for short),
right to left (called right for short), or not relevant.
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result Show


it

+ Addition $x + $y Sum of $x and $y Try it »

- Subtraction $x - $y Difference of $x and $y Try it »

* Multiplication $x * $y Product of $x and $y Try it »

/ Division $x / $y Quotient of $x and $y Try it »

% Modulus $x % $y Remainder of $x divided Try it »


by $y

** Exponentiation $x ** $y Result of raising $x to the Try it »


$y'th power
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to
a variable.

The basic assignment operator in PHP is "=". It means that the left operand
gets set to the value of the assignment expression on the right.

Assignment Same as... Description Show


it

x=y x=y The left operand gets set to the value of the Try it »
expression on the right

x += y x=x+y Addition Try it »

x -= y x=x-y Subtraction Try it »

x *= y x=x*y Multiplication Try it »

x /= y x=x/y Division Try it »

x %= y x=x%y Modulus Try it »


PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or
string):

Operator Name Example Result Show


it

== Equal $x == $y Returns true if $x is equal to $y Try it


»

=== Identical $x === Returns true if $x is equal to $y, and they are of Try it
$y the same type »

!= Not equal $x != $y Returns true if $x is not equal to $y Try it


»

<> Not equal $x <> $y Returns true if $x is not equal to $y Try it


»

!== Not identical $x !== Returns true if $x is not equal to $y, or they are Try it
$y not of the same type »

> Greater than $x > $y Returns true if $x is greater than $y Try it


»
< Less than $x < $y Returns true if $x is less than $y Try it
»

>= Greater than $x >= $y Returns true if $x is greater than or equal to $y Try it
or equal to »

<= Less than or $x <= $y Returns true if $x is less than or equal to $y Try it
equal to »

<=> Spaceship $x <=> Returns an integer less than, equal to, or greater Try it
$y than zero, depending on if $x is less than, equal »
to, or greater than $y. Introduced in PHP 7.
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

Operator Name Description Show


it

++$x Pre-increment Increments $x by one, then returns Try it »


$x

$x++ Post- Returns $x, then increments $x by Try it »


increment one

--$x Pre-decrement Decrements $x by one, then returns Try it »


$x

$x-- Post- Returns $x, then decrements $x by Try it »


decrement one
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.

Operator Name Example Result

and And $x True if both $x and Try it »


and $y are true
$y

or Or $x or True if either $x or $y Try it »


$y is true

xor Xor $x True if either $x or $y Try it »


xor is true, but not both
$y

&& And $x && True if both $x and Try it »


$y $y are true

|| Or $x || True if either $x or $y Try it »


$y is true

! Not !$x True if $x is not true Try it »


PHP String Operators
PHP has two operators that are specially designed for strings.

Operator Name Example Result

. Concatenation $txt1 Concatenation of $txt1 and Try it »


. $txt2
$txt2

.= Concatenation $txt1 Appends $txt2 to $txt1 Try it »


assignment .=
$txt2
PHP Array Operators
The PHP array operators are used to compare arrays.

Operator Name Example Result Show


it

+ Union $x + $y Union of $x and $y Try it


»

== Equality $x == $y Returns true if $x and $y have the same key/value Try it


pairs »

=== Identity $x === Returns true if $x and $y have the same key/value Try it
$y pairs in the same order and of the same types »

!= Inequality $x != $y Returns true if $x is not equal to $y Try it


»

<> Inequality $x <> $y Returns true if $x is not equal to $y Try it


»

!== Non- $x !== $y Returns true if $x is not identical to $y Try it


identity »
PHP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a value depending on
conditions:

Operator Name Example Result Show


it

?: Ternary $x Returns the value of $x. Try it


= expr1 ? expr2 : expr3 The value of $x is expr2 if expr1 = »
TRUE.
The value of $x is expr3 if expr1 =
FALSE

?? Null $x = expr1 ?? expr2 Returns the value of $x. Try it


coalescing The value of $x »
is expr1 if expr1 exists, and is not
NULL.
If expr1 does not exist, or is NULL,
the value of $x is expr2.
Introduced in PHP 7

You might also like