0% found this document useful (0 votes)
5 views4 pages

Operators and Experssions

The document provides an overview of operators and expressions in PHP, detailing various types such as arithmetic, assignment, comparison, logical, string, and conditional operators with definitions and examples. It explains how operators perform operations on variables and values to create expressions that evaluate to a value. Additionally, it includes specific code snippets demonstrating the use of each operator type.

Uploaded by

dhanushbro50
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)
5 views4 pages

Operators and Experssions

The document provides an overview of operators and expressions in PHP, detailing various types such as arithmetic, assignment, comparison, logical, string, and conditional operators with definitions and examples. It explains how operators perform operations on variables and values to create expressions that evaluate to a value. Additionally, it includes specific code snippets demonstrating the use of each operator type.

Uploaded by

dhanushbro50
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/ 4

Operator and Expressions definitions and example

Operators and Expressions in PHP


Operators are symbols that tell the PHP interpreter to perform specific operations. They act o
n variables and values, creating expressions. Expressions are combinations of variables, const
ants, and operators that evaluate to a value.
Here's a breakdown of different types of operators with definitions and examples:

1. Arithmetic Operators

Definition: Used to perform mathematical operations.


Addition (+): Adds two values.

Php Copy

$sum = 5 + 3;
echo $sum; // Output: 8

Subtraction (-): Subtracts one value from another.

Php Copy

$difference = 10 - 7;
echo $difference; // Output: 3

Multiplication ()*: Multiplies two values.

Php Copy

$product = 4 * 2;
echo $product; // Output: 8

Division (/): Divides one value by another.

Php Copy

$quotient = 8 / 2;
echo $quotient; // Output: 4

Modulus (%): Returns the remainder of the division.


Php Copy

$remainder = 5 % 2;
echo $remainder; // Output: 1

2. Assignment Operators

Definition: Used to assign values to variables.


Assignment (=): Assigns a value to a variable.

Php Copy

$x = 10;

Addition Assignment (+=): Adds a value to a variable and assigns the result.

Php Copy

$x = 5;
$x += 3; // Equivalent to $x = $x + 3;
echo $x; // Output: 8

3. Comparison Operators

Definition: Used to compare two values.


Equal (==): Checks if two values are equal.

Php Copy

$result = (5 == 5); // TRUE

Identical (===): Checks if two values are equal and of the same type.

Php Copy

$result = (5 === "5"); // FALSE (different types)

Not Equal (!= or <>): Checks if two values are not equal.

Php Copy

$result = (5 != 3); // TRUE


4. Logical Operators

Definition: Used to combine conditional statements.

And (&&): Returns TRUE if both operands are true.

Php Copy

$result = (true && false); // FALSE

Or (||): Returns TRUE if at least one of the operands is true.

Php Copy

$result = (true || false); // TRUE

Not (!): Inverts the truth value.

Php Copy

$result = !(true); // FALSE

5. String Operators

Definition: Used to concatenate strings.

Concatenation (.): Joins two strings.

Php Copy

$text = "Hello" . " world!";


echo $text; // Output: Hello world!

6. Conditional (Ternary) Operator

Definition: Shorthand for an if-else statement.

Ternary (?:): Evaluates a condition and returns one of two values.

Php Copy

$age = 20;
$result = ($age >= 18) ? "Adult" : "Minor";
echo $result; // Output: Adult

Expressions
Definition: Combinations of variables, constants, and operators that evaluate to a value.

Example:

Php Copy

$a = 5;
$b = 10;
$c = $a + $b; // Expression: $a + $b
echo $c; // Output: 15

You might also like