Friday 27 July 2018

PHP Tips and Tricks

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.

Wednesday 25 July 2018

Ternary Operator in PHP

Ternary operator is a shrtcut operator for if else statement.

Syntax:

$var = (condition) ? if yes, value : if no, value;

For example:
If you have an if else statement like this:

if ($b>5) {
 $a = 3;
}
else {
 $a = 5;
}

The same thing can be wrote with ternary operator in a single line like:


$a = ($b>5) ? 3 : 5;

Thus, we have seen how much lines of codes is reduced.

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...