...

View Full Version : Undefined variable error on variable perviously used



MrBiggZ
12-03-2010, 06:55 PM
Greetings!

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



<?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 :

$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! =)

mlseim
12-03-2010, 07:17 PM
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.

MattF
12-03-2010, 07:18 PM
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:



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


Use defaults.

mlseim
12-03-2010, 07:22 PM
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.




.

MrBiggZ
12-03-2010, 07:29 PM
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! =)

mlseim
12-03-2010, 07:33 PM
Your age .... How old is "old"?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum