Results 1 to 4 of 4

Thread: Just a thought, to deal with undefined variables.

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default Just a thought, to deal with undefined variables.

    Undefined variables or indexes of arrays can be a real pain if you have a strong error level setup.

    isset($var)?$var:'';
    can be very tedious if you have a long script.

    So, here's my thought--
    PHP Code:
    function valueof($var) {
    return isset(
    $var)?$var:'';

    Simple, though seems to be convenient.

    Much faster than the other options.


    That should be included in PHP by default, I think.
    Last edited by djr33; 09-16-2007 at 01:21 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Also known as:
    Code:
    @$var
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, good point. I've used that before, too.
    I guess I just don't like error suppression.
    Is there really no downside to suppressing errors in terms of performance, etc?
    I like having valid code in PHP, so everything is smooth, rather than just bypassing something that is wrong.
    Though, in this case, the error message for an undefined variable is quite stupid in a lot of cases.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I like having valid code in PHP, so everything is smooth, rather than just bypassing something that is wrong.
    Though, in this case, the error message for an undefined variable is quite stupid in a lot of cases.
    The message for an undefined variable isn't an error, it's a warning: it exists to warn you that you're doing something that's quite often a sign of a bug in the script. If you know what you're doing and know that it's not caused by a bug, there's nothing wrong with suppressing it, and it's not "invalidating" your code to do so.
    Is there really no downside to suppressing errors in terms of performance, etc?
    It has less of a performance hit than your function call and two variable accesses, certainly.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •