#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171

    Unknown php Notice


    Hello;
    In this method I refer to 3 variables. But for some reason I only get notice about one undefined variable invalid_year! Why do I get notice only for invalid_year and not for $invalid_month or $invalid_day? Thank you.
    Severity: Notice

    Message: Undefined variable: invalid_year
    PHP Code:
    public function valid_date($date)
        {
            
    $date_format explode('-',$date);
            if(!
    is_numeric($date_format['0']) || $date_format['0']<date('Y') || $date_format['0']>strtotime("+2 years"))
                {
                    
    $invalid_year=true;
                }
            if(!
    is_numeric($date_format['1']) || $date_format['1']<|| $date_format['0']>12)
                {
                    
    $invalid_month=true;
                }
            if(!
    is_numeric($date_format['2']) || $date_format['2']<|| $date_format['0']>31)
                {
                    
    $invalid_day=true;
                }
            if(!
    checkdate($date_format['1'],$date_format['2'],$date_format['0']))
                {
                    
    $invalid_date=true;
                }
             if(
    $invalid_year==true || $invalid_month==true || $invalid_day==true || $invalid_date==true)
                {
                    return 
    false;
                } 
        } 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,466
    Rep Power
    594
    Probably because when it finds the first undef used ($invalid_year) execution stops. I see a couple of issues. Why are you not initializing all those variables to false? Why are you not returning true when the 'if' fails as a matter of good programming practice?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171
    Originally Posted by gw1500se
    Probably because when it finds the first undef used ($invalid_year) execution stops. I see a couple of issues. Why are you not initializing all those variables to false? Why are you not returning true when the 'if' fails as a matter of good programming practice?
    Hello; I usually do this on top before the methods (of the class):
    PHP Code:
    $invalid_year=false
    and then further instead of
    PHP Code:
    $invalid_year=true
    I write
    PHP Code:
    $this->invalid_year=true
    . This time it worked fine with month and day! I found that behavior unexpected.

IMN logo majestic logo threadwatch logo seochat tools logo