Introduction:
Learn the ins and outs of PHP assignment operators with our comprehensive guide. Whether you’re a beginner or an experienced developer, this lesson covers the basics, advanced techniques, and practical examples to enhance your PHP programming skills.
In PHP, assignment operators are used to assign values to variables. They are shorthand notations that combine the assignment operation with another operation. Here are some commonly used assignment operators in PHP:
The basic assignment operator is the equal sign (=). It assigns the value on the right to the variable on the left.
$a = 10; // $a is assigned the value 10
Adds the right operand to the left operand and assigns the result to the left operand.
$a += 5; // Equivalent to: $a = $a + 5;
Subtracts the right operand from the left operand and assigns the result to the left operand.
$a -= 3; // Equivalent to: $a = $a – 3;
Multiplies the left operand by the right operand and assigns the result to the left operand.
$a *= 2; // Equivalent to: $a = $a * 2;
Divides the left operand by the right operand and assigns the result to the left operand.
$a /= 4; // Equivalent to: $a = $a / 4;
Calculates the modulus of the left operand divided by the right operand and assigns the result to the left operand.
$a %= 3; // Equivalent to: $a = $a % 3;
Concatenates the right operand to the left operand and assigns the result to the left operand.
$str = “Hello “;
$str .= “World!”; // Equivalent to: $str = $str . “World!”;
These assignment operators help to perform operations and update the value of a variable in a concise and readable manner.
complete example in html with explanation
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Assignment Operators Example</title> </head> <body> <?php // PHP code starts here // Basic assignment $a = 10; echo "<p>Value of \$a after basic assignment: $a</p>"; // Addition assignment $a += 5; echo "<p>Value of \$a after addition assignment: $a</p>"; // Subtraction assignment $a -= 3; echo "<p>Value of \$a after subtraction assignment: $a</p>"; // Multiplication assignment $a *= 2; echo "<p>Value of \$a after multiplication assignment: $a</p>"; // Division assignment $a /= 4; echo "<p>Value of \$a after division assignment: $a</p>"; // Modulus assignment $a %= 3; echo "<p>Value of \$a after modulus assignment: $a</p>"; // Concatenation assignment $str = "Hello "; $str .= "World!"; echo "<p>Concatenated string: $str</p>"; // PHP code ends here ?> </body> </html>
Explanation:
The HTML page displays the results of each operation, providing a visual representation of the effects of different assignment operators in PHP.
Am application about this lesson with explanation
Let’s create a simple PHP web application that provides a quiz about PHP assignment operators. This application will consist of two pages: one for the quiz questions and another for displaying the quiz results.
index.php (Quiz Questions Page):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Assignment Operators Quiz</title> </head> <body> <h1>PHP Assignment Operators Quiz</h1> <form action="results.php" method="post"> <?php $questions = array( "What is the purpose of the basic assignment operator in PHP?", "Which operator is used for addition assignment in PHP?", "What does the following code do? \$a -= 3;", "What does the modulus assignment operator (\%=) do in PHP?", "How is concatenation assignment represented in PHP?", "What will be the value of \$a after the code \$a *= 2; if \$a was initially 5?", "Which operator is used for division assignment in PHP?", "What is the output of the code \$str = 'Hello '; \$str .= 'World!'; echo \$str;?", "What is the primary purpose of concatenation assignment?", "Which assignment operator is used to subtract a value in PHP?", "What will be the value of \$a after the code \$a /= 4; if \$a was initially 16?", "What does the code \$a %= 3; do?", "How is the value of \$a updated in the code \$a += 5; if \$a was initially 8?", "Which assignment operator is used for basic assignment in PHP?", "What is the purpose of the concatenation operator (.) in PHP?" ); $choices = array( array("A) Addition", "B) Subtraction", "C) Assignment", "D) Concatenation"), array("A) +=", "B) -=", "C) *=", "D) /="), array("A) Adds 3 to \$a", "B) Subtracts 3 from \$a", "C) Multiplies \$a by 3", "D) Divides \$a by 3"), array("A) Adds", "B) Subtracts", "C) Finds the remainder", "D) Multiplies"), array("A) .+", "B) =.", "C) .=", "D) :+"), array("A) 10", "B) 7", "C) 15", "D) 2.5"), array("A) %", "B) /", "C) /=", "D) *="), array("A) Hello", "B) World!", "C) Hello World!", "D) Hello + World!"), array("A) Addition", "B) Subtraction", "C) String concatenation", "D) Division"), array("A) -", "B) -=", "C) /=", "D) *="), array("A) 4", "B) 2", "C) 8", "D) 16"), array("A) Multiplies \$a by 3", "B) Divides \$a by 3", "C) Adds 3 to \$a", "D) Finds the remainder of \$a divided by 3"), array("A) \$a remains 8", "B) \$a becomes 5", "C) \$a becomes 13", "D) \$a becomes 3"), array("A) =:", "B) ==", "C) =", "D) :="), array("A) Addition", "B) Subtraction", "C) String concatenation", "D) Multiplication") ); for ($i = 0; $i < count($questions); $i++) { echo "<p><strong>{$questions[$i]}</strong></p>"; foreach ($choices[$i] as $choice) { echo "<label><input type='radio' name='q{$i}' value='{$choice}'> {$choice}</label><br>"; } echo "<br>"; } ?> <button type="submit">Submit Quiz</button> </form> </body> </html>
results.php (Quiz Results Page):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Assignment Operators Quiz Results</title> </head> <body> <h1>PHP Assignment Operators Quiz Results</h1> <?php $correctAnswers = array( "C) Assignment", "A) +=", "B) Subtracts 3 from \$a", "C) Finds the remainder", "C) .=", "A) 10", "C) /=", "C) Hello World!", "C) String concatenation", "B) -=", "B) 2", "D) Finds the remainder of \$a divided by 3", "C) \$a becomes 13", "C) =", "C) String concatenation" ); $score = 0; foreach ($_POST as $key => $value) { $Answer = htmlspecialchars($value); $questionNumber = filter_var($key, FILTER_SANITIZE_NUMBER_INT); echo "<p><strong>Question {$questionNumber}:</strong><br>"; echo "Your Answer: {$Answer}<br>"; echo "Correct Answer: {$correctAnswers[$questionNumber]}</p>"; if ($Answer === $correctAnswers[$questionNumber]) { $score++; } } echo "<h2>Your Score: {$score} / " . count($correctAnswers) . "</h2>"; ?> </body> </html>
Explanation:
A quiz about this lesson:15 questions
A) Addition
B) Subtraction
C) Assignment
D) Concatenation
A) +=
B) -=
C) *=
D) /=
3. What does the following code do? $a -= 3;
A) Adds 3 to $a
B) Subtracts 3 from $a
C) Multiplies $a by 3
D) Divides $a by 3
A) Adds
B) Subtracts
C) Finds the remainder
D) Multiplies
A) .+
B) =.
C) .=
D) :+
A) 10
B) 7
C) 15
D) 2.5
A) Hello
B) World!
C) Hello World!
D) Hello + World!
A) Addition
B) Subtraction
C) String concatenation
D) Division
– A) –
– B) -=
– C) /=
– D) *=
– A) 4
– B) 2
– C) 8
– D) 16
– A) Multiplies $a by 3
– B) Divides $a by 3
– C) Adds 3 to $a
– D) Finds the remainder of $a divided by 3
– A) $a remains 8
– B) $a becomes 5
– C) $a becomes 13
– D) $a becomes 3
– A) :=
– B) ==
– C) =
– D) =
– A) Addition
– B) Subtraction
– C) String concatenation
– D) Multiplication
1-C) Assignment
2-A) +=
3-B) Subtracts 3 from $a
4-C) Finds the remainder
5-C) .=
6-A) 10
7-C) /=
8-C) Hello World!
9-C) String concatenation
10-B) -=
11-B) 2
12-D) Finds the remainder of $a divided by 3
13-C) $a becomes 13
14-C) =
15-C) String concatenation