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