Variable doesn't keep it's value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Variable doesn't keep it's value

Post 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?
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try var_dumping it. And try setting it to something outside of the and before the while loop.
Post Reply