Search code examples
phpnotice

How to prevent PHP Notices with a defined variable?


Basicly I have a bunch of links on a page and I will use something like this

<?PHP echo $SITE_PATH ?>

Many many times on the same page but it will show a Notice in PHP for doing so.

I know you are supposed to use things like isset() but would I really need to use it every time I call?

<?PHP echo $SITE_PATH ?>

--- EDIT :
If I switch to using a defined variable, then the notices seem to go away


Solution

  • Two solutions, here :

    • only use variables that exist -- which is what you should probably do.
    • or test if they exist (with isset) before trying to use them.

    There is no magic ^^

    If your application is using many not-set variables, you probably have some problem in your design.


    In a case such as the one you presented, for a variable that's used lots of times, I would make sure it exists, and, if not, set it to '', for instance, at the beginning of my script.

    That's not really looking great, but it'll work -- and, this way, you won't have to go through your whole application, patching everything.

    (Or I might also disable the E_NOTICE error_reporting level -- I don't like that idea, but, sometimes, it's really the only way to deal with some code base)