In this tutorial, you shall learn about Simple Assignment Operator, its syntax, and how to use this operator in programs, with examples.

PHP – Simple Assignment Operator

Simple assignment operator is used to assign a value to your variable.

The operator takes two operands, and stores the right operand in the left operand.

Syntax

The syntax to assign a value or something to a variable using simple assignment operator is

variable = value

where variable is the left operand and value is the right operand.

Examples

1. Assign value to a variable

In the following example, we assign an integer value 10 to a variable $x using simple assignment operator.

PHP Program

<?php
$x = 10;
print_r("x is {$x}");
?>

Output

PHP - Simple Assignment Operator - Assign value to variable

2. Assign an expression to a variable

We can write an expression and assign it to a variable using simple assignment operator. The expression is first evaluated and then the resulting value is stored in the variable.

In the following example, we assign an expression of adding two numbers to a variable $result.

PHP Program

<?php
$a = 10;
$b = 30;

$result = $a + $b;
print_r("Sum of {$a} and {$b} is {$result}.");
?>

Output

PHP - Simple Assignment Operator - Assign expression to variable

Conclusion

In this PHP Tutorial, we learned about Simple Assignment Operator, and how to use it to assign a value to a variable.