1

With E_NOTICE error messages enabled, PHP doesn't like the following code, unless the variables $mdDialog and $mdToast have already been defined:

if ($mdDialog || $mdToast) {
    $ngMaterial = true;
}

To avoid E_NOTICE error, I must write:

if (isset($mdDialog) || isset($mdToast)) {
    $ngMaterial = true;
}

The problem is that, with the above code, if I have a $mdDialog = false; line somewhere earlier, the statement will be truthy, which is not the idea. To avoid this, I'd have to write:

if ((isset($mdDialog) && $mdDialog) || (isset($mdToast) && $mdToast)) {
    $ngMaterial = true;
}

And this is sooo much longer and feels excessive, only to avoid the E_NOTICE message.

So, the question is, should I care about these E_NOTICE messages? Is there anything bad with checking the value of a variable that may not exist?

Marcus Edensky
  • 924
  • 3
  • 13
  • 33
  • 1
    You can use ternary for the assignment. `isset` also can take multiple variables.. or even use `!empty` if you want to see if it has a value. – user3783243 Jul 18 '18 at 17:26

3 Answers3

4

empty can be useful in this case. Like below:

if (!empty($mdDialog) || !empty($mdToast)) {
    $ngMaterial = true;
}

Read more about empty.

Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
  • True, that works perfectly well, and cleaner than `isset()`, though the question remains - is best practice to use the `empty()` function to remove any notices, or is the code quicker just by checking directly if the var is truthy, and hiding any notices that are thrown if it's undefined? – Marcus Edensky Jul 18 '18 at 17:42
  • `empty` also checks for isset along with empty values like blank string `''`, `NULL`, `false` etc. So there will be no undefined warning. – Lovepreet Singh Jul 18 '18 at 17:43
  • Note the `The following values are considered to be empty` section in the manual though. If any of those values are valid you'll need to use an `||` and the valid value check; or go back to `isset`. – user3783243 Jul 18 '18 at 17:45
  • 5
    It's best practice to write code that does not generate notices, whether hidden or not. – AbraCadaver Jul 18 '18 at 17:45
  • AbraCadaver Why are notices like these bad? What is caused by checking an undefined var? – Marcus Edensky Jul 18 '18 at 17:46
  • @MarcusEdensky You need to use the `@` for notices to be sent. – user3783243 Jul 18 '18 at 17:49
  • 2
    @MarcusEdensky Notices *aren't* bad, that's the point -- but if you blindly ignore them, then you'll never see the ones that are actually meaningful. E.g., if you write your code to not generate notices, and then you get a notice about an undefined variable, then you can be pretty sure you have a bug. Maybe you simply have a spelling typo -- which you'd never catch if you disabled notices. – Alex Howansky Jul 18 '18 at 17:50
1

Or if the variable is a string you can also use this:

if(strlen($mdDialog) < 1 || strlen($mdToast) < 1){
     $ngMaterial = true;
 } 
sach jot
  • 570
  • 4
  • 16
0

You're always better off avoiding generating notices than setting error_reporting() to ignore them. That said, in cases like this, it's almost always easier to make sure that the vars are set at the start than it is to check that they're set every place you refer to them. I.e., the var never gets set for the false condition if you do something like this:

if (some condition) {
    $flag = true;
}

Instead do something like this:

if (some condition) {
    $flag = true;
} else {
    $flag = false;
}

Or this:

$flag = false;
if (some condition) {
    $flag = true;
}

Or this:

$flag = (some condition) ? true : false;

Or this:

$flag = (bool) (some condition);

Now you don't need isset() or empty(), and when you get an E_NOTICE, it's actually meaningful.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98