Monday 17 July 2017

How to avoid Notices and warnings errors in PHP code.

While writing PHP code, we often get errors like warnings and notices.

To avoid them, following are some best practises:

1) Always check if a variable is set before using it.

For example:

$age = $_GET['age'];

Should be replaced by

$age = isset($_GET['age']) ? ($_GET['age'] : '';

Reference:

http://php.net/manual/en/function.isset.php

2) Always check if array is not empty before looping over it.

For example:

foreach ($arr as $elem) {
  // Your code.
}

Should be replaced by:

if (! empty($arr)) {
 foreach ($arr as $elem) {
   // Your code.
 }
}

Reference:

http://php.net/manual/en/function.empty.php

No comments:

Post a Comment

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...