Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How do I ignore undefined variable notices? Changes to config file are ignored.
  • Hi guys, I downloaded and installed FuelPHP 1.1-rc1 tonight, and it's awesome. My only problem is that I can't turn the notices off! I have several variables in my template file that don't always have a value, so I don't pass them. For example, I have a variable $body_classname that I use when I need it, and don't pass it when I don't need it...
    <body class="<?= $body_classname ?>">
    
    PHP has never really complained about it, but FuelPHP is displaying notices on my page...
    [b]Notice![/b]
    ErrorException [ NOTICE ]: Undefined variable "body_classname"
    [b]APPPATH/views/template.php @ line 49[/b]
    

    I know it's undefined, I didn't pass it in. Don't worry about it Fuel, just get on with it. I tried changing the 'errors' options in fuel/app/config/config.php (line 51). I changed 'throttle' to 0, no difference. I changed 'notices' to false, no difference. I changed the 'continue_on' array to an empty array, no difference. The notices remain. I thought maybe the config file was being cached magically somewhere somehow (I don't have caching enabled), but when I change line 43 to default to production environment, the notices dissappear.
    // old, defaults to development
    43. 'environment' => (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::DEVELOPMENT),
    // new, defaults to production
    43. 'environment' => (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::PRODUCTION),
    
    Am I missing something, or is this an issue? Thanks!
  • Imho that is a very bad programming practice. Either always pass a value, or check if the value exists before using it. The reason for seeing these now is that FuelPHP in development mode enables all error messages. In your previous setup you'd probably filtered notices. Not a good idea, because your logfiles will still overflow with these messages, and your application is slower because of all the error handling,
  • Thanks Wan, you pointed me in the right direction. I usually use conditional statements, but even those got notices:
    // try a compact tertiary
    <body class="<? $body_classname ?: '' ">
    // try a good ol' if-then
    <? if ( $body_classname ): ?>
        <body class="<?= $body_classname ?>">
    <? else: ?>
        <body>
    <? endif; ?>
    
    Turns out I misread the PHP docs a long time ago. I always thought these two statements were identical when $var is undeclared:
    1. if ( $var )  ...
    2. if ( ! empty( $var ) ) ...
    
    They are basically identical, however, PHP issues a warning in the first case when the variable is not set.
    "empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."
    http://php.net/manual/en/function.empty.php

    Bottom line: I've been doing it wrong for years, and I guess my log-level was customized or too high in my php.ini to notice. Thanks for pointing me in the right direction. This is just an aside, but shouldn't I still be able to configure my 'errors' even if my environment is development? Why include the error options in fuel/app/config/config.php if they're ignored? Thanks buddy.
  • It's one of the reasons we like to enable ALL error messages on development. Note that there's a difference between empty() and isset(). It's not going to be very important for views (as you'd probably need strings there), but you might want to look it up... Notices are supressed when:
    if (\Fuel::$is_test or ( ! $always_show and (\Fuel::$env == \Fuel::PRODUCTION or \Config::get('errors.notices', true) === false)))
      {
       return;
      }
    

    so the notices switch only has relevance in a production environment, in development they will always show, for the reasons I already mentioned.
  • Copy that. Thanks buddy.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion