SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help needed with undeclared indexes

    I have an older script that needs updating due to the amount of errors it produces for such things as undeclared variables, indexes, offsets, etc. From what I can read up on, the issue appears to be a lack of isset usage in the script. When I try to correct the code however, I am obviously not incorporating the isset function properly, as it is giving results differently then intended, and definitely different then it did without the isset function. Here's an example of one of the more complex functions, showing the usage of undeclared indexes throughout it. While the function is long, I wanted to show all of it to give a thorough example of what I am facing. Can someone show me what the proper usage of isset would be with the code below, or am I completely offbase with using isset? Or is this simply too long of an example to give?

    Thanks in advance!

    Dennis

    PHP Code:
    function showEvent$data ) {
        global 
    $citations$notestogether$text;

        
    $dateplace $data['date'] || $data['place'] ? 0;
        
    $rows $dateplace;
        if( 
    $data['fact'] ) $rows++;
        if( 
    $data['note'] && $notestogether $rows++;
        
    $output "";
        
    $cite $data['citekey'] ? reorderCitation$data['citekey'] ) : "";
        
        
    $preoutput "<tr>\n";
        
    $preoutput .= "<td valign=\"top\" class=\"fieldnameback\" nowrap rowspan=\"$rows\"><span class=\"fieldname\">$data[text]&nbsp;</span></td>\n";
        if( 
    $dateplace ) {
            if( 
    $data['date'] ) {
                
    $output .= "<td valign=\"top\" class=\"databack\"";
                if( !
    $data['place'] )
                    
    $output .= " colspan='2'";
                
    $output .= "><span class=\"normal\"><nobr>" displayDate$data['date'] );
                if( !
    $data['place'] && $cite ) {
                    
    $output .= "&nbsp; [$cite]";
                    
    $cite "";
                }
                
    $output .= "</nobr>&nbsp;</span></td>\n";
            }
            if( 
    $data['place'] ) {
                
    $output .= "<td valign=\"top\" width=\"80%\" class=\"databack\"";
                if( !
    $data['date'] )
                    
    $output .= " colspan='2'";
                
    $output .= "><span class=\"normal\">$data[place]";
                if( 
    $cite ) {
                    
    $output .= "&nbsp; [$cite]";
                    
    $cite "";
                }
                
    $output .= "&nbsp;</span></td>\n";
            }
            
    $output .= "</tr>\n";
        }
        elseif( !
    $data['fact'] && $cite )
            
    $data['fact'] = $text['yesabbr'];
        if( 
    $data['fact'] ) {
            if( 
    $output $output .= "<tr>\n";
            
    $output .= "<td valign=\"top\" colspan=\"2\" class=\"databack\"><span class=\"normal\">" nl2br$data['fact'] );
            if( 
    $cite ) {
                
    $output .= "&nbsp; [$cite]";
                
    $cite "";
            }
            
    $output .= "&nbsp;</span></td>\n";
            
    $output .= "</tr>\n";
        }
        if( 
    $data['note'] && $notestogether ) {
            if( 
    $output $output .= "<tr>\n";
            
    $output .= "<td valign=\"top\" colspan=\"2\" class=\"databack\"><span class=\"normal\"><ul>" $data['note']['text'];
            if( 
    $cite ) {
                
    $output .= "&nbsp; [$cite]";
                
    $cite "";
            }
            
    $output .= "</ul>&nbsp;</span></td>\n";
            
    $output .= "</tr>\n";
        }
        if( 
    $output )
            echo 
    $preoutput $output;


  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The author appears to be making a lot of "phantom" calls to empty() rather than isset(). The former tells whether an expression evaluates to an "empty" PHP value such as 0, false, null, a 0-length string, or an array with 0 elements. This is not the same as testing whether a variable is set.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An easier solution would be to change error_reporting in php.ini to something a little more relaxed...

    Code:
    error_reporting = E_ALL & ~E_NOTICE
    The better solution would be to write code that passes all those basics standards tests -- but when dealing with lagcy code this is a good solution.
    The only constant in software is change itself

  4. #4
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PCSpectra View Post
    An easier solution would be to change error_reporting in php.ini to something a little more relaxed...

    Code:
    error_reporting = E_ALL & ~E_NOTICE
    The better solution would be to write code that passes all those basics standards tests -- but when dealing with lagcy code this is a good solution.
    Thanks PCSpectra... I would really like to fix the issues though, and not simply turn off error reporting. The main reason is this is an active site (the log file when errors are turned on fills up so fast as to shut Apache down in only a few hours, due to the size allowance for the file). So there are quite a bit of errors having to be handled by php and I imagine that is having an overall effect on the performance of the script, even with reporting minimized or turned off. Somebody, please correct me if I'm wrong in that assumption.

    Let me provide a shorter piece of code, and see if I can get some examples on how others might code it:

    PHP Code:
    function getBirthInfo$thisperson ) {
        global 
    $text;
        
        
    $birthstring "";
        if( isset(
    $thisperson['birthdate']) ) {
            
    $birthstring .= ", $text[birthabbr] " displayDate$thisperson['birthdate'] );
            if( 
    $thisperson['birthplace'] )
                
    $birthstring .= ", $thisperson[birthplace]";
        }
        else if( isset(
    $thisperson['altbirthdate']) ) {
            
    $birthstring .= ", $text[chrabbr] " displayDate$thisperson['altbirthdate'] );
            if( 
    $thisperson['altbirthplace'] ) 
                
    $birthstring .= ", $thisperson[altbirthplace]";
        }
        return 
    $birthstring;


  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just want to test if the variable exists, and does not have a value of type NULL, then use isset(). isset() will still return true for things like an empty string/empty array/string or integer zero/boolean false etc...

    If you want to evaluate the value as a boolean, and not raise php notices, then use !empty($var). if (!empty($var)) and if ($var) have identical behavior, except using !empty() will not raise a php notice in the event the variable is undefined. So this is probably what you want to use here, because it is the existing behavior of your code, which presumably works correctly aside from throwing notices.


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
  •