Jump to content

Undefined Variable


michael.davis

Recommended Posts

Hi!  I have written some code to strip the weather forecast of a text file and it is working just fine.  In my error_log, I noticed the following errors:

 

[Wed Jul 11 12:56:25 2012] [error] [client 205.165.229.208] PHP Notice:  Undefined variable: state in /sync/public/intranet/ohx/tvwx/index.php on line 114

[Wed Jul 11 12:56:25 2012] [error] [client 205.165.229.208] PHP Notice:  Undefined variable: state in /sync/public/intranet/ohx/tvwx/index.php on line 117

 

Line 114 refers to $state == 0;

Line 117 refers to    if ($state == 0) {

 

I have this section in bold in my code.

 

Can someone point me in the right direciton to correct this?

 

Thanks!

Mike

 

My code is listed below:

<?php


function getHeadlines($product)
{
[b]	$retarray = array();
$lines = split("\n", $product);
$state == 0;

foreach ($lines as $line) {
	if ($state == 0) {
		if (substr($line,0,3) == "...") {
			$headline = $line . "\n";
			$state = 1;[/b]
		}
	} else if ($state == 1) {
		if (preg_match("/^\s*$/", $line)) {
			$headlines .= $headline . "\n";
			$retarray[] = $headlines;
			$headlines="";
			$state = 0;
		} else {
			$headline .= $line;
		}
	}
}
return $retarray;
}

function expandUGC($str)
{
$str = ereg_replace("/[\r\n]/", "", $str);
$codes = split("-", $str);

foreach ($codes as $code) {
	if (preg_match("/^([A-Z]{2}([CZ]))/", $code, $m)) {
		$loc = $m[0];
		$typ = $m[2];
	}
	if (preg_match("/^\d{6}[A-Z]?$/", $code)) {
		$tm = $code;
		break;
	} else if (preg_match("/(\d\d\d)>(\d\d\d)\s*$/", $code, $m)) { 

		for ($i = $m[1]; $i <= $m[2]; $i++) { 
			if ($typ == "C" && !($i % 2)) { 
			// bypass even numbers for FIPS codes 
			} else { 
				$retarray[] = 
					sprintf ("%s%03d", $loc, $i);
			}
		}
	} else if (preg_match ("/(\d{3})$/", $code, $m)) { 
		$retarray[] = $loc . $m[0]; 
	} else { 
		// probably an error
		break;
	}
}
return join("-", $retarray) . "-" . $tm . "-";
}

function getSegments($str)
{
$retarray = array(); $text = strtok ($str, "\$\$"); 
while ($text) { 
	if (preg_match("/^.*\n([A-Z]{2}[CZ]\d\d\d.*)$/sm",
			$text, $seg)) { 
		$text = $seg[1];
	}
	if (preg_match("/^[A-Z]/", $text)) {
		$retarray[] = $text;
	}
	$text = strtok("\$\$");
}
return $retarray;
}

function getSegmentsByUGC($product, $code) 
{ 
$retarray = array(); 
$segments = getSegments($product); 
foreach ($segments as $segment) { 
	$ugc = expandUGC($segment); 
	if (preg_match ("/$code/i", $ugc)) { 
		$retarray[] = $segment;
	}
}
return $retarray;
} 

// function that returns x periods in an array...

function getPeriods($product, $num)
{
$retarray = array();
$lines = split("\n", $product);
$state = 0;
$count = 0;

foreach ($lines as $line) {
	if ($state == 0) {
		if (preg_match ("/^\.[A-Z].*\.\.\./", $line)) {
			$period = "$line\n";
			$state = 1;
		}
	} else if ($state == 1) {
		if (preg_match("/^\.[A-Z].*\.\.\./", $line)) {
			$retarray[] = "$period\n";
			$count++;
			$period = "$line\n";
		} else {
			$period .= "$line\n";
		}
	}
	if ($count >= $num) {
		break;
	}
}
return $retarray;
}

$file_array = file("ZFPOHX"); 
$prod=join("", $file_array); 
$segments = getSegmentsByUGC($prod, "TNZ027");

$headlines=getHeadlines($segments[0]);

//print "" . join("<BR>", $headlines) . "\n";

$periods_one_and_two = getPeriods($segments[0], 1);

print "" . join("   ", $periods_one_and_two) . "   ";

?>

Link to comment
https://forums.phpfreaks.com/topic/265526-undefined-variable/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.