0
0
PHPprogramming~5 mins

Assignment and compound assignment in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the assignment operator = do in PHP?
It assigns the value on the right side to the variable on the left side. For example, $a = 5; sets the variable $a to 5.
Click to reveal answer
beginner
What is a compound assignment operator?
A compound assignment operator combines an operation with assignment. For example, $a += 3; adds 3 to $a and then assigns the result back to $a.
Click to reveal answer
beginner
List three common compound assignment operators in PHP.
1. += (addition assignment)<br>2. -= (subtraction assignment)<br>3. *= (multiplication assignment)
Click to reveal answer
beginner
What happens when you write $x *= 2; if $x is 4?
The value of $x becomes 8 because it multiplies the current value (4) by 2 and assigns the result back to $x.
Click to reveal answer
beginner
Can compound assignment operators be used with strings in PHP? Give an example.
Yes. For example, $str = 'Hello'; $str .= ' World'; appends ' World' to $str, so $str becomes 'Hello World'.
Click to reveal answer
What does $a += 5; do in PHP?
AAssigns 5 to $a
BAdds 5 to $a and assigns the result to $a
CSubtracts 5 from $a
DMultiplies $a by 5
Which operator assigns a value to a variable?
A=
B==
C+=
D!=
What will be the value of $x after $x = 10; $x -= 3;?
A3
B13
C7
D10
Which compound assignment operator appends a string in PHP?
A*=
B-=
C+=
D.=
What does $a *= 4; do if $a is 2?
ASets $a to 8
BSets $a to 2
CSets $a to 4
DSets $a to 6
Explain how the compound assignment operator += works in PHP with an example.
Think about adding a number to a variable and saving it.
You got /3 concepts.
    Describe the difference between the assignment operator = and the compound assignment operator *=.
    One just sets a value, the other does math then sets.
    You got /3 concepts.