Page 1 of 1

Variable doesn't keep it's value

Posted: Tue Apr 10, 2007 3:39 pm
by toasty2
In my php code, I am trying to output the variable $srcds_raw. It displays the contents of the variable within the while loop, but not outside of it. See the code near the bottom.

Code: Select all

<?php
if (substr_count($_GET['address'], ":") >= "1")
{
$cut = explode(":", $_GET['address']);
$HL2_address = $cut[0];
$HL2_port = $cut[1];
}

else
{
// Default port 27015
$HL2_address = $_GET['address'];
$HL2_port = "27015";
}

$HL2_command = "\377\377\377\377TSource Engine Query\0";
$HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr);
fwrite($HL2_socket, $HL2_command); $JunkHead = fread($HL2_socket,4);
$CheckStatus = socket_get_status($HL2_socket);

if($CheckStatus["unread_bytes"] == 0)
{
$srcds_status = "Offline";
die();
}

$do = 1;
while($do)
{
$str = fread($HL2_socket,1);
$HL2_stats.= $str;
$status = socket_get_status($HL2_socket);

if($status["unread_bytes"] == 0)
{
$do = 0;
}
}
fclose($HL2_socket);
$x = 0;

while ($x <= strlen($HL2_stats))
{
$x++;
$srcds_raw = substr($HL2_stats, $x, 1);

echo $srcds_raw; // This echos the contents of the variable fine

}
echo "<br>";
echo $srcds_raw; // And this doesn't echo anything?
?>

Posted: Tue Apr 10, 2007 3:52 pm
by feyd
Whitespace added to make it legible for more people:

Code: Select all

<?php
if (substr_count($_GET['address'], ":") >= "1")
{
	$cut = explode(":", $_GET['address']);
	$HL2_address = $cut[0];
	$HL2_port = $cut[1];
}
else
{
	// Default port 27015
	$HL2_address = $_GET['address'];
	$HL2_port = "27015";
}

$HL2_command = "\377\377\377\377TSource Engine Query\0";
$HL2_socket = fsockopen("udp://".$HL2_address, $HL2_port, $errno, $errstr);
fwrite($HL2_socket, $HL2_command); $JunkHead = fread($HL2_socket,4);
$CheckStatus = socket_get_status($HL2_socket);

if($CheckStatus["unread_bytes"] == 0)
{
	$srcds_status = "Offline";
	die();
}

$do = 1;

while($do)
{
	$str = fread($HL2_socket,1);
	$HL2_stats.= $str;
	$status = socket_get_status($HL2_socket);

	if($status["unread_bytes"] == 0)
	{
		$do = 0;
	}
}

fclose($HL2_socket);
$x = 0;

while ($x <= strlen($HL2_stats))
{
	$x++;
	$srcds_raw = substr($HL2_stats, $x, 1);

	echo $srcds_raw; // This echos the contents of the variable fine
}

echo "<br>";
echo $srcds_raw; // And this doesn't echo anything?

?>

Posted: Tue Apr 10, 2007 4:56 pm
by RobertGonzalez
Try var_dumping it. And try setting it to something outside of the and before the while loop.