SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to avoid NOTICE: Undefined Index??

    Currently when I check to see if a variable == true I get an notice level error if it isnt set which is frustrating the only way I can think of to easily fix this is to change my if statements from this
    PHP Code:
    if($value){
     
    //do something

    to this
    PHP Code:
    if(isset($value) && $value == true){
      
    //do something

    I know of "empty()" but is this relavant because I also need to check if it equals true and empty() is only different to isset() is that a variable can be initialised to null and isset() will return true where empty() will return false.

    What do you think? or use?

  2. #2
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    umm how about declaring all your variables before trying to use them? Or use isset
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dakine View Post
    Currently when I check to see if a variable == true I get an notice level error if it isnt set which is frustrating the only way I can think of to easily fix this is to change my if statements from this
    PHP Code:
    if($value){
     
    //do something

    to this
    PHP Code:
    if(isset($value) && $value == true){
      
    //do something

    I know of "empty()" but is this relavant because I also need to check if it equals true and empty() is only different to isset() is that a variable can be initialised to null and isset() will return true where empty() will return false.

    What do you think? or use?
    declare an variable before use it.

    regards

  4. #4
    SitePoint Enthusiast Pramit's Avatar
    Join Date
    Feb 2007
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error reporting

    undefined index occurs due to undefined variables. These actually dont cause problem to program execution. To avoid all the kinds of warnings, you can simply put error_reporting(0) in the page that shows warnings

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pramit View Post
    undefined index occurs due to undefined variables. These actually dont cause problem to program execution. To avoid all the kinds of warnings, you can simply put error_reporting(0) in the page that shows warnings
    Pretending errors aren't happening doesn't make it true

  6. #6
    SitePoint Enthusiast dakine's Avatar
    Join Date
    Dec 2006
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok ok ok, declaring the variable is extremely obvious, but php doesnt require you to do it unlike other languages. I was merely seeing if there was another technique other people used.

  7. #7
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    using isset is the way to go.
    -- Jelena --

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    isset all the way.
    Code to avoid errors not surpress them at runtime.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    declaring the variable is extremely obvious, but php doesnt require you to do it unlike other languages
    PHP may not require it, but good practise definitely *does* require it.

  10. #10
    SitePoint Guru
    Join Date
    Sep 2003
    Location
    Northern California
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm an intermediate PHP coder I suppose. Curious to know what practical issues arise from not declaring variables and getting the Error message? Is there a tangible downside or is it just a matter of principle/ethic/good practice?

    If the latter, then why do articles/tutorials recommend turning error_reporting off once development is complete? Why not leave it on?

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    mgm, there realy is no valid reason to put your applications out knowing there are bugs in it. Turning the error_reporting off is only a precaution if an error MAY occur giving away details of the structure of your filesystem or giving clues to your mysql setup (table names etc).

    It is just good practice really to make sure that the code you write is as bug free as possible. obviously some bugs will make it through the net and thats why you turn errors off or better still write them to an error log so that you can check them.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ctx2002 View Post
    declare an variable before use it.

    regards
    Uh oh

    Code PHP:
    if($_SERVER['HTTP_X_FORWARDED_FOR']){
    	$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }elseif($_SERVER['HTTP_CLIENT_IP']){
    	$ip = $_SERVER['HTTP_CLIENT_IP'];
    }else{
    	$ip = $_SERVER['REMOTE_ADDR'];
    }
     
    $ip = $_SERVER['REMOTE_ADDR'];
    $ip = preg_match("/^[\d]([\d\.]){5,13}[\d]$/", $ip) ? $ip : 'unknown';
    Try it => Undefined Index

  13. #13
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bietchetlien View Post
    Try it => Undefined Index
    That's what array_key_exists(), isset() and empty() are for. They allow you to test the existence of an array key or variable before using it. No undefined index notices.

  14. #14
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    That's what array_key_exists(), isset() and empty() are for. They allow you to test the existence of an array key or variable before using it. No undefined index notices.
    Thanks

  15. #15
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    That's what array_key_exists(), isset() and empty() are for. They allow you to test the existence of an array key or variable before using it. No undefined index notices.
    array_key_exists is much slower than isset though, so unless a key containing a null value is a valid key you want to process it is much better to use isset.

    For the author of the thread, mainly you want to use empty() and in special cases you will use isset(). It is importent that you learn how empty() and isset() works, so you are able to see when each should be used.

    isset() is great to check if array keys exists (plan your arrays so that your able to use isset() instead of both array_key_exists and in_array), lenght of strings and of course when you need to check for a value empty would return false on.

  16. #16
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheRedDevil View Post
    isset() is great to check if array keys exists (plan your arrays so that your able to use isset() instead of both array_key_exists and in_array), lenght of strings and of course when you need to check for a value empty would return false on.
    The way I use empty is, I generally have the array $output to provided messages and errors as I process something.
    PHP Code:
    $output = array();
    if (
    $condition == true)
    {
      
    $output['message'][] = 'its true';
    } else {
      
    $output['error'][] = 'its not true';
    }

    if (empty(
    $output['error'])) echo 'its true!'
    I don't have any problems or errors using this method, so I assume this would be considered valid usage of empty?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  17. #17
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A nice and readable way to get variable value in case it's defined, and NULL in case it's not, is to use the @ operator. That's especially useful when dealing with data submitted from web forms:
    PHP Code:
    $value = @$_POST['value']; 
    Which (for me) appears to be more reable than:
    PHP Code:
    $value = isset($_POST['value']) ? $_POST['value'] : NULL

  18. #18
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    wheeler:
    Yes, using empty() to make sure a variable contain data is valid. Just remember that it will also return true on some data types.

    I guess what you posted is just an example, but just for reference. It is always best to use a identical check when using boolians, i.e. ===

    earl-grey:
    By doing that you do not remove the error/issue, you just suppress it; Which is a very bad approach.

    This is also a very slow function! Using it will be much slower than the addtional code required for empty or isset would be.

    One of the only times you should use the error suppressor (@) on is on the function gethostbyaddr() as it is not always able to find the host. Using it for anything else is to be considered a "hack".

  19. #19
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheRedDevil View Post
    By doing that you do not remove the error/issue, you just suppress it; Which is a very bad approach.
    Semantically, "undefined index" is not necessarily an error condition. Other scripting languages don't consider it to be an error and just quietly return null.

    This is also a very slow function! Using it will be much slower than the addtional code required for empty or isset would be.
    What makes you think '@' is slow?

    One of the only times you should use the error suppressor (@) on is on the function gethostbyaddr() as it is not always able to find the host. Using it for anything else is to be considered a "hack".
    There are many cases you need '@'. Most resource-allocation functions like 'fopen' blather too much in case of failure.

  20. #20
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Semantically, "undefined index" is not necessarily an error condition. Other scripting languages don't consider it to be an error and just quietly return null.
    Its not an error in the sence of the script failing, but its a programming logic error. Which in worst cases can lead to an exploit beeing found if the live site has error reporting turned on (which it in general should not but it happens). My personal opinion is that every script should not return any issue warnings what so ever, if they do they are not properly coded.

    Quote Originally Posted by stereofrog View Post
    What makes you think '@' is slow?
    It is a common fact. Php books state it, its stated at conferences etc.

    But dont take my word for it, feel free to test the crude script below to see the difference.
    PHP Code:
    $time microtime();

    for (
    $nr=0;$nr 10000;++$nr)
        {
        
    $per = @$_GET['test'];
        }

    echo (
    microtime() - $time).'<br />';

    $time microtime();    
        
    for (
    $nr=0;$nr 10000;++$nr)
        {
        if (!empty(
    $_GET['test'])) $per $_GET['test'];
        else 
    $per '';
        }    
        
    echo (
    microtime() - $time).'<br />'
    Quote Originally Posted by stereofrog View Post
    There are many cases you need '@'. Most resource-allocation functions like 'fopen' blather too much in case of failure.
    I beg to differ, why would you even use fopen if your not sure the file exists? If you check if the file exists before calling fopen it would not be failing.

  21. #21
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TheRedDevil View Post

    But dont take my word for it, feel free to test the crude script below to see the difference.
    How oft do you pull 10,000 params from request?

    I beg to differ, why would you even use fopen if your not sure the file exists? If you check if the file exists before calling fopen it would not be failing.
    File not being found is one of hundred reasons why fopen might fail.

    My point is that if you want to handle error gracefully and continue, using '@' operator is the only way in php4.

  22. #22
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    How oft do you pull 10,000 params from request?
    The number in the test code does not matter, as you know the more you repeat the sequence the easier it will be to see the difference. Though you will notice the same major difference if you only run one or 10 repeats as well... Going with 10000 repeats only give us a easier number to read.

    Quote Originally Posted by stereofrog View Post
    My point is that if you want to handle error gracefully and continue, using '@' operator is the only way in php4.
    If the code is properly structured you very seldom need to use the error suppress function. Beeing dependent on it is wrong imo.


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
  •