Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    342
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Undefined variable error on variable perviously used

    Greetings!

    This script I'm playing with is behaving in a way that I don't understand!

    PHP Code:
    <?php
    /***********************************************************************/

    ini_set('display_errors'1);
    ini_set('error_reporting'E_ALL);
    ini_set('display_startup_errors'1);

    /***********************************************************************/
    $fh file("http://www.walmart.com/storeLocator/ca_storefinder_results.do?sfsearch_zip=&sfsearch_city=Indianapolis&sfsearch_state=IN&x=28&y=9&serviceName=ALL&sfatt=ALL&rx_title=&rx_dest=%2Findex.gsp");
    //$fh = file('walmart.txt');

    $walmart fopen("walmart_GPS01.txt","w");

    $regex_market        "/storeAddresses\['(.*?)'\] = {'street1' : '(.*?)',\n'city' : '(.*?)',/m";
    $regex_address        "/storeAddresses\['(.*?)'\] = {'street1' : '(.*?)',/";
    $regex_city            "/\'city\' : \'(.*?)\'/";
    $regex_state            "/\'state\': \'(.*?)\'/";
    $regex_zip            "/\'zipcode\': \'(.*?)\'/";
    $regex_phone        "/\'phone\':\'(\(\d{3}\)\s\d{3}[-]\d{4})\'/";
    $regex_lat                "/\'latitude\':\'(.*?)\'/";
    $regex_long            "/\'longitude\':\'(.*?)\'/";
    $format_phone       true;
    $line "";    

    foreach (
    $fh as $key=>$value) {
            
        if (
    preg_match_all($regex_address,$value,$matches_address))     {    
                
    $store rtrim($matches_address[1][0]);
                
    $address rtrim($matches_address[2][0],". ,");
        }    

        if (
    preg_match_all($regex_phone,$value,$matches_phone)) {
            if (
    $format_phone)  {
                
    $phone   '(' substr($matches_phone[1][0],1,3) . ') ' substr($matches_phone[1][0],6,3) . '-' substr($matches_phone[1][0],10,4); 
            }
            else     {
                
    $phone substr($matches_phone[1][0],1,3) . substr($matches_phone[1][0],6,3) . substr($matches_phone[1][0],10,4);
            }
        }

        if (
    preg_match_all($regex_lat,$value,$matches_lat))     {
                
    $lat   $matches_lat[1][0];
        }                        

        if (
    preg_match_all($regex_long,$value,$matches_long)) {
                
    $long   $matches_long[1][0];
        }                                

        
    $line =  $long " , " $lat " , \"" "Walmart " $store "; " $address "; >+" $phone "\"\n";
        
        
    fwrite($walmart,$line);

        
    $line "";
    }

        
    fclose($walmart_txt);
       echo 
    "DONE!";
    ?>
    When I get to :
    PHP Code:
        $line =  $long " , " $lat " , \"" "Walmart " $store "; " $address "; >+" $phone "\"\n"
    I get these errors:

    Notice: Undefined variable: long in /home/mrbiggz6/public_html/test/wally/walmart.php on line 49

    Notice: Undefined variable: lat in /home/mrbiggz6/public_html/test/wally/walmart.php on line 49

    Notice: Undefined variable: store in /home/mrbiggz6/public_html/test/wally/walmart.php on line 49

    Notice: Undefined variable: address in /home/mrbiggz6/public_html/test/wally/walmart.php on line 49

    Notice: Undefined variable: phone in /home/mrbiggz6/public_html/test/wally/walmart.php on line 49
    I don't understand why because the variable has been used earlier on in the script.

    Thanks for you help! =)
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Those are "warning" errors (or notices) ...

    Maybe not really a problem, but it's scolding you about them not having a value.
    You don't give them a value before the "if" statement, and the "if" statement is false,
    so it is still not given a value.

    You can turn-off those warnings by altering your top lines:

    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
    ini_set('display_startup_errors', 1);

    Change to:

    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL ^ E_NOTICE);
    ini_set('display_startup_errors', 1);


    Alternatively,
    You can assign something ("" = nothing ... or empty) to them at the top.
    An "empty" value is defining it as empty ... but it is defined.

    $long="";
    $lat="";
    $store="";

    etc.

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Those variables haven't been used previously. Unless all of the preg_ blocks evaluate to true, various of those vars may or may not be set, i.e:

    Code:
        if (preg_match_all($regex_long,$value,$matches_long)) {
                $long   = $matches_long[1][0];
        }
    Use defaults.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    If you think you've used them before, you might be assuming that using them
    on a different script will "carry them over" ... but your webhost most likely has
    "register_globals" turned off, which is the correct thing to do.

    In the past, "register_globals" was enabled ... but not anymore.
    That is probably where the change took place. Your webhost upgraded their PHP version.




    .

  • #5
    Regular Coder MrBiggZ's Avatar
    Join Date
    Apr 2005
    Location
    Indianapolis IN
    Posts
    342
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Ok .. that what's a thought. I knew that if I just threw something in there that it'd burp about it.

    Well, I feel a little less stupid now! =)
    “No matter how slick the demo is in rehearsal, when you do it in front of a live audience, the probability of a flawless presentation is inversely proportional to the number of people watching, raised to the power of the amount of money involved.” ~ Mark Gibbs

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Your age .... How old is "old"?


  •  

    Posting Permissions

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