Use short cut operators for adding/subtracting values from variables.
1) += operator.
Consider following:
$a = 10;
$a = $a + 3;
// $a is 13 now.
We can replace the second statement by:
$a += 3; // Add 3 to $a
2) -= operator.
Consider following:
$a = 10;
$a = $a - 3;
// $a is 7 now.
We can replace the second statement by:
$a -= 3; // Subtract 3 to $a
3) Say if you are assigning a variable to some value if some condition is met. Also, the if condition has another variable set.
Just look at the following example:
if ($condition == TRUE) {
$a = 7;
$b = 9;
}
else {
$a = 11;
}
We can skip the else structure like following:
$a = 11;
if ($condition == TRUE) {
$a = 7;
$b = 9;
}
In above code, we have reduced 2 lines of code.
1) += operator.
Consider following:
$a = 10;
$a = $a + 3;
// $a is 13 now.
We can replace the second statement by:
$a += 3; // Add 3 to $a
2) -= operator.
Consider following:
$a = 10;
$a = $a - 3;
// $a is 7 now.
We can replace the second statement by:
$a -= 3; // Subtract 3 to $a
3) Say if you are assigning a variable to some value if some condition is met. Also, the if condition has another variable set.
Just look at the following example:
if ($condition == TRUE) {
$a = 7;
$b = 9;
}
else {
$a = 11;
}
We can skip the else structure like following:
$a = 11;
if ($condition == TRUE) {
$a = 7;
$b = 9;
}
In above code, we have reduced 2 lines of code.
No comments:
Post a Comment